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/deep_hash_set.rb,
lib/rspec_api_docs/formatter/resource/example/request_headers.rb

Defined Under Namespace

Classes: DeepHashSet, RequestHeaders

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(example) ⇒ Example

Returns a new instance of Example.



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

def initialize(example)
  @example = example
end

Instance Attribute Details

#exampleObject (readonly)

Returns the value of attribute example.



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

def example
  @example
end

Instance Method Details

#descriptionString

The description of the example.

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

Returns:

  • (String)


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

def description
  [:description]
end

#http_methodString?

The HTTP method of first route requested.

Returns:

  • (String, nil)


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

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

#inspectObject



98
99
100
# File 'lib/rspec_api_docs/formatter/resource/example.rb', line 98

def inspect
  "#<RspecApiDocs::Resource::Example #{name.inspect}>"
end

#nameString

The name of the example.

E.g. “Returns a Character”

Returns:

  • (String)


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

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

#notesHash<Symbol,String>?

Returns:

  • (Hash<Symbol,String>, nil)


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

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

#parametersArray<Parameter>

Parameters for the example.

Returns:



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

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

#pathString?

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

Returns:

  • (String, nil)


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

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

#precedenceString?

Returns:

  • (String, nil)


94
95
96
# File 'lib/rspec_api_docs/formatter/resource/example.rb', line 94

def precedence
  [:example_precedence]
end

#requestsArray<Hash>

Requests stored for the example.

Returns:

  • (Array<Hash>)


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

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:



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

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