Class: Rack::Middleware::JsonParser

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

Defined Under Namespace

Classes: Error

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#appObject (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