Skip to content

Instantly share code, notes, and snippets.

@jessereynolds
Created January 3, 2014 05:43
Show Gist options
  • Select an option

  • Save jessereynolds/8233428 to your computer and use it in GitHub Desktop.

Select an option

Save jessereynolds/8233428 to your computer and use it in GitHub Desktop.
json_params_parser.rb
#!/usr/bin/env ruby
require 'rack'
module Flapjack
module Gateways
module JSONAPI
module Rack
class JsonParamsParser < Struct.new(:app)
def call(env)
if env['rack.input'] and not input_parsed?(env) and type_match?(env)
env['rack.request.form_input'] = env['rack.input']
data = env['rack.input'].read
env['rack.input'].rewind
env['rack.request.form_hash'] = data.empty? ? {} : Oj.load(data)
end
app.call(env)
end
def input_parsed? env
env['rack.request.form_input'].eql? env['rack.input']
end
def type_match? env
type = env['CONTENT_TYPE'] and
Flapjack::Gateways::JSONAPI::JSON_REQUEST_MIME_TYPES.include?(type.split(/\s*[;,]\s*/, 2).first.downcase)
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment