Rails: get hostname in initializer

I am using Sorcery for authentication and I need to set up third party authentication in its initializer.

The initializer has a string that looks like this:

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

      

... where example.dev

is the hostname when I use Pow in local development. This should be the example.com

case if the app is in production, or staging.example.com

if it is in production, etc.

I would like to set this line like this:

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

      

... but request.host

is the only method I know of this and this is only available at the controller level.

I can use a conditional test and manually configure the hostname for each environment, but as I test in different local and staging environments, it would be great to just set this up programmatically.

Any suggestions?

+3


source to share


2 answers


Using:

`hostname`

      



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

+5


source


You can get the hostname via Socket.gethostname

, which is part of the Ruby standard library.



+4


source







All Articles