2
\$\begingroup\$

I'm working on a simple superfeedr powered rails app here.

Based on the superfeedr-rack gem documentation, I'm doing this to initialize the middleware (snippet from application.rb config block):

Configuration = YAML.load_file(Rails.root.join('config', 'config.yml'))
config.middleware.use Rack::Superfeedr, { :host => Configuration["general"]["hostname"], :login => Configuration["superfeedr"]["username"], :password => Configuration["superfeedr"]["password"]} do |superfeedr|
 Superfeedr = superfeedr
 superfeedr.on_notification do |notification|
 Article.create_from_raw_notification(notification)
 end
end

I'm looking for a better way to do it - I don't like to load my configuration file there instead of in an initializer and I think the block with the article creation callback smells. Any ideas?

Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Apr 2, 2013 at 1:10
\$\endgroup\$
2
  • \$\begingroup\$ Try Rails.configuration.middleware \$\endgroup\$ Commented Apr 4, 2013 at 20:00
  • \$\begingroup\$ @tapajos your answer was converted to a comment because it was too short. If you'd like to expand it with an example and a better description, you're welcome to do so. \$\endgroup\$ Commented Apr 5, 2013 at 11:34

1 Answer 1

1
\$\begingroup\$

Based on tapajos comment, I was able to to refactor the code using initializers. First I created one to load the configuration file:

APP_CONFIG = YAML.load_file(File.expand_path('../../config.yml', __FILE__))

Then I created another initializer, like this:

superfeedr_config = {
 :host => APP_CONFIG["general"]["hostname"],
 :login => APP_CONFIG["superfeedr"]["username"],
 :password => APP_CONFIG["superfeedr"]["password"]
}
Rails.configuration.middleware.use Rack::Superfeedr, superfeedr_config do |superfeedr|
 Superfeedr = superfeedr
 superfeedr.on_notification {|notification| Article.create_from_raw_notification(notification)}
end

I also applied the logic described in this SO question to ensure the load order: How do I change the load order of initializers in Rails 3?.

answered Apr 10, 2013 at 5:05
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.