Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: Get hostname in an initializer

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?

like image 316
Andrew Avatar asked Dec 28 '22 04:12

Andrew


2 Answers

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

like image 53
Joe Lencioni Avatar answered Jan 09 '23 02:01

Joe Lencioni


Use:

`hostname`

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

like image 27
Ryan Porter Avatar answered Jan 09 '23 01:01

Ryan Porter