Question

I'm trying to load a yaml config file during initialization of my Rails 3.1 app, and the call to YAML.load never returns. Here is my initializer file:

STRIPE_CONFIG = begin
  config = YAML.load(Rails.root.join('config', 'stripe.yml')) || {}
  config = config[Rails.env] || {}
  config.to_options
end

And here is my stripe.yml file:

default: &default
  api_key:    test
  public_key: test

development:
  <<: *default

test:
  <<: *default

production:
  api_key:    prod
  public_key: prod

For whatever reason, the YAML.load call never returns. If I perform a stack trace, it seems to be stuck on syck.rb line 135. The interesting thing is, the longer I let my app sit before breaking, the more calls to line 135 appear.

/Users/mhuggins/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/syck.rb:135:in `read'
/Users/mhuggins/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/syck.rb:135:in `read'
/Users/mhuggins/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/syck.rb:135:in `load'
/Users/mhuggins/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/syck.rb:135:in `load'
/Users/mhuggins/Sites/dating/config/initializers/stripe.rb:2:in `<top (required)>'
...

I've tried explicitly using Psych as well instead of using Syck, but with no luck. (It ends up hanging as well.)

STRIPE_CONFIG = begin
  require 'psych'
  config = Psych.load(Rails.root.join('config', 'stripe.yml')) || {}
  config = config[Rails.env] || {}
  config.to_options
end
Was it helpful?

Solution

Ugh, apparently I just needed to explicitly read the file. I changed this:

YAML.load(Rails.root.join('config', 'stripe.yml'))

to this:

YAML.load(File.open(Rails.root.join('config', 'stripe.yml')))

OTHER TIPS

Answer will be kinda late, but I have stumbled upon similar problem just now ;)

You could use YAML::load_file, which expects filename as an argument.

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