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
- #call(env) ⇒ Object
-
#initialize(app) ⇒ Goliath::Rack::Formatters::JSON
constructor
Called by the framework to create the formatter.
- #json_response?(headers) ⇒ Boolean
- #post_process(status, headers, body) ⇒ Object
Constructor Details
#initialize(app) ⇒ Goliath::Rack::Formatters::JSON
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
#call(env) ⇒ Object
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 |
#json_response?(headers) ⇒ Boolean
36 37 38 |
# File 'lib/goliath/rack/formatters/json.rb', line 36 def json_response?(headers) headers['Content-Type'] =~ %r{^application/(json|javascript)} end |
#post_process(status, headers, body) ⇒ Object
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 |