Class: OpenapiFirst::RuntimeResponse
- Inherits:
-
Object
- Object
- OpenapiFirst::RuntimeResponse
- 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
-
#error ⇒ Failure?
readonly
Error object if validation failed.
Instance Method Summary collapse
-
#body ⇒ Hash, String
Returns the parsed (JSON) body of the response.
-
#description ⇒ String?
Returns the description of the response definition if available.
-
#headers ⇒ Hash
Returns the headers of the response as defined in the API description.
-
#initialize(operation, rack_response) ⇒ RuntimeResponse
constructor
A new instance of RuntimeResponse.
-
#known? ⇒ Boolean
Checks if the response is defined in the API description.
-
#known_status? ⇒ Boolean
Checks if the response status is defined in the API description.
-
#response_definition ⇒ Definition::Response?
Returns the response definition associated with the response.
-
#valid? ⇒ Boolean
Checks if the response is valid.
-
#validate ⇒ Failure?
Validates the response.
-
#validate! ⇒ Object
Validates the response and raises an error if invalid.
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
#error ⇒ Failure? (readonly)
Returns Error object if validation failed.
19 20 21 |
# File 'lib/openapi_first/runtime_response.rb', line 19 def error @error end |
Instance Method Details
#body ⇒ Hash, String
Returns the parsed (JSON) 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 |
#description ⇒ String?
Returns the description of the response definition if available.
49 50 51 |
# File 'lib/openapi_first/runtime_response.rb', line 49 def description response_definition&.description end |
#headers ⇒ Hash
Returns the headers of the response as defined in the API description. This only returns the headers that are defined in the API description.
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.
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.
43 44 45 |
# File 'lib/openapi_first/runtime_response.rb', line 43 def known_status? @operation.response_status_defined?(status) end |
#response_definition ⇒ Definition::Response?
Returns the response definition associated with the response.
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.
30 31 32 33 |
# File 'lib/openapi_first/runtime_response.rb', line 30 def valid? validate unless @validated @error.nil? end |
#validate ⇒ Failure?
Validates the response.
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.
75 76 77 78 |
# File 'lib/openapi_first/runtime_response.rb', line 75 def validate! error = validate error&.raise! end |