Sha256: c0f9c3cc74ab949121c768e6075e6bc7a090a447011891177c56129067f91d55

Contents?: true

Size: 815 Bytes

Versions: 11

Compression:

Stored size: 815 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'])
          env['rack.input'] = env['rack.request.form_input']
        end
        @app.call(env)
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
hyperdrive-0.0.25 lib/hyperdrive/middleware/parse_json_body.rb
hyperdrive-0.0.24 lib/hyperdrive/middleware/parse_json_body.rb
hyperdrive-0.0.23 lib/hyperdrive/middleware/parse_json_body.rb
hyperdrive-0.0.22 lib/hyperdrive/middleware/parse_json_body.rb
hyperdrive-0.0.21 lib/hyperdrive/middleware/parse_json_body.rb
hyperdrive-0.0.20 lib/hyperdrive/middleware/parse_json_body.rb
hyperdrive-0.0.19 lib/hyperdrive/middleware/parse_json_body.rb
hyperdrive-0.0.18 lib/hyperdrive/middleware/parse_json_body.rb
hyperdrive-0.0.17 lib/hyperdrive/middleware/parse_json_body.rb
hyperdrive-0.0.16 lib/hyperdrive/middleware/parse_json_body.rb
hyperdrive-0.0.15 lib/hyperdrive/middleware/parse_json_body.rb