Class: Goliath::Rack::Formatters::JSON
- Inherits:
-
Object
- Object
- Goliath::Rack::Formatters::JSON
- 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.
Instance Method Summary (collapse)
- - (Object) call(env)
-
- (Goliath::Rack::Formatters::JSON) initialize(app)
constructor
Called by the framework to create the formatter.
- - (Boolean) json_response?(headers)
- - (Object) post_process(status, headers, body)
Constructor Details
- (Goliath::Rack::Formatters::JSON) initialize(app)
Called by the framework to create the formatter.
15 16 17 |
# File 'lib/goliath/rack/formatters/json.rb', line 15 def initialize(app) @app = app end |
Instance Method Details
- (Object) call(env)
19 20 21 22 23 24 25 26 27 |
# File 'lib/goliath/rack/formatters/json.rb', line 19 def call(env) async_cb = env['async.callback'] env['async.callback'] = Proc.new do |status, headers, body| async_cb.call(post_process(status, headers, body)) end status, headers, body = @app.call(env) post_process(status, headers, body) end |
- (Boolean) json_response?(headers)
36 37 38 |
# File 'lib/goliath/rack/formatters/json.rb', line 36 def json_response?(headers) headers['Content-Type'] =~ %r{^application/(json|javascript)} end |
- (Object) post_process(status, headers, body)
29 30 31 32 33 34 |
# File 'lib/goliath/rack/formatters/json.rb', line 29 def post_process(status, headers, body) if json_response?(headers) body = [MultiJson.encode(body)] end [status, headers, body] end |