Class: OpenapiFirst::Request
- Inherits:
-
Object
- Object
- OpenapiFirst::Request
- Defined in:
- lib/openapi_first/request.rb
Overview
Represents one request definition of an OpenAPI description. Note that this is not the same as an OpenAPI 3.x Operation. An 3.x Operation object can accept multiple requests, because it can handle multiple content-types. This class represents one of those requests.
Instance Attribute Summary collapse
-
#content_schema ⇒ Object
readonly
Returns the value of attribute content_schema.
-
#content_type ⇒ Object
readonly
Returns the value of attribute content_type.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#operation ⇒ Object
readonly
Returns the value of attribute operation.
-
#parameters ⇒ Object
readonly
Returns the value of attribute parameters.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#query_schema ⇒ Object
readonly
Returns the value of attribute query_schema.
-
#request_method ⇒ Object
readonly
Returns the value of attribute request_method.
Instance Method Summary collapse
- #allow_empty_content? ⇒ Boolean
-
#initialize(path:, request_method:, operation_object:, parameters:, content_type:, content_schema:, required_body:, key:) ⇒ Request
constructor
rubocop:disable Metrics/MethodLength.
- #operation_id ⇒ Object
- #validate(request, route_params:) ⇒ Object
Constructor Details
#initialize(path:, request_method:, operation_object:, parameters:, content_type:, content_schema:, required_body:, key:) ⇒ Request
rubocop:disable Metrics/MethodLength
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/openapi_first/request.rb', line 15 def initialize(path:, request_method:, operation_object:, # rubocop:disable Metrics/MethodLength parameters:, content_type:, content_schema:, required_body:, key:) @path = path @request_method = request_method @content_type = content_type @content_schema = content_schema @operation = operation_object @allow_empty_content = content_type.nil? || required_body == false @key = key @query_parser = parameters.query&.then { |params| OpenapiParameters::Query.new(params) } @path_parser = parameters.path&.then { |params| OpenapiParameters::Path.new(params) } @headers_parser = parameters.header&.then { |params| OpenapiParameters::Header.new(params) } = parameters.&.then { |params| OpenapiParameters::Cookie.new(params) } @body_parsers = RequestBodyParsers[content_type] if content_type @validator = RequestValidator.new( content_schema:, required_request_body: required_body == true, path_schema: parameters.path_schema, query_schema: parameters.query_schema, header_schema: parameters.header_schema, cookie_schema: parameters. ) @parameters = parameters end |
Instance Attribute Details
#content_schema ⇒ Object (readonly)
Returns the value of attribute content_schema.
40 41 42 |
# File 'lib/openapi_first/request.rb', line 40 def content_schema @content_schema end |
#content_type ⇒ Object (readonly)
Returns the value of attribute content_type.
40 41 42 |
# File 'lib/openapi_first/request.rb', line 40 def content_type @content_type end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
40 41 42 |
# File 'lib/openapi_first/request.rb', line 40 def key @key end |
#operation ⇒ Object (readonly)
Returns the value of attribute operation.
40 41 42 |
# File 'lib/openapi_first/request.rb', line 40 def operation @operation end |
#parameters ⇒ Object (readonly)
Returns the value of attribute parameters.
40 41 42 |
# File 'lib/openapi_first/request.rb', line 40 def parameters @parameters end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
40 41 42 |
# File 'lib/openapi_first/request.rb', line 40 def path @path end |
#query_schema ⇒ Object (readonly)
Returns the value of attribute query_schema.
40 41 42 |
# File 'lib/openapi_first/request.rb', line 40 def query_schema @query_schema end |
#request_method ⇒ Object (readonly)
Returns the value of attribute request_method.
40 41 42 |
# File 'lib/openapi_first/request.rb', line 40 def request_method @request_method end |
Instance Method Details
#allow_empty_content? ⇒ Boolean
43 44 45 |
# File 'lib/openapi_first/request.rb', line 43 def allow_empty_content? @allow_empty_content end |
#operation_id ⇒ Object
57 58 59 |
# File 'lib/openapi_first/request.rb', line 57 def operation_id @operation['operationId'] end |
#validate(request, route_params:) ⇒ Object
47 48 49 50 51 52 53 54 55 |
# File 'lib/openapi_first/request.rb', line 47 def validate(request, route_params:) parsed_request = nil error = catch FAILURE do parsed_request = parse_request(request, route_params:) @validator.call(parsed_request) nil end ValidatedRequest.new(request, parsed_request:, error:, request_definition: self, query_parser:) end |