Class: OpenapiFirst::RuntimeResponse

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/openapi_first/runtime_response.rb

Overview

Represents a response returned by the Rack application and how it relates to the API description.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(operation, rack_response) ⇒ RuntimeResponse

Returns a new instance of RuntimeResponse.



12
13
14
15
16
# File 'lib/openapi_first/runtime_response.rb', line 12

def initialize(operation, rack_response)
  @operation = operation
  @rack_response = rack_response
  @error = nil
end

Instance Attribute Details

#errorFailure? (readonly)

Returns Error object if validation failed.

Returns:

  • (Failure, nil)

    Error object if validation failed.



19
20
21
# File 'lib/openapi_first/runtime_response.rb', line 19

def error
  @error
end

Instance Method Details

#bodyHash, String

Returns the parsed (JSON) body of the response.

Returns:

  • (Hash, String)

    Returns the body of the response.



57
58
59
# File 'lib/openapi_first/runtime_response.rb', line 57

def body
  @body ||= content_type =~ /json/i ? load_json(original_body) : original_body
end

#descriptionString?

Returns the description of the response definition if available.

Returns:

  • (String, nil)

    Returns the description of the response, or nil if not available.



51
52
53
# File 'lib/openapi_first/runtime_response.rb', line 51

def description
  response_definition&.description
end

#headersHash

Returns the headers of the response as defined in the API description. This only returns the headers that are defined in the API description.

Returns:

  • (Hash)

    Returns the headers of the response.



64
65
66
# File 'lib/openapi_first/runtime_response.rb', line 64

def headers
  @headers ||= unpack_response_headers
end

#known?Boolean

Checks if the response is defined in the API description.

Returns:

  • (Boolean)

    Returns true if the response is known, false otherwise.



39
40
41
# File 'lib/openapi_first/runtime_response.rb', line 39

def known?
  !!response_definition
end

#known_status?Boolean

Checks if the response status is defined in the API description.

Returns:

  • (Boolean)

    Returns true if the response status is known, false otherwise.



45
46
47
# File 'lib/openapi_first/runtime_response.rb', line 45

def known_status?
  @operation.response_status_defined?(status)
end

#nameString

Returns name The name of the operation. Used for generating error messages.

Returns:

  • (String)

    name The name of the operation. Used for generating error messages.



26
27
28
# File 'lib/openapi_first/runtime_response.rb', line 26

def name
  "#{@operation.name} response status: #{status}"
end

#response_definitionDefinition::Response?

Returns the response definition associated with the response.

Returns:



84
85
86
# File 'lib/openapi_first/runtime_response.rb', line 84

def response_definition
  @response_definition ||= @operation.response_for(status, content_type)
end

#valid?Boolean

Checks if the response is valid. Runs the validation unless it has been run before.

Returns:

  • (Boolean)


32
33
34
35
# File 'lib/openapi_first/runtime_response.rb', line 32

def valid?
  validate unless @validated
  @error.nil?
end

#validateFailure?

Validates the response.

Returns:

  • (Failure, nil)

    Returns the validation error, or nil if the response is valid.



70
71
72
73
# File 'lib/openapi_first/runtime_response.rb', line 70

def validate
  @validated = true
  @error = ResponseValidation::Validator.new(@operation).validate(self)
end

#validate!Object

Validates the response and raises an error if invalid.

Raises:

  • (ResponseNotFoundError, ResponseInvalidError)

    Raises an error if the response is invalid.



77
78
79
80
# File 'lib/openapi_first/runtime_response.rb', line 77

def validate!
  error = validate
  error&.raise!
end