Class: OpenapiFirst::RequestParser

Inherits:
Object
  • Object
show all
Defined in:
lib/openapi_first/request_parser.rb

Overview

Parse a request

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query_parameters:, path_parameters:, header_parameters:, cookie_parameters:, content_type:) ⇒ RequestParser

Returns a new instance of RequestParser.



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/openapi_first/request_parser.rb', line 9

def initialize(
  query_parameters:,
  path_parameters:,
  header_parameters:,
  cookie_parameters:,
  content_type:
)
  @query_parser = OpenapiParameters::Query.new(query_parameters) if query_parameters
  @path_parser = OpenapiParameters::Path.new(path_parameters) if path_parameters
  @headers_parser = OpenapiParameters::Header.new(header_parameters) if header_parameters
  @cookies_parser = OpenapiParameters::Cookie.new(cookie_parameters) if cookie_parameters
  @body_parser = BodyParser.new(content_type) if content_type
end

Instance Attribute Details

#cookiesObject (readonly)

Returns the value of attribute cookies.



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

def cookies
  @cookies
end

#headersObject (readonly)

Returns the value of attribute headers.



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

def headers
  @headers
end

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

#queryObject (readonly)

Returns the value of attribute query.



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

def query
  @query
end

Instance Method Details

#parse(request, route_params:) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/openapi_first/request_parser.rb', line 25

def parse(request, route_params:)
  result = {}
  result[:path] = @path_parser.unpack(route_params) if @path_parser
  result[:query] = @query_parser.unpack(request.env[Rack::QUERY_STRING]) if @query_parser
  result[:headers] = @headers_parser.unpack_env(request.env) if @headers_parser
  result[:cookies] = @cookies_parser.unpack(request.env[Rack::HTTP_COOKIE]) if @cookies_parser
  result[:body] = @body_parser.parse(request) if @body_parser
  result
end