Sha256: 39052aff7ac7850e3c3203c99bfad9656d608a6f5374d764e2f556d714cf8a45

Contents?: true

Size: 1002 Bytes

Versions: 4

Compression:

Stored size: 1002 Bytes

Contents

# frozen_string_literal: true

require 'rack'

module OpenapiParameters
  # Parses OpenAPI path parameters from a route params Hash that is usually provided by your Rack webframework
  class Path
    # @param parameters [Array<Hash>] The OpenAPI path parameters.
    # @param convert [Boolean] Whether to convert the values to the correct type.
    def initialize(parameters, convert: true)
      @parameters = parameters
      @convert = convert
    end

    attr_reader :parameters

    # @param path_params [Hash] The path parameters from the Rack request. The keys are strings.
    def unpack(path_params)
      parameters.each_with_object({}) do |param, result|
        parameter = Parameter.new(param)
        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
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
openapi_parameters-0.3.3 lib/openapi_parameters/path.rb
openapi_parameters-0.3.2 lib/openapi_parameters/path.rb
openapi_parameters-0.3.1 lib/openapi_parameters/path.rb
openapi_parameters-0.3.0 lib/openapi_parameters/path.rb