class Praxis::Docs::OpenApi::ResponseObject

Attributes

Public Class Methods

new(info:) click to toggle source
# File lib/praxis/docs/open_api/response_object.rb, line 12
def initialize(info:)
  @info = info
  default_handlers = ApiDefinition.instance.info.produces
  @output_handlers = Praxis::Application.instance.handlers.select do |k, _v|
    default_handlers.include?(k)
  end
end

Public Instance Methods

dump() click to toggle source
# File lib/praxis/docs/open_api/response_object.rb, line 51
def dump
  data = {
    description: info.description || ''
  }
  if (headers_object = dump_response_headers_object(info.headers))
    data[:headers] = headers_object
  end

  if info.media_type

    identifier = MediaTypeIdentifier.load(info.media_type.identifier)
    example_handlers = @output_handlers.each_with_object([]) do |(name, _handler), accum|
      accum.push({ (identifier + name).to_s => name })
    end
    data[:content] = MediaTypeObject.create_content_attribute_helper(
      type: info.media_type,
      example_payload: info.example(nil),
      example_handlers: example_handlers
    )
  end

  # if payload = info[:payload]
  #   body_type= payload[:id]
  #   raise "WAIT! response payload doesn't have an existing id for the schema!!! (do an if, and describe it if so)" unless body_type
  #   data[:schema] = {"$ref" => "#/definitions/#{body_type}" }
  # end

  # TODO: we do not support 'links'
  data
end
dump_response_headers_object(headers) click to toggle source
# File lib/praxis/docs/open_api/response_object.rb, line 20
def dump_response_headers_object(headers)
  headers.each_with_object({}) do |(name, data), accum|
    # each header comes from Praxis::ResponseDefinition
    # the keys are the header names, and value can be:
    #  "true"  => means it only needs to exist
    #  String => which means that it has to fully match
    #  Regex  => which means it has to regexp match it

    # Get the schema from the type (defaulting to string in case the type doesn't have the as_json_schema defined)
    schema = begin
      data[:attribute].type.as_json_schema
    rescue StandardError
      { type: :string }
    end
    hash = { description: data[:description] || '', schema: schema }
    # Note, our Headers in response definition are not full types...they're basically only
    # strings, which can either match anything, match the exact word or match a regex
    # they don't even have a description...
    data_value = data[:value]
    case data_value
    when String
      hash[:pattern] = "^#{data_value}$" # Exact String match
    when Regexp
      sanitized_pattern = data_value.inspect[1..-2] # inspect returns enclosing '/' characters
      hash[:pattern] = sanitized_pattern
    end

    accum[name] = hash
  end
end