Class: OpenapiFirst::Definition::Operation
- Inherits:
-
Object
- Object
- OpenapiFirst::Definition::Operation
- Extended by:
- Forwardable
- Defined in:
- lib/openapi_first/definition/operation.rb
Overview
Represents an operation object in the OpenAPI 3.X specification. Use this class to access information about the operation. Use ‘#[key]` to read the raw data. When using the middleware you can access the operation object via `env.operation`.
Instance Attribute Summary collapse
-
#method ⇒ String
(also: #request_method)
readonly
Returns the (downcased) request method of the operation.
-
#openapi_version ⇒ Object
readonly
:nodoc:.
-
#path ⇒ String
readonly
Returns the path of the operation as in the API description.
Instance Method Summary collapse
-
#cookie_parameters ⇒ Array<Hash>
Returns the cookie parameters of the operation.
-
#header_parameters ⇒ Array<Hash>
Returns the header parameters of the operation.
-
#initialize(path, request_method, path_item_object, openapi_version:) ⇒ Operation
constructor
A new instance of Operation.
-
#operation_id ⇒ String?
Returns the operation ID as defined in the API description.
-
#path_parameters ⇒ Array<Hash>
Returns the path parameters of the operation.
-
#query_parameters ⇒ Array<Hash>
Returns the query parameters of the operation.
-
#read? ⇒ Boolean
Checks if the operation is a read operation.
-
#request_body ⇒ RequestBody?
Returns the request body definition if defined in the API description.
-
#response_for(status, content_type) ⇒ Response?
Returns the response object for a given status.
-
#response_status_defined?(status) ⇒ Boolean
Checks if a response status is defined for this operation.
-
#schema_for(content_type) ⇒ Schema?
Returns the schema for a given content type.
-
#write? ⇒ Boolean
Checks if the operation is a write operation.
Constructor Details
#initialize(path, request_method, path_item_object, openapi_version:) ⇒ Operation
Returns a new instance of Operation.
23 24 25 26 27 28 29 |
# File 'lib/openapi_first/definition/operation.rb', line 23 def initialize(path, request_method, path_item_object, openapi_version:) @path = path @method = request_method @path_item_object = path_item_object @openapi_version = openapi_version @operation_object = @path_item_object[request_method] end |
Instance Attribute Details
#method ⇒ String (readonly) Also known as: request_method
Returns the (downcased) request method of the operation. Example: “get”
38 39 40 |
# File 'lib/openapi_first/definition/operation.rb', line 38 def method @method end |
#openapi_version ⇒ Object (readonly)
:nodoc:
41 42 43 |
# File 'lib/openapi_first/definition/operation.rb', line 41 def openapi_version @openapi_version end |
#path ⇒ String (readonly)
Returns the path of the operation as in the API description.
33 34 35 |
# File 'lib/openapi_first/definition/operation.rb', line 33 def path @path end |
Instance Method Details
#cookie_parameters ⇒ Array<Hash>
Returns the cookie parameters of the operation. Returns parameters defined on the path and in the operation.
126 127 128 |
# File 'lib/openapi_first/definition/operation.rb', line 126 def all_parameters['cookie'] end |
#header_parameters ⇒ Array<Hash>
Returns the header parameters of the operation. Returns parameters defined on the path and in the operation.
119 120 121 |
# File 'lib/openapi_first/definition/operation.rb', line 119 def header_parameters all_parameters['header'] end |
#operation_id ⇒ String?
Returns the operation ID as defined in the API description.
45 46 47 |
# File 'lib/openapi_first/definition/operation.rb', line 45 def operation_id operation_object['operationId'] end |
#path_parameters ⇒ Array<Hash>
Returns the path parameters of the operation.
105 106 107 |
# File 'lib/openapi_first/definition/operation.rb', line 105 def path_parameters all_parameters['path'] end |
#query_parameters ⇒ Array<Hash>
Returns the query parameters of the operation. Returns parameters defined on the path and in the operation.
112 113 114 |
# File 'lib/openapi_first/definition/operation.rb', line 112 def query_parameters all_parameters['query'] end |
#read? ⇒ Boolean
Checks if the operation is a read operation. This is the case for all request methods except POST, PUT, PATCH and DELETE.
52 53 54 |
# File 'lib/openapi_first/definition/operation.rb', line 52 def read? !write? end |
#request_body ⇒ RequestBody?
Returns the request body definition if defined in the API description.
65 66 67 |
# File 'lib/openapi_first/definition/operation.rb', line 65 def request_body @request_body ||= RequestBody.new(operation_object['requestBody'], self) if operation_object['requestBody'] end |
#response_for(status, content_type) ⇒ Response?
Returns the response object for a given status.
80 81 82 |
# File 'lib/openapi_first/definition/operation.rb', line 80 def response_for(status, content_type) responses.response_for(status, content_type) end |
#response_status_defined?(status) ⇒ Boolean
Checks if a response status is defined for this operation.
72 73 74 |
# File 'lib/openapi_first/definition/operation.rb', line 72 def response_status_defined?(status) responses.status_defined?(status) end |
#schema_for(content_type) ⇒ Schema?
Returns the schema for a given content type.
87 88 89 90 91 92 93 94 95 |
# File 'lib/openapi_first/definition/operation.rb', line 87 def schema_for(content_type) content = @request_body_object['content'] return unless content&.any? content_schemas&.fetch(content_type) do type = content_type.split(';')[0] content_schemas[type] || content_schemas["#{type.split('/')[0]}/*"] || content_schemas['*/*'] end end |
#write? ⇒ Boolean
Checks if the operation is a write operation. This is the case for POST, PUT, PATCH and DELETE request methods.
59 60 61 |
# File 'lib/openapi_first/definition/operation.rb', line 59 def write? WRITE_METHODS.include?(method) end |