Class: OpenapiParameters::Path

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

Overview

Parses OpenAPI path parameters from a route params Hash that is usually provided by your Rack webframework

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parameters, convert: true) ⇒ Path

Returns a new instance of Path.

Parameters:

  • parameters (Array<Hash>)

    The OpenAPI path parameters.

  • convert (Boolean) (defaults to: true)

    Whether to convert the values to the correct type.



10
11
12
13
# File 'lib/openapi_parameters/path.rb', line 10

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.



15
16
17
# File 'lib/openapi_parameters/path.rb', line 15

def parameters
  @parameters
end

Instance Method Details

#unpack(path_params) ⇒ Object

Parameters:

  • path_params (Hash)

    The path parameters from the Rack request. The keys are strings.



18
19
20
21
22
23
24
25
26
27
# File 'lib/openapi_parameters/path.rb', line 18

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

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