Skip to content

Commit

Permalink
override for JSON formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
Philip (flip) Kromer committed Aug 2, 2011
1 parent 2db659b commit 4efea94
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions lib/senor_armando/goliath_extensions.rb
Expand Up @@ -112,3 +112,39 @@ def safely(env, headers={})

end
end
require 'multi_json'

module Goliath
module Rack
module Formatters
# A JSON formatter. Uses MultiJson so you can use the JSON
# encoder that is right for your project.
#
# @example
# use Goliath::Rack::Formatters::JSON
class JSON
include AsyncMiddleware

def post_process(env, status, headers, body)
if self.class.applies_format?(headers)
body = self.class.format(body)
end
[status, headers, body]
end

def json_response?(headers)
self.class.applies_format?(headers)
end

def self.format(body)
[MultiJson.encode(body)]
end

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

end
end
end
end

0 comments on commit 4efea94

Please sign in to comment.