Sha256: 3a24842fa9c02b065079ab176ee5f26f912b0a855e073e9ef4ba2be6a85add69

Contents?: true

Size: 754 Bytes

Versions: 1

Compression:

Stored size: 754 Bytes

Contents

# encoding: utf-8

module Hyperdrive
  module Middleware
    class ParseJSONBody
      def initialize(app)
        @app = app
      end

      def call(env)
        req = Rack::Request.new(env)
        if req.media_type =~ /json$/
          begin
            json_body = MultiJson.load(env['rack.input'].read)
            env["rack.input"].rewind
          rescue => e
            puts "JSONParseError: #{e.message}"
            raise Hyperdrive::Errors::JSONParseError.new
          end
          env['rack.request.form_hash'] ||= {}
          env['rack.request.form_hash'].merge!(json_body)
          env['rack.request.form_input'] = Rack::Utils.build_query(env['rack.request.form_hash'])
        end
        @app.call(env)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hyperdrive-0.0.14 lib/hyperdrive/middleware/parse_json_body.rb