Class: RequestHandler::QueryParser

Inherits:
SchemaParser show all
Defined in:
lib/request_handler/query_parser.rb

Constant Summary collapse

RESERVED_KEYS =
%w[fields filter include page sort].freeze

Instance Method Summary collapse

Constructor Details

#initialize(params:, schema:, schema_options: {}) ⇒ QueryParser

Returns a new instance of QueryParser.



9
10
11
12
13
# File 'lib/request_handler/query_parser.rb', line 9

def initialize(params:, schema:, schema_options: {})
  super(schema: schema, schema_options: schema_options)
  @query = params.dup
  RESERVED_KEYS.each { |key| query.delete(key) }
end

Instance Method Details

#runObject



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/request_handler/query_parser.rb', line 15

def run
  validate_schema(query)
rescue SchemaValidationError => e
  raise ExternalArgumentError, (e.errors.map do |schema_error|
    param = schema_error[:source][:pointer]
    { status: '400',
      code: "#{query[param] ? 'INVALID' : 'MISSING'}_QUERY_PARAMETER",
      detail: schema_error[:detail],
      source: { param: param } }
  end)
end