Class: RspecApiDocs::Resource::Example

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec_api_docs/formatter/resource/example.rb,
lib/rspec_api_docs/formatter/resource/example/request_headers.rb

Defined Under Namespace

Classes: RequestHeaders

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(example) ⇒ Example

Returns a new instance of Example.



8
9
10
# File 'lib/rspec_api_docs/formatter/resource/example.rb', line 8

def initialize(example)
  @example = example
end

Instance Attribute Details

#exampleObject (readonly)

Returns the value of attribute example.



6
7
8
# File 'lib/rspec_api_docs/formatter/resource/example.rb', line 6

def example
  @example
end

Instance Method Details

#descriptionString

The description of the example.

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

Returns:

  • (String)


26
27
28
# File 'lib/rspec_api_docs/formatter/resource/example.rb', line 26

def description
  [:description]
end

#http_methodString?

The HTTP method of first route requested.

Returns:

  • (String, nil)


82
83
84
85
# File 'lib/rspec_api_docs/formatter/resource/example.rb', line 82

def http_method
  return if request_response_pairs.empty?
  request_response_pairs.first.first.request_method
end

#nameString

The name of the example.

E.g. “Returns a Character”

Returns:

  • (String)


17
18
19
# File 'lib/rspec_api_docs/formatter/resource/example.rb', line 17

def name
  .fetch(:example_name, example.description)
end

#notesHash<Symbol,String>?

Returns:

  • (Hash<Symbol,String>, nil)


88
89
90
# File 'lib/rspec_api_docs/formatter/resource/example.rb', line 88

def notes
  .fetch(:note, {})
end

#parametersArray<Parameter>

Parameters for the example.

Returns:



33
34
35
36
37
# File 'lib/rspec_api_docs/formatter/resource/example.rb', line 33

def parameters
  .fetch(:parameters, []).map do |name, parameter|
    Parameter.new(name, parameter)
  end
end

#pathString?

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

Returns:

  • (String, nil)


72
73
74
75
76
77
# File 'lib/rspec_api_docs/formatter/resource/example.rb', line 72

def path
  .fetch(:path) do
    return if request_response_pairs.empty?
    request_response_pairs.first.first.path
  end
end

#requestsArray<Hash>

Requests stored for the example.

Returns:

  • (Array<Hash>)


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rspec_api_docs/formatter/resource/example.rb', line 51

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),
      response_headers: response.headers,
      response_content_type: response.content_type,
    }
  end
end

#response_fieldsArray<ResponseField>

Response fields for the example.

Returns:



42
43
44
45
46
# File 'lib/rspec_api_docs/formatter/resource/example.rb', line 42

def response_fields
  .fetch(:fields, []).map do |name, field|
    ResponseField.new(name, field)
  end
end