8

I'm working to integrate facebook in my app. To do that I'm using koala, devise, and omniauth.

For koala, I have

/config/facebook.yml

development:
    app_id: 123123132123
    secret_key: dasadsasd1231231
test:
    app_id: 313131313
    secret_key: das132asdads12132
production:
    app_id: dasdsadsadsadsa
    secret_key: adsdsa12das123fds21

I then also have with omniauth:

/config/initalizers/omniauth.rb

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :facebook, 123123132123, 'dasadsasd1231231'
end

Problem here is I have the same value repeated in both locations. How can I dry this up so that the app_id and secret_key only live in one place and one file references the other?

Cœur
  • 37,241
  • 25
  • 195
  • 267
AnApprentice
  • 108,152
  • 195
  • 629
  • 1,012

2 Answers2

10

There is a great Railscast on doing this: http://railscasts.com/episodes/85-yaml-configuration-file

In omniauth.rb, you can add this line:

FACEBOOK_CONFIG = YAML.load_file("#{RAILS_ROOT}/config/facebook.yml")[RAILS_ENV]

Then you can do:

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :facebook, FACEBOOK_CONFIG['app_id'], FACEBOOK_CONFIG['secret_key']
end
Dylan Markow
  • 123,080
  • 26
  • 284
  • 201
  • Helped me with a similar question about S3 config, http://stackoverflow.com/questions/6305523/rails3-how-to-get-at-aws-s3s-yml-config-data-in-the-app/6306039#6306039, thanks! – Max Williams Jun 10 '11 at 12:00
3

In addition to the above answer you might need to use slightly different code to load the file:

FACEBOOK_CONFIG = YAML.load_file(Rails.root.join("config","facebook.yml"))[Rails.env]
Benjamin
  • 727
  • 1
  • 9
  • 19