Question

What is the best practice for storing/retrieving API keys in rails3?

Should I create my own application yaml and access it through there? If so, how?

Sorry for the noob question...

Was it helpful?

Solution

I use the settingslogic plugin for things like this. Very easy to use.

Add settingslogic to your Gemfile and bundle install:

gem 'settingslogic'

Create a directory for your settings and place the settingslogic yaml in there:

/my_app/config/settings/my_settings.yml

You can include default settings and per environment settings. The file looks like this:

defaults: &defaults
  api_key: abc123

development:
  <<: *defaults

test:
  <<: *defaults

production:
  <<: *defaults

Add this file: app/models/my_settings.rb, start up your app and you are good to go

class MySettings < Settingslogic
  source "#{Rails.root}/config/settings/my_settings.yml"
  namespace Rails.env
end

Now you can use call these settings from anywhere in the app like so:

MySettings.api_key
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top