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.
-
#name ⇒ String
Name The name of the operation.
-
#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.
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 |
#description ⇒ String?
Returns the description of the response definition if available.
51 52 53 |
# File 'lib/openapi_first/runtime_response.rb', line 51 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.
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.
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.
45 46 47 |
# File 'lib/openapi_first/runtime_response.rb', line 45 def known_status? @operation.response_status_defined?(status) end |
#name ⇒ String
Returns 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_definition ⇒ Definition::Response?
Returns the response definition associated with the response.
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.
32 33 34 35 |
# File 'lib/openapi_first/runtime_response.rb', line 32 def valid? validate unless @validated @error.nil? end |
#validate ⇒ Failure?
Validates the response.
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.
77 78 79 80 |
# File 'lib/openapi_first/runtime_response.rb', line 77 def validate! error = validate error&.raise! end |