Class: Rack::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/parser.rb

Constant Summary collapse

POST_BODY =
'rack.input'.freeze
FORM_INPUT =
'rack.request.form_input'.freeze
FORM_HASH =
'rack.request.form_hash'.freeze
JSON_PARSER =
proc { |data| JSON.parse data }
ERROR_HANDLER =
proc { |err, type| [400, {}, ['']] }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Parser.



13
14
15
16
17
18
# File 'lib/rack/parser.rb', line 13

def initialize(app, options = {})
  @app      = app
  @parsers  = options.delete(:parsers)  || { %r{json} => JSON_PARSER }
  @handlers = options.delete(:handlers) || {}
  @logger   = options.delete(:logger)
end

Instance Attribute Details

#handlersObject (readonly)

Returns the value of attribute handlers.



11
12
13
# File 'lib/rack/parser.rb', line 11

def handlers
  @handlers
end

#loggerObject (readonly)

Returns the value of attribute logger.



11
12
13
# File 'lib/rack/parser.rb', line 11

def logger
  @logger
end

#parsersObject (readonly)

Returns the value of attribute parsers.



11
12
13
# File 'lib/rack/parser.rb', line 11

def parsers
  @parsers
end

Instance Method Details

#call(env) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rack/parser.rb', line 20

def call(env)
  type   = Rack::Request.new(env).media_type
  parser = parsers.detect { |content_type, _| type.match(content_type) } if type
  return @app.call(env) unless parser
  body = env[POST_BODY].read ; env[POST_BODY].rewind
  return @app.call(env) unless body && !body.empty?
  begin
    parsed = parser.last.call body
    env.update FORM_HASH => parsed, FORM_INPUT => env[POST_BODY]
  rescue StandardError => e
    warn! e, type
    handler   = handlers.detect { |content_type, _|  type.match(content_type) }
    handler ||= ['default', ERROR_HANDLER]
    return handler.last.call(e, type)
  end
  @app.call env
end

#warn!(error, type) ⇒ Object

Private: send a warning out to the logger

error - Exception object type - String of the Content-Type



43
44
45
46
47
# File 'lib/rack/parser.rb', line 43

def warn!(error, type)
  return unless logger
  message = "[Rack::Parser] Error on %s : %s" % [type, error.to_s]
  logger.warn message
end