Class: Rack::JsonStringify

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/json_stringify.rb,
lib/rack/json_stringify/version.rb

Constant Summary collapse

VERSION =
"0.0.3"

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ JsonStringify

Returns a new instance of JsonStringify.



7
8
9
# File 'lib/rack/json_stringify.rb', line 7

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rack/json_stringify.rb', line 11

def call(env)
  @env = env

  status, headers, body = @app.call(env)
 
  return [400,{},[]] if self.stringify? && !self.json_response?(headers['Content-Type'])

  if self.stringify?
    json = ''
    body.each { |s| json << s }
    body = [JSON.dump(self.stringify(JSON.parse(json)))]
    headers['Content-Length'] = Rack::Utils.bytesize(body[0]).to_s
  end

  [status, headers, body]
end