class Dox::Printers::ExampleResponsePrinter

Attributes

example[RW]

Public Instance Methods

print(example) click to toggle source

Private Instance Methods

add_content(body) click to toggle source
# File lib/dox/printers/example_response_printer.rb, line 27
def add_content(body)
  add_content_name(body['content'] = find_or_add(body, 'content'))
end
add_content_name(body) click to toggle source
# File lib/dox/printers/example_response_printer.rb, line 31
def add_content_name(body)
  resp_header = find_headers(example.response_headers)

  add_example(body[resp_header] = find_or_add(body, resp_header))
  add_schema(body[resp_header], Dox.config.schema_response_folder_path)
end
add_desc(body) click to toggle source
# File lib/dox/printers/example_response_printer.rb, line 44
def add_desc(body)
  body[example.desc] = { 'summary' => example.desc,
                         'value' => formatted_body(example.response_body,
                                                   find_headers(example.response_headers)) }
end
add_example(body) click to toggle source
# File lib/dox/printers/example_response_printer.rb, line 38
def add_example(body)
  return if example.response_body.empty?

  add_desc(body['examples'] = find_or_add(body, 'examples'))
end
add_example_response() click to toggle source
# File lib/dox/printers/example_response_printer.rb, line 13
def add_example_response
  add_statuses(find_or_add(find_or_add(spec, 'responses'), example.response_status.to_s))
end
add_headers(body) click to toggle source
# File lib/dox/printers/example_response_printer.rb, line 81
def add_headers(body)
  body['headers'] = Hash[example.response_headers.map { |key, value| [key, { description: value }] }]
end
add_schema(body, path) click to toggle source
# File lib/dox/printers/example_response_printer.rb, line 50
def add_schema(body, path)
  return unless path

  schema = find_schema

  return unless schema

  add_schema_to_hash(body, path, schema)
end
add_schema_to_hash(body, path, schema) click to toggle source
# File lib/dox/printers/example_response_printer.rb, line 68
def add_schema_to_hash(body, path, schema)
  body['schema'] =
    if schema.is_a?(Pathname)
      { '$ref' => schema }
    else
      { '$ref' => File.join(path, "#{schema}.json") }
    end
end
add_status_desc(body) click to toggle source
# File lib/dox/printers/example_response_printer.rb, line 23
def add_status_desc(body)
  body['description'] = Util::Http::HTTP_STATUS_CODES[example.response_status]
end
add_statuses(body) click to toggle source
# File lib/dox/printers/example_response_printer.rb, line 17
def add_statuses(body)
  add_status_desc(body)
  add_content(body)
  add_headers(body)
end
find_headers(headers) click to toggle source
# File lib/dox/printers/example_response_printer.rb, line 77
def find_headers(headers)
  headers.find { |key, _| key == 'Content-Type' }&.last || 'any'
end
find_schema() click to toggle source
# File lib/dox/printers/example_response_printer.rb, line 60
def find_schema
  if example.response_success?
    example.response_schema_success
  else
    example.response_schema_fail || Dox.config.schema_response_fail_file_path
  end
end