class Sparrow::ResponseMiddleware

Handles the Response conversion

Public Instance Methods

call(env) click to toggle source

This call ends the rack chain @param [Hash] env the Rack environment @return [Array<String, Hash, Array<String>>] the Rack return Array

# File lib/sparrow/response_middleware.rb, line 9
def call(env)
  @last_env                = env
  @status, @headers, @body = app.call(env)
  [status, headers, converted_response_body]
end

Private Instance Methods

converted_response_body() click to toggle source
# File lib/sparrow/response_middleware.rb, line 26
def converted_response_body
  # return the original body if we are not going to process it
  return body unless steward.has_processable_http_message?

  response_body = Sparrow::Strategies::JsonFormatStrategy.convert(body)

  return [] if response_body.blank?

  @headers.delete 'Content-Length'
  response_strategy = strategy.new(last_env, :response, response_body)
  response_strategy.handle
  Array(response_strategy.json_body)
end
http_message() click to toggle source
# File lib/sparrow/response_middleware.rb, line 40
def http_message
  response_message         = ResponseHttpMessage.new(last_env)
  response_message.status  = status
  response_message.body    = body
  response_message.headers = headers
  response_message
end
steward() click to toggle source
# File lib/sparrow/response_middleware.rb, line 17
def steward
  configuration = Sparrow.configuration
  ResponseSteward.new(http_message,
                      allowed_content_types:  configuration.allowed_content_types,
                      allowed_accepts:        configuration.allowed_accepts,
                      excluded_routes:        configuration.excluded_routes,
                      ignored_response_codes: configuration.ignored_response_codes)
end