Class: Meta::Parameters

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/meta/application/parameters.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parameters) ⇒ Parameters

Returns a new instance of Parameters.



9
10
11
# File 'lib/meta/application/parameters.rb', line 9

def initialize(parameters)
  @parameters = parameters.dup
end

Instance Attribute Details

#parametersObject (readonly)

Returns the value of attribute parameters.



7
8
9
# File 'lib/meta/application/parameters.rb', line 7

def parameters
  @parameters
end

Instance Method Details

#filter(request) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/meta/application/parameters.rb', line 13

def filter(request)
  errors = {}
  final_value = {}

  parameters.each do |name, options|
    schema = options[:schema]
    value = if options[:in] == 'header'
              schema.filter(request.get_header('HTTP_' + name.to_s.upcase.gsub('-', '_')))
            else
              schema.filter(request.params[name.to_s])
            end
    final_value[name] = value
  rescue JsonSchema::ValidationError => e
    errors[name] = e.message
  end
  raise Errors::ParameterInvalid.new(errors) unless errors.empty?

  final_value
end

#merge(parameters) ⇒ Object



48
49
50
51
# File 'lib/meta/application/parameters.rb', line 48

def merge(parameters)
  parameters_hash = parameters.is_a?(Parameters) ? parameters.parameters : parameters
  Parameters.new(self.parameters.merge(parameters_hash))
end

#to_swagger_docObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/meta/application/parameters.rb', line 33

def to_swagger_doc
  parameters.map do |name, options|
    property_options = options[:schema].options
    {
      name: name,
      in: options[:in],
      required: property_options.key?(:required) ? property_options[:required] : false,
      description: property_options[:description] || '',
      schema: {
        type: property_options[:type]
      }.compact
    }.compact
  end unless parameters.empty?
end