Class: OpenapiParameters::Query

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

Overview

Query parses query parameters from a http query strings.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parameters, convert: true) ⇒ Query

Returns a new instance of Query.

Parameters:

  • parameters (Array<Hash>)

    The OpenAPI query parameter definitions.

  • convert (Boolean) (defaults to: true)

    Whether to convert the values to the correct type.



10
11
12
13
# File 'lib/openapi_parameters/query.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.



32
33
34
# File 'lib/openapi_parameters/query.rb', line 32

def parameters
  @parameters
end

Instance Method Details

#unpack(query_string) ⇒ Object

rubocop:disable Metrics/AbcSize



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/openapi_parameters/query.rb', line 15

def unpack(query_string) # rubocop:disable Metrics/AbcSize
  parsed_query = Rack::Utils.parse_query(query_string)
  parameters.each_with_object({}) do |parameter, result|
    if parameter.deep_object?
      parsed_nested_query = Rack::Utils.parse_nested_query(query_string)
      next unless parsed_nested_query.key?(parameter.name)

      result[parameter.name] = convert(parsed_nested_query[parameter.name], parameter)
    else
      next unless parsed_query.key?(parameter.name)

      value = Unpacker.unpack_value(parameter, parsed_query[parameter.name])
      result[parameter.name] = convert(value, parameter)
    end
  end
end