Class: Praxis::Docs::OpenApi::ResponseObject

Inherits:
Object
  • Object
show all
Defined in:
lib/praxis/docs/open_api/response_object.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(info:) ⇒ ResponseObject

Returns a new instance of ResponseObject.



12
13
14
15
16
17
18
# 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

Instance Attribute Details

#infoObject (readonly)



10
11
12
# File 'lib/praxis/docs/open_api/response_object.rb', line 10

def info
  @info
end

Instance Method Details

#dumpObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# 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) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# 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