Class: Jsapi::Controller::Parameters

Inherits:
Object
  • Object
show all
Includes:
Model::Nestable
Defined in:
lib/jsapi/controller/parameters.rb

Overview

Used to wrap request parameters.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Model::Nestable

#[], #attribute?, #attributes

Constructor Details

#initialize(params, operation, definitions, strong: false) ⇒ Parameters

Creates a new instance that wraps params according to operation. References are resolved to API components in definitions.

If strong is true+ parameters that can be mapped are accepted only. That means that the instance created is invalid if params contains any parameters that cannot be mapped to a parameter or a request body property of operation.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/jsapi/controller/parameters.rb', line 18

def initialize(params, operation, definitions, strong: false)
  @params = params
  @strong = strong == true
  @raw_attributes = {}

  # Merge parameters and request body properties
  meta_models = operation.parameters.transform_values do |parameter|
    parameter.resolve(definitions)
  end
  request_body = operation.request_body&.resolve(definitions)
  if request_body && request_body.schema.respond_to?(:properties)
    meta_models.merge!(
      request_body.schema.resolve_properties(:write, definitions)
    )
  end

  # Wrap params
  meta_models.each do |name, meta_model|
    @raw_attributes[name] =
      JSON.wrap(params[name], meta_model.schema, definitions)
  end
end

Instance Attribute Details

#raw_attributesObject (readonly)

Returns the value of attribute raw_attributes.



9
10
11
# File 'lib/jsapi/controller/parameters.rb', line 9

def raw_attributes
  @raw_attributes
end

Instance Method Details

#inspectObject

:nodoc:



41
42
43
44
# File 'lib/jsapi/controller/parameters.rb', line 41

def inspect # :nodoc:
  "#<#{self.class.name} " \
  "#{attributes.map { |k, v| "#{k}: #{v.inspect}" }.join(', ')}>"
end

#validate(errors) ⇒ Object

Validates the request parameters. Returns true if the parameters are valid, false otherwise. Detected errors are added to errors.



48
49
50
51
52
53
54
55
56
57
# File 'lib/jsapi/controller/parameters.rb', line 48

def validate(errors)
  [
    validate_attributes(errors),
    !@strong || validate_parameters(
      @params.except(:controller, :action),
      attributes,
      errors
    )
  ].all?
end