4

I'm using Sorcery for Authentication, and I need to setup third party authentication in its initializer.

The initializer has a line that looks like this:

config.twitter.callback_url "http://example.dev/auth/callback?provider=twitter"

...where example.dev is the hostname when I'm using Pow in local development. This needs to be example.com if the app is in production, or staging.example.com if it's in staging, etc.

I would like to set this line to be something like this:

config.twitter.callback_url "#{Rails.hostname}/auth/callback?provider=twitter"

... but request.host is the only method I know of that knows that and it's only available at the controller level.

I can use a conditional test and manually setup a hostname for each environment, but as I test on different local and staging environments it would be great to just be able to set this programatically.

Any suggestions?

2 Answers 2

6

You can get the hostname via Socket.gethostname which is part of the Ruby Standard Library.

5

Use:

`hostname`

That uses the Unix "hostname" utility, and it returns a string.

2
  • 1
    with the backticks? as in config.twitter.callback_url "#{`hostname`}/..." ?
    – Andrew
    Mar 19, 2012 at 2:55
  • Right, yes, backticks. You can test it pretty easily with the Rails console. Just go into the console and type "hostname" with the backticks to see what it will return. Mar 19, 2012 at 3:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.