Created
January 3, 2014 05:43
-
-
Save jessereynolds/8233428 to your computer and use it in GitHub Desktop.
json_params_parser.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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