Class: Jsapi::Controller::Parameters
- Inherits:
-
Object
- Object
- Jsapi::Controller::Parameters
- Includes:
- Model::Nestable
- Defined in:
- lib/jsapi/controller/parameters.rb
Overview
Used to wrap request parameters.
Instance Attribute Summary collapse
-
#raw_additional_attributes ⇒ Object
readonly
Returns the value of attribute raw_additional_attributes.
-
#raw_attributes ⇒ Object
readonly
Returns the value of attribute raw_attributes.
Instance Method Summary collapse
-
#initialize(params, headers, operation, definitions, strong: false) ⇒ Parameters
constructor
Creates a new instance that wraps
paramsaccording tooperation. -
#validate(errors) ⇒ Object
Validates the request parameters.
Methods included from Model::Nestable
#[], #additional_attributes, #attribute?, #attributes, #inspect
Constructor Details
#initialize(params, headers, 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 can’t be mapped to a parameter or a request body property of operation.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/jsapi/controller/parameters.rb', line 17 def initialize(params, headers, operation, definitions, strong: false) @params = params.to_h @strong = strong == true @raw_attributes = {} @raw_additional_attributes = {} # Parameters operation.parameters.each do |name, parameter_model| parameter_model = parameter_model.resolve(definitions) @raw_attributes[name] = JSON.wrap( parameter_model.in == 'header' ? headers[name] : @params[name], parameter_model.schema.resolve(definitions), definitions, context: :request ) end # Request body request_body_schema = operation.request_body&.resolve(definitions) &.schema&.resolve(definitions) if request_body_schema&.object? request_body = JSON.wrap( @params.except(*operation.parameters.keys), request_body_schema, definitions, context: :request ) @raw_attributes.merge!(request_body.raw_attributes) @raw_additional_attributes = request_body.raw_additional_attributes end end |
Instance Attribute Details
#raw_additional_attributes ⇒ Object (readonly)
Returns the value of attribute raw_additional_attributes.
9 10 11 |
# File 'lib/jsapi/controller/parameters.rb', line 9 def raw_additional_attributes @raw_additional_attributes end |
#raw_attributes ⇒ Object (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
#validate(errors) ⇒ Object
Validates the request parameters. Returns true if the parameters are valid, false otherwise. Detected errors are added to errors.
52 53 54 55 56 57 |
# File 'lib/jsapi/controller/parameters.rb', line 52 def validate(errors) [ validate_attributes(errors), !@strong || validate_parameters(@params, attributes, errors) ].all? end |