Sinatra with Figaro Gem

杀马特。学长 韩版系。学妹 提交于 2019-12-12 02:45:48

问题


I m trying to use Figaro gem with Sinatra. I have installed Figaro and it created the following file/ folder...

/config/application.yml

On this file I have added some environment variables...

ENV['GMAIL_USERNAME']
ENV['GMAIL_PASSWORD']

Then on my my "app.rb" file I am trying to include the yml file like...

require 'config/application.yml'

How can I have access to my "ENV['BIG_SECRET']" in my app.rb file?

Mail.defaults do
  delivery_method :smtp, {
    :address => 'smtp.gmail.com',
    :port => '587',
    :domain => 'mydomain.com',
    :user_name => ENV['GMAIL_USERNAME'],
    :password => ENV['GMAIL_PASSWORD'],
    :authentication => :plain,
    :enable_starttls_auto => true
  }
end

回答1:


You should declare yuor variables in application.yml as explained at Figaro github README https://github.com/laserlemon/figaro (in YAML syntax):

pusher_app_id: "2954"
pusher_key: "7381a978f7dd7f9a1117"
pusher_secret: "abdc3b896a0ffb85d373"

test:
  pusher_app_id: "5112"
  pusher_key: "ad69caf9a44dcac1fb28"
  pusher_secret: "83ca7aa160fedaf3b350"
...

You will be able to access the declared variables as follows:

ENV["stripe_api_key"] # => "sk_live_dSqzdUq80sw9GWmuoI0qJ9rL"
ENV.key?("stripe_api_key") # => true
ENV["google_analytics_key"] # => nil
ENV.key?("google_analytics_key") # => false


来源:https://stackoverflow.com/questions/32213138/sinatra-with-figaro-gem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!