Class: OpenapiFirst::Request

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(path:, request_method:, operation_object:, parameters:, content_type:, content_schema:, required_body:) ⇒ Request

Returns a new instance of Request.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/openapi_first/request.rb', line 13

def initialize(path:, request_method:, operation_object:,
               parameters:, content_type:, content_schema:, required_body:)
  @path = path
  @request_method = request_method
  @content_type = content_type
  @content_schema = content_schema
  @operation = operation_object
  @request_parser = RequestParser.new(
    query_parameters: parameters.query,
    path_parameters: parameters.path,
    header_parameters: parameters.header,
    cookie_parameters: parameters.cookie,
    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.cookie_schema
  )
end

Instance Attribute Details

#content_schemaObject (readonly)

Returns the value of attribute content_schema.



37
38
39
# File 'lib/openapi_first/request.rb', line 37

def content_schema
  @content_schema
end

#content_typeObject (readonly)

Returns the value of attribute content_type.



37
38
39
# File 'lib/openapi_first/request.rb', line 37

def content_type
  @content_type
end

#operationObject (readonly)

Returns the value of attribute operation.



37
38
39
# File 'lib/openapi_first/request.rb', line 37

def operation
  @operation
end

#pathObject (readonly)

Returns the value of attribute path.



37
38
39
# File 'lib/openapi_first/request.rb', line 37

def path
  @path
end

#request_methodObject (readonly)

Returns the value of attribute request_method.



37
38
39
# File 'lib/openapi_first/request.rb', line 37

def request_method
  @request_method
end

Instance Method Details

#operation_idObject



49
50
51
# File 'lib/openapi_first/request.rb', line 49

def operation_id
  @operation['operationId']
end

#validate(request, route_params:) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/openapi_first/request.rb', line 39

def validate(request, route_params:)
  parsed_request = nil
  error = catch FAILURE do
    parsed_request = @request_parser.parse(request, route_params:)
    @validator.call(parsed_request)
    nil
  end
  ValidatedRequest.new(request, parsed_request:, error:, request_definition: self)
end