Class: Rack::Parser
- Inherits:
-
Object
- Object
- Rack::Parser
- 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
-
#handlers ⇒ Object
readonly
Returns the value of attribute handlers.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#parsers ⇒ Object
readonly
Returns the value of attribute parsers.
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, options = {}) ⇒ Parser
constructor
A new instance of Parser.
-
#warn!(error, type) ⇒ Object
Private: send a warning out to the logger.
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, = {}) @app = app @parsers = .delete(:parsers) || { %r{json} => JSON_PARSER } @handlers = .delete(:handlers) || {} @logger = .delete(:logger) end |
Instance Attribute Details
#handlers ⇒ Object (readonly)
Returns the value of attribute handlers.
11 12 13 |
# File 'lib/rack/parser.rb', line 11 def handlers @handlers end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
11 12 13 |
# File 'lib/rack/parser.rb', line 11 def logger @logger end |
#parsers ⇒ Object (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 = "[Rack::Parser] Error on %s : %s" % [type, error.to_s] logger.warn end |