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
# File 'lib/openapi_first/runtime_response.rb', line 12

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

Instance Attribute Details

#errorFailure?

Returns Error object if validation failed.

Returns:

  • (Failure, nil)

    Error object if validation failed.



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

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.



59
60
61
# File 'lib/openapi_first/runtime_response.rb', line 59

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.



53
54
55
# File 'lib/openapi_first/runtime_response.rb', line 53

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.



66
67
68
# File 'lib/openapi_first/runtime_response.rb', line 66

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.



41
42
43
# File 'lib/openapi_first/runtime_response.rb', line 41

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.



47
48
49
# File 'lib/openapi_first/runtime_response.rb', line 47

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.



28
29
30
# File 'lib/openapi_first/runtime_response.rb', line 28

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

#response_definitionDefinition::Response?

Returns the response definition associated with the response.

Returns:



88
89
90
# File 'lib/openapi_first/runtime_response.rb', line 88

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)


34
35
36
37
# File 'lib/openapi_first/runtime_response.rb', line 34

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

#validateFailure?

Deprecated.

Please use Definition#validate_response instead

Validates the response.

Returns:

  • (Failure, nil)

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



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

def validate
  warn '[DEPRECATION] `validate` is deprecated. ' \
       "Please use `OpenapiFirst.load('openapi.yaml').validate_response(rack_request, rack_response)` instead."
  @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.



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

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