Class: Mountapi::Route::Parameters
- Inherits:
-
Object
- Object
- Mountapi::Route::Parameters
- Defined in:
- lib/mountapi/route/parameters.rb
Overview
The route parameters collection Top level access to parameters validation and casting
Class Method Summary collapse
Instance Method Summary collapse
-
#add(*args) ⇒ Object
buil and add param to collection.
- #by_location ⇒ Object
-
#cast(inbound_params) ⇒ Object
Cast parameter against schema and symbolize keys.
-
#filter(request_params) ⇒ Hash
Reject request params that does not belongs to specification Return merge all params from location to root level.
-
#initialize(parameters = []) ⇒ Parameters
constructor
A new instance of Parameters.
-
#validation_schema ⇒ Hash
A json-schema for parameters validation.
Constructor Details
#initialize(parameters = []) ⇒ Parameters
Returns a new instance of Parameters.
21 22 23 |
# File 'lib/mountapi/route/parameters.rb', line 21 def initialize(parameters = []) @parameters = parameters end |
Class Method Details
.build(parameters = []) ⇒ Mountapi::Route::Parameters
builder
13 14 15 16 17 18 19 |
# File 'lib/mountapi/route/parameters.rb', line 13 def self.build(parameters = []) return parameters if parameters.is_a?(self) parameters.inject(new) do |params, param| params.add(*param) end end |
Instance Method Details
#add(*args) ⇒ Object
buil and add param to collection
26 27 28 29 |
# File 'lib/mountapi/route/parameters.rb', line 26 def add(*args) @parameters = @parameters << Parameter.build(*args) self end |
#by_location ⇒ Object
72 73 74 |
# File 'lib/mountapi/route/parameters.rb', line 72 def by_location @parameters.group_by(&:location) end |
#cast(inbound_params) ⇒ Object
Cast parameter against schema and symbolize keys
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/mountapi/route/parameters.rb', line 34 def cast(inbound_params) @parameters.inject({}) do |acc, param| if inbound_params.key?(param.name) acc.merge(param.name.to_sym => param.cast(inbound_params[param.name])) elsif param.default acc.merge(param.name => param.default) else acc end end end |
#filter(request_params) ⇒ Hash
Reject request params that does not belongs to specification Return merge all params from location to root level
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/mountapi/route/parameters.rb', line 51 def filter(request_params) by_location.inject({}) do |hash, (location, params)| request_location = request_params.fetch(location.to_sym, {}) hash.merge(params.each_with_object({}) do |param, hsh| key, val = request_location.find { |k, _v| k.casecmp(param.name).zero? } # Avoid skipping nil param if param key is present and param is nullable hsh[param.name] = val if val || (key && val.nil? && param.json_schema["nullable"]) end) end end |
#validation_schema ⇒ Hash
Returns a json-schema for parameters validation.
64 65 66 67 68 69 70 |
# File 'lib/mountapi/route/parameters.rb', line 64 def validation_schema @validation_schema ||= { "type" => "object", "required" => required_params, "properties" => params_schemas } end |