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.
74 75 76 |
# File 'lib/openapi_first/definition.rb', line 74 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}')
83 84 85 86 87 |
# File 'lib/openapi_first/definition.rb', line 83 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.
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/openapi_first/definition.rb', line 52 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.
67 68 69 70 |
# File 'lib/openapi_first/definition.rb', line 67 def response(rack_request, rack_response) runtime_request = request(rack_request) RuntimeResponse.new(runtime_request.operation, rack_response) end |
#validate_request(rack_request, raise_error: false) ⇒ RuntimeRequest
Validates the request against the API description.
26 27 28 29 30 31 32 33 |
# File 'lib/openapi_first/definition.rb', line 26 def validate_request(rack_request, raise_error: false) runtime_request = request(rack_request) validator = RequestValidation::Validator.new(runtime_request.operation) validation_error = validator.validate(runtime_request) validation_error.raise! if validation_error && raise_error runtime_request.error = validation_error runtime_request end |
#validate_response(rack_request, rack_response, raise_error: false) ⇒ RuntimeResponse
Validates the response against the API description.
40 41 42 43 44 45 46 47 |
# File 'lib/openapi_first/definition.rb', line 40 def validate_response(rack_request, rack_response, raise_error: false) runtime_response = response(rack_request, rack_response) validator = ResponseValidation::Validator.new(runtime_response.operation) validation_error = validator.validate(runtime_response) validation_error.raise! if validation_error && raise_error runtime_response.error = validation_error runtime_response end |