Class: OpenapiParameters::Cookie

Inherits:
Object
  • Object
show all
Defined in:
lib/openapi_parameters/cookie.rb

Overview

Cookie parses OpenAPI cookie parameters from a cookie string.

Instance Method Summary collapse

Constructor Details

#initialize(parameters, convert: true) ⇒ Cookie

Returns a new instance of Cookie.

Parameters:

  • parameters (Array<Hash>)

    The OpenAPI parameter definitions.

  • convert (Boolean) (defaults to: true)

    Whether to convert the values to the correct type.



8
9
10
11
# File 'lib/openapi_parameters/cookie.rb', line 8

def initialize(parameters, convert: true)
  @parameters = parameters.map { Parameter.new(_1) }
  @convert = convert
end

Instance Method Details

#unpack(cookie_string) ⇒ Object

Parameters:

  • cookie_string (String)

    The cookie string from the request. Example “foo=bar; baz=qux”



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/openapi_parameters/cookie.rb', line 14

def unpack(cookie_string)
  cookies = Rack::Utils.parse_cookies_header(cookie_string)
  parameters.each_with_object({}) do |parameter, result|
    next unless cookies.key?(parameter.name)

    result[parameter.name] = catch :skip do
      value = Unpacker.unpack_value(parameter, cookies[parameter.name])
      @convert ? Converter.convert(value, parameter.schema) : value
    end
  end
end