class Rack::JsonPrettifier

Constants

CONTENT_LENGTH
CONTENT_TYPE
VERSION

Public Class Methods

new(app) click to toggle source
# File lib/rack/json_prettifier.rb, line 9
def initialize(app)
  @app = app
end

Public Instance Methods

call(env) click to toggle source
# File lib/rack/json_prettifier.rb, line 13
def call(env)
  status, headers, response = @app.call(env)
  if headers[CONTENT_TYPE] =~ /^application\/json/
    begin
      obj = JSON.parse(response.body)
      pretty_str = JSON.pretty_unparse(obj)
      response = [pretty_str]
      headers[CONTENT_LENGTH] = Rack::Utils.bytesize(pretty_str).to_s
    rescue JSON::ParserError
    end
  end
  [status, headers, response]
end