module Rack::Cargo::ResponseBuilder

Public Class Methods

call(request, state) click to toggle source
# File lib/rack/cargo/response_builder.rb, line 7
def call(request, state)
  app_response = state.fetch(:app_response)

  name = request.fetch(REQUEST_NAME, autogenerated_name)

  state[:response] = {
    RESPONSE_NAME => name,
    RESPONSE_PATH => request.fetch(REQUEST_PATH),
    RESPONSE_STATUS => app_response.fetch(:status),
    RESPONSE_HEADERS => app_response.fetch(:headers),
    RESPONSE_BODY => response_body(app_response.fetch(:body))
  }
end

Private Class Methods

autogenerated_name() click to toggle source
# File lib/rack/cargo/response_builder.rb, line 23
def autogenerated_name
  "unnamed_#{SecureRandom.hex(6)}"
end
response_body(app_response_body) click to toggle source

Transform body object to String according to Rack specification requirements.

# File lib/rack/cargo/response_builder.rb, line 29
def response_body(app_response_body)
  response_string = ""
  app_response_body.each { |piece| response_string += piece }
  return if response_string.empty?

  JSON.parse(response_string)
end