Class: OpenapiParameters::Header

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

Overview

Header parses OpenAPI parameters from the request headers.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parameters, convert: true) ⇒ Header

Returns a new instance of Header.

Parameters:

  • parameters (Array<Hash>)

    The OpenAPI parameters

  • convert (Boolean) (defaults to: true)

    Whether to convert the values to the correct type.



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

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

Instance Attribute Details

#parametersObject (readonly)

Returns the value of attribute parameters.



29
30
31
# File 'lib/openapi_parameters/header.rb', line 29

def parameters
  @parameters
end

Instance Method Details

#unpack(headers) ⇒ Object

Parameters:

  • headers (Hash)

    The headers from the request. Use HeadersHash to convert a Rack env to a Hash.



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

def unpack(headers)
  parameters.each_with_object({}) do |parameter, result|
    next unless headers.key?(parameter.name)

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

#unpack_env(env) ⇒ Object



25
26
27
# File 'lib/openapi_parameters/header.rb', line 25

def unpack_env(env)
  unpack(HeadersHash.new(env))
end