Class: Goliath::Rack::Formatters::JSON

Inherits:
Object
  • Object
show all
Includes:
AsyncMiddleware
Defined in:
lib/goliath/rack/formatters/json.rb

Overview

A JSON formatter. Uses MultiJson so you can use the JSON encoder that is right for your project.

Examples:

use Goliath::Rack::Formatters::JSON

Instance Method Summary (collapse)

Methods included from AsyncMiddleware

#call, #initialize

Constructor Details

This class inherits a constructor from Goliath::Rack::AsyncMiddleware

Instance Method Details

- (Boolean) json_response?(headers)

Returns:

  • (Boolean)


21
22
23
# File 'lib/goliath/rack/formatters/json.rb', line 21

def json_response?(headers)
  headers['Content-Type'] =~ %r{^application/(json|javascript)}
end

- (Object) post_process(env, status, headers, body)



14
15
16
17
18
19
# File 'lib/goliath/rack/formatters/json.rb', line 14

def post_process(env, status, headers, body)
  if json_response?(headers)
    body = [MultiJson.encode(body)]
  end
  [status, headers, body]
end