西纳特拉与费加罗 Gem

Sinatra with Figaro Gem

我正在尝试将 Figaro gem 与 Sinatra 一起使用。我已经安装了 Figaro 并创建了以下文件/文件夹...

/config/application.yml

在这个文件中我添加了一些环境变量...

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

然后在我的 "app.rb" 文件中,我试图包含 yml 文件,例如...

require 'config/application.yml'

如何在我的 app.rb 文件中访问我的 "ENV['BIG_SECRET']"?

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

您应该按照 Figaro github 自述文件 https://github.com/laserlemon/figaro 中的说明在 application.yml 中声明您的变量(使用 YAML 语法):

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

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

您将能够按如下方式访问声明的变量:

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