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.



55
56
57
# File 'lib/openapi_first/runtime_response.rb', line 55

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.



49
50
51
# File 'lib/openapi_first/runtime_response.rb', line 49

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.



62
63
64
# File 'lib/openapi_first/runtime_response.rb', line 62

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.



37
38
39
# File 'lib/openapi_first/runtime_response.rb', line 37

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.



43
44
45
# File 'lib/openapi_first/runtime_response.rb', line 43

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

#response_definitionDefinition::Response?

Returns the response definition associated with the response.

Returns:



82
83
84
# File 'lib/openapi_first/runtime_response.rb', line 82

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)


30
31
32
33
# File 'lib/openapi_first/runtime_response.rb', line 30

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.



68
69
70
71
# File 'lib/openapi_first/runtime_response.rb', line 68

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.



75
76
77
78
# File 'lib/openapi_first/runtime_response.rb', line 75

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