Skip to content

Instantly share code, notes, and snippets.

@jjthrash
Created June 8, 2011 14:16
Show Gist options
  • Save jjthrash/1014493 to your computer and use it in GitHub Desktop.
Save jjthrash/1014493 to your computer and use it in GitHub Desktop.
Heroku API config (use ENV
# Use like this:
# QuickConfig.api(:spreedly, 'SPREEDLY_CORE_LOGIN', 'SPREEDLY_CORE_SECRET') do
# SpreedlyCore.configure(ENV['SPREEDLY_CORE_LOGIN'],
# ENV['SPREEDLY_CORE_SECRET'],
# SPREEDLY_CORE[Rails.env]['token'])
# end
#
# or
#
# QuickConfig.api(:janrain, 'JANRAIN_API_KEY')
#
# Looks for a file in the project root, e.g. .spreedly, or .janrain in the
# above cases, and loads it to optionally set env vars for local development
#
# e.g.:
# ENV['SPREEDLY_CORE_LOGIN'] = 'asdfasdf'
# ENV['SPREEDLY_CORE_SECRET'] = 'fdsafdsa'
#
# Otherwise just defaults to using environment variables. Set your variables
# in your Heroku config to keep them out of your code.
class QuickConfig
def self.api(name, *vars)
File.join(Rails.root, ".#{name}").tap do |local_config|
load local_config if File.exist?(local_config)
end
if vars.inject(true) {|m,k| m && ENV[k].present?}
yield if block_given?
else
Rails.logger.warn <<-WARNING
You must configure your #{name.to_s.titleize} credentials in the following environment variables:
#{vars.join("\n ")}
WARNING
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment