Class: OpenapiFirst::RuntimeRequest

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/openapi_first/runtime_request.rb

Overview

RuntimeRequest represents how an incoming request (Rack::Request) matches a request definition.

Instance Method Summary collapse

Constructor Details

#initialize(request:, path_item:, operation:, path_params:) ⇒ RuntimeRequest

Returns a new instance of RuntimeRequest.



13
14
15
16
17
18
# File 'lib/openapi_first/runtime_request.rb', line 13

def initialize(request:, path_item:, operation:, path_params:)
  @request = request
  @path_item = path_item
  @operation = operation
  @original_path_params = path_params
end

Instance Method Details

#bodyObject Also known as: parsed_body



62
63
64
# File 'lib/openapi_first/runtime_request.rb', line 62

def body
  @body ||= BodyParser.new.parse(request, request.media_type)
end

#cookiesObject



57
58
59
60
# File 'lib/openapi_first/runtime_request.rb', line 57

def cookies
  @cookies ||=
    operation.cookie_parameters&.unpack(request.env) || {}
end

#headersObject



52
53
54
55
# File 'lib/openapi_first/runtime_request.rb', line 52

def headers
  @headers ||=
    operation.header_parameters&.unpack(request.env) || {}
end

#known?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/openapi_first/runtime_request.rb', line 23

def known?
  known_path? && known_request_method?
end

#known_path?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/openapi_first/runtime_request.rb', line 27

def known_path?
  !!path_item
end

#known_request_method?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/openapi_first/runtime_request.rb', line 31

def known_request_method?
  !!operation
end

#paramsObject

Merged path and query parameters



36
37
38
# File 'lib/openapi_first/runtime_request.rb', line 36

def params
  @params ||= query.merge(path_parameters)
end

#path_parametersObject



40
41
42
43
# File 'lib/openapi_first/runtime_request.rb', line 40

def path_parameters
  @path_parameters ||=
    operation.path_parameters&.unpack(@original_path_params) || {}
end

#queryObject Also known as: query_parameters



45
46
47
48
# File 'lib/openapi_first/runtime_request.rb', line 45

def query
  @query ||=
    operation.query_parameters&.unpack(request.env) || {}
end

#response(rack_response) ⇒ Object



76
77
78
# File 'lib/openapi_first/runtime_request.rb', line 76

def response(rack_response)
  RuntimeResponse.new(operation, rack_response)
end

#validateObject



67
68
69
# File 'lib/openapi_first/runtime_request.rb', line 67

def validate
  RequestValidation::Validator.new(operation).validate(self)
end

#validate!Object



71
72
73
74
# File 'lib/openapi_first/runtime_request.rb', line 71

def validate!
  error = validate
  error&.raise!
end