class RspecApiDocs::Resource::Example

Constants

MAX_PRECEDENCE

Attributes

example[R]

Public Class Methods

new(example) click to toggle source
# File lib/rspec_api_docs/formatter/resource/example.rb, line 11
def initialize(example)
  @example = example
end

Public Instance Methods

description() click to toggle source

The description of the example.

E.g. “For getting information about a Character.”

@return [String]

# File lib/rspec_api_docs/formatter/resource/example.rb, line 29
def description
  metadata[:description]
end
http_method() click to toggle source

The HTTP method of first route requested.

@return [String, nil]

# File lib/rspec_api_docs/formatter/resource/example.rb, line 85
def http_method
  return if request_response_pairs.empty?
  request_response_pairs.first.first.request_method
end
inspect() click to toggle source
# File lib/rspec_api_docs/formatter/resource/example.rb, line 100
def inspect
  "#<RspecApiDocs::Resource::Example #{name.inspect}, precedence: #{precedence.inspect}>"
end
name() click to toggle source

The name of the example.

E.g. “Returns a Character”

@return [String]

# File lib/rspec_api_docs/formatter/resource/example.rb, line 20
def name
  metadata.fetch(:example_name, example.description)
end
notes() click to toggle source

@return [Hash<Symbol,String>, nil]

# File lib/rspec_api_docs/formatter/resource/example.rb, line 91
def notes
  metadata.fetch(:note, {})
end
parameters() click to toggle source

Parameters for the example.

@return [Array<Parameter>]

# File lib/rspec_api_docs/formatter/resource/example.rb, line 36
def parameters
  metadata.fetch(:parameters, []).map do |name_hash, parameter|
    Parameter.new(name_hash[:name], parameter)
  end
end
path() click to toggle source

Path stored on the example OR the path of first route requested.

@return [String, nil]

# File lib/rspec_api_docs/formatter/resource/example.rb, line 75
def path
  metadata.fetch(:path) do
    return if request_response_pairs.empty?
    request_response_pairs.first.first.path
  end
end
precedence() click to toggle source

@return [Integer]

# File lib/rspec_api_docs/formatter/resource/example.rb, line 96
def precedence
  metadata.fetch(:example_precedence, MAX_PRECEDENCE)
end
requests() click to toggle source

Requests stored for the example.

@return [Array<Hash>]

# File lib/rspec_api_docs/formatter/resource/example.rb, line 54
def requests # rubocop:disable Metrics/AbcSize
  request_response_pairs.map do |request, response|
    {
      request_method: request.request_method,
      request_path: request_path(request),
      request_body: request_body(request.body),
      request_headers: request_headers(request.env),
      request_query_parameters: request.params,
      request_content_type: request.content_type,
      response_status: response.status,
      response_status_text: response_status_text(response.status),
      response_body: response_body(response.body, content_type: response.content_type),
      response_headers: response_headers(response.headers),
      response_content_type: response.content_type,
    }
  end
end
response_fields() click to toggle source

Response fields for the example.

@return [Array<ResponseField>]

# File lib/rspec_api_docs/formatter/resource/example.rb, line 45
def response_fields
  metadata.fetch(:fields, []).map do |name_hash, field|
    ResponseField.new(name_hash[:name], field)
  end
end

Private Instance Methods

metadata() click to toggle source
# File lib/rspec_api_docs/formatter/resource/example.rb, line 154
def metadata
  example.metadata[METADATA_NAMESPACE]
end
request_body(body) click to toggle source
# File lib/rspec_api_docs/formatter/resource/example.rb, line 128
def request_body(body)
  body.rewind if body.respond_to?(:rewind)
  body_content = body.read
  body.rewind if body.respond_to?(:rewind)
  body_content.empty? ? nil : body_content
end
request_headers(env) click to toggle source
# File lib/rspec_api_docs/formatter/resource/example.rb, line 110
def request_headers(env)
  RequestHeaders.call(env)
end
request_path(request) click to toggle source
# File lib/rspec_api_docs/formatter/resource/example.rb, line 122
def request_path(request)
  URI(request.path).tap do |uri|
    uri.query = request.query_string unless request.query_string.empty?
  end.to_s
end
request_response_pairs() click to toggle source
# File lib/rspec_api_docs/formatter/resource/example.rb, line 106
def request_response_pairs
  metadata.fetch(:requests, []).reject { |pair| pair.any?(&:nil?) }
end
response_body(body, content_type:) click to toggle source
# File lib/rspec_api_docs/formatter/resource/example.rb, line 135
def response_body(body, content_type:)
  unless body.empty? || !content_type.to_s.include?('json')
    parsed_body = JSON.parse(body, symbolize_names: true)
    response_fields.each do |f|
      unless f.example.nil?
        DeepHashSet.call(parsed_body, f.scope + [f.name], f.example)
      end
    end
    if metadata[:response_body_after_hook]
      parsed_body = metadata[:response_body_after_hook].call(parsed_body)
    end
    JSON.dump(parsed_body)
  end
end
response_headers(headers) click to toggle source
# File lib/rspec_api_docs/formatter/resource/example.rb, line 114
def response_headers(headers)
  excluded_headers = RspecApiDocs.configuration.exclude_response_headers

  headers.reject do |k, v|
    excluded_headers.include?(k)
  end
end
response_status_text(status) click to toggle source
# File lib/rspec_api_docs/formatter/resource/example.rb, line 150
def response_status_text(status)
  Rack::Utils::HTTP_STATUS_CODES[status]
end