class Praxis::Response

Attributes

response_name[RW]
status[RW]
body[RW]
headers[RW]
name[R]
parts[R]
request[RW]
status[RW]

Public Class Methods

inherited(klass) click to toggle source
# File lib/praxis/response.rb, line 13
def self.inherited(klass)
  klass.response_name = klass.name.demodulize.underscore.to_sym
  klass.status = status if status
end
new(status: self.class.status, headers: {}, body: nil, location: nil) click to toggle source
# File lib/praxis/response.rb, line 18
def initialize(status: self.class.status, headers: {}, body: nil, location: nil)
  @name    = response_name
  @status  = status
  @headers = headers
  @body    = body
  @headers['Location'] = location if location
  @form_data = nil
  @parts = {}
end

Public Instance Methods

add_part(part, name = nil) click to toggle source
# File lib/praxis/response.rb, line 46
def add_part(part, name = nil)
  @form_data ||= begin
    form = MIME::Multipart::FormData.new
    @headers.merge! form.headers.headers
    form
  end

  name ||= "part-#{part.object_id}"
  part.name = name
  @parts[name.to_s] = part
end
content_type() click to toggle source

Determine the content type of this response.

@return [MediaTypeIdentifier]

# File lib/praxis/response.rb, line 31
def content_type
  MediaTypeIdentifier.load(headers['Content-Type']).freeze
end
content_type=(identifier) click to toggle source

Set the content type for this response. @todo DRY this out (also used in Multipart::Part)

@return [String] @param [String,MediaTypeIdentifier] identifier

# File lib/praxis/response.rb, line 40
def content_type=(identifier)
  headers['Content-Type'] = MediaTypeIdentifier.load(identifier).to_s
end
encode!() click to toggle source
# File lib/praxis/response.rb, line 64
def encode!
  case @body
  when Hash, Array
    # response payload is structured data; transform it into an entity using the handler
    # implied by the response's media type. If no handler is registered for this
    # name, assume JSON as a default handler.
    handlers = Praxis::Application.instance.handlers
    handler = (content_type && handlers[content_type.handler_name]) || handlers['json']
    @body = handler.generate(@body)
  end
end
finish() click to toggle source
# File lib/praxis/response.rb, line 76
def finish
  format!
  encode!

  @body = Array(@body)

  if @form_data
    @body << "\r\n" if @body.any? && @body.last !~ /\n$/

    @parts.each do |name, part|
      part.encode!
      entity = MIME::Text.new(part.body)

      part.headers.each do |header_name, header_value|
        entity.headers.set header_name, header_value
      end

      @form_data.add entity, name
    end

    @body << @form_data.body.to_s
  end

  [@status, @headers, @body]
end
format!() click to toggle source
# File lib/praxis/response.rb, line 62
def format!; end
handle() click to toggle source
# File lib/praxis/response.rb, line 44
def handle; end
response_name() click to toggle source
# File lib/praxis/response.rb, line 58
def response_name
  self.class.response_name
end
validate(action, validate_body: false) click to toggle source

Validates the response

@param [Object] action

# File lib/praxis/response.rb, line 106
def validate(action, validate_body: false)
  return if response_name == :validation_error

  unless (response_definition = action.responses[response_name])
    raise Exceptions::Validation, "Attempting to return a response with name #{response_name} " \
      'but no response definition with that name can be found'
  end
  response_definition.validate(self, validate_body: validate_body)
rescue Exceptions::Validation => e
  ve = Application.instance.validation_handler.handle!(
    summary: 'Error validating response',
    exception: e,
    request: request,
    stage: 'response',
    errors: e.errors
  )
  body = ve.format!

  Responses::InternalServerError.new(body: body)
end