class Rack::Hackery::PrettyJsonResponse

Prettify JSON responses

Public Class Methods

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

Public Instance Methods

call(env) click to toggle source
# File lib/rack/hackery/pretty_json_response.rb, line 9
def call(env)
  status, headers, response = @app.call(env)

  if headers['Content-Type'] =~ /^application\/json/
    object = JSON.parse(response.body)
    string = JSON.pretty_unparse(object)
    response = [string]
    headers['Content-Length'] = Rack::Utils.bytesize(string).to_s
  end

  [status, headers, response]
end