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
PARSER_RESULT =
'rack.parser.result'.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.



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

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

Instance Attribute Details

#handlersObject (readonly)

Returns the value of attribute handlers.



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

def handlers
  @handlers
end

#loggerObject (readonly)

Returns the value of attribute logger.



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

def logger
  @logger
end

#parsersObject (readonly)

Returns the value of attribute parsers.



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

def parsers
  @parsers
end

Instance Method Details

#call(env) ⇒ Object



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

def call(env)
  type   = Rack::Request.new(env).media_type
  parser = match_content_types_for(parsers, 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[PARSER_RESULT] = parsed
    env.update FORM_HASH => parsed, FORM_INPUT => env[POST_BODY] if parsed.is_a?(Hash)
  rescue StandardError => e
    warn! e, type
    handler   = match_content_types_for handlers, type
    handler ||= ['default', ERROR_HANDLER]
    return handler.last.call(e, type)
  end
  @app.call env
end

#match_content_types_for(content_types, type) ⇒ Object

Private: matches content types for the given media type

content_types - An array of the parsers or handlers options type - The media type. gathered from the Rack::Request

Returns The match from the parser/handler hash or nil



57
58
59
60
61
# File 'lib/rack/parser.rb', line 57

def match_content_types_for(content_types, type)
  content_types.detect do |content_type, _|
    content_type.is_a?(Regexp) ? type.match(content_type) : type == content_type
  end
end

#warn!(error, type) ⇒ Object

Private: send a warning out to the logger

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



45
46
47
48
49
# File 'lib/rack/parser.rb', line 45

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