Rails edge case solved with middleware

Recently I was working with a friend, @sidho on an interesting problem. Sid built this awesome app for tracking beers. It’s a Rails app that pulls some stuff from a brewery API. He setup a webhook and was receiving updates from the API. Problem was: the data posted to the webhook included the key “action” which was used to denote what type of change was happening. By default, when a request is routed, Rails sets the key “action” to the controller action name and the key “controller” to the name of the controller. I spent a little time searching, and looked at the source for about 20 min before deciding the best solution would be to some how intercept the params, rename the key action to something else and then let Rails do its thing.

Here’s our first ever Rack middleware and it’s only job is to rename an incoming param with the name “action” to “beer_db_action”.

# lib/params_fixer.rb
class ParamsFixer
  def initialize(app)
    @app = app
  end

  def call(env)
    request = Rack::Request.new(env)
    if request.params['action']
      request.update_param('beer_db_action', request.params['action'])
    end
    status, headers, resp = @app.call(env)
    [status, headers, resp]
  end
end

# config/application.rb
config.autoload_paths += Dir["#{config.root}/lib/**/"]
config.middleware.use "ParamsFixer"

Checkout our solution if you’re interested 🙂

I’m hoping to write a pull request for the gem/make a Rails version of the gem.

Advertisements
Rails edge case solved with middleware

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s