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

Constant Summary collapse

MAX_PRECEDENCE =
9_999

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(example) ⇒ Example

Returns a new instance of Example.



11
12
13
# File 'lib/rspec_api_docs/formatter/resource/example.rb', line 11

def initialize(example)
  @example = example
end

Instance Attribute Details

#exampleObject (readonly)

Returns the value of attribute example.



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

def example
  @example
end

Instance Method Details

#descriptionString

The description of the example.

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

Returns:

  • (String)


29
30
31
# File 'lib/rspec_api_docs/formatter/resource/example.rb', line 29

def description
  [:description]
end

#http_methodString?

The HTTP method of first route requested.

Returns:

  • (String, nil)


85
86
87
88
# 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

#inspectObject



100
101
102
# File 'lib/rspec_api_docs/formatter/resource/example.rb', line 100

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

#nameString

The name of the example.

E.g. “Returns a Character”

Returns:

  • (String)


20
21
22
# File 'lib/rspec_api_docs/formatter/resource/example.rb', line 20

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

#notesHash<Symbol,String>?

Returns:

  • (Hash<Symbol,String>, nil)


91
92
93
# File 'lib/rspec_api_docs/formatter/resource/example.rb', line 91

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

#parametersArray<Parameter>

Parameters for the example.

Returns:



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

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)


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

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

#precedenceInteger

Returns:

  • (Integer)


96
97
98
# File 'lib/rspec_api_docs/formatter/resource/example.rb', line 96

def precedence
  .fetch(:example_precedence, MAX_PRECEDENCE)
end

#requestsArray<Hash>

Requests stored for the example.

Returns:

  • (Array<Hash>)


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# 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_fieldsArray<ResponseField>

Response fields for the example.

Returns:



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

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