Class: Rack::Middleware::JsonParser
- Inherits:
-
Object
- Object
- Rack::Middleware::JsonParser
- Defined in:
- lib/rack/middleware/json_parser.rb
Defined Under Namespace
Classes: Error
Instance Attribute Summary collapse
-
#app ⇒ Object
readonly
Returns the value of attribute app.
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app) ⇒ JsonParser
constructor
A new instance of JsonParser.
Constructor Details
#initialize(app) ⇒ JsonParser
Returns a new instance of JsonParser.
10 11 12 |
# File 'lib/rack/middleware/json_parser.rb', line 10 def initialize(app) @app = app end |
Instance Attribute Details
#app ⇒ Object (readonly)
Returns the value of attribute app.
8 9 10 |
# File 'lib/rack/middleware/json_parser.rb', line 8 def app @app end |
Instance Method Details
#call(env) ⇒ Object
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/rack/middleware/json_parser.rb', line 14 def call(env) if env['CONTENT_TYPE'] == 'application/json' and (%w[put post].include?(env['REQUEST_METHOD'].downcase)) input = env['rack.input'] input.rewind env['rack.json'] = Oj.load(input.read, symbol_keys: true) end app.call(env) rescue Oj::ParseError raise Error end |