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.



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

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_parsers = RequestBodyParsers[content_type] if content_type
end

Instance Attribute Details

#cookiesObject (readonly)

Returns the value of attribute cookies.



25
26
27
# File 'lib/openapi_first/request_parser.rb', line 25

def cookies
  @cookies
end

#headersObject (readonly)

Returns the value of attribute headers.



25
26
27
# File 'lib/openapi_first/request_parser.rb', line 25

def headers
  @headers
end

#pathObject (readonly)

Returns the value of attribute path.



25
26
27
# File 'lib/openapi_first/request_parser.rb', line 25

def path
  @path
end

#queryObject (readonly)

Returns the value of attribute query.



25
26
27
# File 'lib/openapi_first/request_parser.rb', line 25

def query
  @query
end

Instance Method Details

#parse(request, route_params:) ⇒ Object



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

def parse(request, route_params:)
  ParsedRequest.new(
    path: @path_parser&.unpack(route_params),
    query: parse_query(request.env[Rack::QUERY_STRING]),
    headers: @headers_parser&.unpack_env(request.env),
    cookies: @cookies_parser&.unpack(request.env[Rack::HTTP_COOKIE]),
    body: @body_parsers&.call(request)
  )
end