class RestCore::JsonResponse

Constants

JSON_RESPONSE_HEADER

Public Class Methods

members() click to toggle source
# File lib/rest-core/middleware/json_response.rb, line 6
def self.members; [:json_response]; end

Public Instance Methods

call(env) { |process(response)| ... } click to toggle source
# File lib/rest-core/middleware/json_response.rb, line 20
def call env, &k
  return app.call(env, &k) if env[DRY]
  return app.call(env, &k) unless json_response(env)

  app.call(env.merge(REQUEST_HEADERS =>
    JSON_RESPONSE_HEADER.merge(env[REQUEST_HEADERS]||{}))){ |response|
      yield(process(response))
    }
end
process(response) click to toggle source
# File lib/rest-core/middleware/json_response.rb, line 30
def process response
  body = response[RESPONSE_BODY]
  json = if body.kind_of?(String)
           Json.normalize(body)
         else
           # Yajl supports streaming, so let's pass it directly to make
           # it possible to do streaming here. Although indeed we should
           # use RESPONSE_SOCKET in this case, but doing that could
           # introduce some incompatibility which I don't want to take
           # care of for now.
           body
         end

  response.merge(RESPONSE_BODY => Json.decode(json))
rescue Json.const_get(:ParseError) => error
  fail(response, ParseError.new(error, body))
end