Class: Interpol::RequestParamsParser

Inherits:
Object
  • Object
show all
Defined in:
lib/interpol/request_params_parser.rb

Overview

This class is designed to parse and validate a rails or sinatra style params hash based on the path_params/query_params declarations of an endpoint request definition.

The path_params and query_params declarations are merged together during validation, since both rails and sinatra give users a single params hash that contains the union of both kinds of params.

Note that the validation here takes some liberties; it supports ‘3’ as well as 3 for ‘integer’ params, for example, because it assumes that the values in a params hash are almost certainly going to be strings since that’s how rails and sinatra give them to you.

The parsed params object supports dot-syntax for accessing parameters and will convert values where feasible (e.g. ‘3’ = 3, ‘true’ => true, etc).

Defined Under Namespace

Classes: ParamConverter, ParamValidator

Instance Method Summary collapse

Constructor Details

#initialize(endpoint_definition, configuration) ⇒ RequestParamsParser

Returns a new instance of RequestParamsParser.



22
23
24
25
26
# File 'lib/interpol/request_params_parser.rb', line 22

def initialize(endpoint_definition, configuration)
  @validator = ParamValidator.new(endpoint_definition, configuration)
  @validator.validate_path_params_valid_for_route!
  @converter = ParamConverter.new(@validator.param_definitions, configuration)
end

Instance Method Details

#parse(params) ⇒ Object



28
29
30
31
# File 'lib/interpol/request_params_parser.rb', line 28

def parse(params)
  validate!(params)
  DynamicStruct.new(@converter.convert params)
end

#validate!(params) ⇒ Object



33
34
35
# File 'lib/interpol/request_params_parser.rb', line 33

def validate!(params)
  @validator.validate!(params)
end