Class: OpenapiFirst::Definition
- Inherits:
-
Object
- Object
- OpenapiFirst::Definition
- Defined in:
- lib/openapi_first/definition.rb,
lib/openapi_first/definition/response.rb,
lib/openapi_first/definition/operation.rb,
lib/openapi_first/definition/path_item.rb,
lib/openapi_first/definition/responses.rb,
lib/openapi_first/definition/request_body.rb,
lib/openapi_first/definition/path_template.rb
Overview
Represents an OpenAPI API Description document This is returned by OpenapiFirst.load.
Defined Under Namespace
Classes: Operation, PathItem, RequestBody, Response
Instance Attribute Summary collapse
-
#filepath ⇒ Object
readonly
Returns the value of attribute filepath.
-
#openapi_version ⇒ Object
readonly
Returns the value of attribute openapi_version.
-
#paths ⇒ Object
readonly
Returns the value of attribute paths.
Instance Method Summary collapse
-
#initialize(resolved, filepath = nil) ⇒ Definition
constructor
A new instance of Definition.
-
#operations ⇒ Array<Operation>
Gets all the operations defined in the API description.
-
#path(pathname) ⇒ PathItem
Gets the PathItem object for the specified path.
-
#request(rack_request) ⇒ RuntimeRequest
Builds a RuntimeRequest object based on the Rack request.
-
#response(rack_request, rack_response) ⇒ RuntimeResponse
Builds a RuntimeResponse object based on the Rack request and response.
-
#validate_request(rack_request, raise_error: false) ⇒ RuntimeRequest
Validates the request against the API description.
-
#validate_response(rack_request, rack_response, raise_error: false) ⇒ RuntimeResponse
Validates the response against the API description.
Constructor Details
#initialize(resolved, filepath = nil) ⇒ Definition
Returns a new instance of Definition.
16 17 18 19 20 |
# File 'lib/openapi_first/definition.rb', line 16 def initialize(resolved, filepath = nil) @filepath = filepath @paths = resolved['paths'] @openapi_version = detect_version(resolved) end |
Instance Attribute Details
#filepath ⇒ Object (readonly)
Returns the value of attribute filepath.
12 13 14 |
# File 'lib/openapi_first/definition.rb', line 12 def filepath @filepath end |
#openapi_version ⇒ Object (readonly)
Returns the value of attribute openapi_version.
12 13 14 |
# File 'lib/openapi_first/definition.rb', line 12 def openapi_version @openapi_version end |
#paths ⇒ Object (readonly)
Returns the value of attribute paths.
12 13 14 |
# File 'lib/openapi_first/definition.rb', line 12 def paths @paths end |
Instance Method Details
#operations ⇒ Array<Operation>
Gets all the operations defined in the API description.
65 66 67 |
# File 'lib/openapi_first/definition.rb', line 65 def operations @operations ||= path_items.flat_map(&:operations) end |
#path(pathname) ⇒ PathItem
Gets the PathItem object for the specified path. Example:
definition.path('/pets/{id}')
74 75 76 77 78 |
# File 'lib/openapi_first/definition.rb', line 74 def path(pathname) return unless paths.key?(pathname) PathItem.new(pathname, paths[pathname], openapi_version:) end |
#request(rack_request) ⇒ RuntimeRequest
Builds a RuntimeRequest object based on the Rack request.
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/openapi_first/definition.rb', line 44 def request(rack_request) path_item, path_params = find_path_item_and_params(rack_request.path) operation = path_item&.operation(rack_request.request_method.downcase) RuntimeRequest.new( request: rack_request, path_item:, operation:, path_params: ) end |
#response(rack_request, rack_response) ⇒ RuntimeResponse
Builds a RuntimeResponse object based on the Rack request and response.
59 60 61 |
# File 'lib/openapi_first/definition.rb', line 59 def response(rack_request, rack_response) request(rack_request).response(rack_response) end |
#validate_request(rack_request, raise_error: false) ⇒ RuntimeRequest
Validates the request against the API description.
26 27 28 29 30 |
# File 'lib/openapi_first/definition.rb', line 26 def validate_request(rack_request, raise_error: false) validated = request(rack_request).tap(&:validate) validated.error&.raise! if raise_error validated end |
#validate_response(rack_request, rack_response, raise_error: false) ⇒ RuntimeResponse
Validates the response against the API description.
37 38 39 |
# File 'lib/openapi_first/definition.rb', line 37 def validate_response(rack_request, rack_response, raise_error: false) request(rack_request).validate_response(rack_response, raise_error:) end |