Class: Rack::PostBodyMsgpackParser

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/post_body_msgpack_parser.rb,
lib/rack/post_body_msgpack_parser/version.rb

Constant Summary collapse

CONTENT_TYPE =
'CONTENT_TYPE'.freeze
POST_BODY =
'rack.input'.freeze
FORM_INPUT =
'rack.request.form_input'.freeze
FORM_HASH =
'rack.request.form_hash'.freeze
FORM_HASH_MSGPACK =
'rack.request.form_hash_msgpack'.freeze
APPLICATION_MSGPACK_MIMES =
["application/x-msgpack", "application/x-mpac"]
VERSION =
"0.1.0"

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ PostBodyMsgpackParser

Returns a new instance of PostBodyMsgpackParser.



17
18
19
20
# File 'lib/rack/post_body_msgpack_parser.rb', line 17

def initialize(app, options={})
  @app = app
  @options = options
end

Instance Method Details

#call(env) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rack/post_body_msgpack_parser.rb', line 26

def call(env)
  if APPLICATION_MSGPACK_MIMES.include?(Rack::Request.new(env).media_type) && (body = env[POST_BODY].read).length != 0
    env[POST_BODY].rewind # somebody might try to read this stream
    body = MessagePack.unpack body
    if override_params?
      env.update(FORM_HASH => body, FORM_INPUT => env[POST_BODY])
    end
    env.update(FORM_HASH_MSGPACK => body)
  end
  @app.call(env)
end

#override_params?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/rack/post_body_msgpack_parser.rb', line 22

def override_params?
  !! @options[:override_params]
end