Class: Praxis::RequestStages::ValidateParamsAndHeaders

Inherits:
RequestStage show all
Defined in:
lib/praxis/request_stages/validate_params_and_headers.rb

Instance Attribute Summary collapse

Attributes inherited from Stage

#after_callbacks, #before_callbacks, #context, #name, #stages

Instance Method Summary collapse

Methods inherited from RequestStage

#action, #controller, #execute_controller_callbacks, #execute_with_around, #request, #run, #setup!, #validation_handler

Methods inherited from Stage

#after, #application, #before, #callback_args, #execute_callbacks, #run, #setup!, #setup_deferred_callbacks!

Constructor Details

#initialize(name, context, parent:) ⇒ ValidateParamsAndHeaders

Returns a new instance of ValidateParamsAndHeaders.



8
9
10
11
# File 'lib/praxis/request_stages/validate_params_and_headers.rb', line 8

def initialize(name, context, parent:)
  super
  @parent = parent
end

Instance Attribute Details

#parentObject (readonly)

Returns the value of attribute parent.



6
7
8
# File 'lib/praxis/request_stages/validate_params_and_headers.rb', line 6

def parent
  @parent
end

Instance Method Details

#executeObject



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
49
50
51
52
53
# File 'lib/praxis/request_stages/validate_params_and_headers.rb', line 17

def execute
  begin
    request.load_headers(CONTEXT_FOR[:headers])
  rescue StandardError => e
    message = 'Error loading headers.'
    return validation_handler.handle!(
      exception: e,
      summary: message,
      request: request,
      stage: name
    )
  end

  begin
    request.load_params(CONTEXT_FOR[:params])
  rescue Attributor::AttributorException => e
    message = 'Error loading params.'
    return validation_handler.handle!(
      exception: e,
      summary: message,
      request: request,
      stage: name
    )
  end

  errors = request.validate_headers(CONTEXT_FOR[:headers])
  errors += request.validate_params(CONTEXT_FOR[:params])
  return unless errors.any?

  message = 'Error validating request data.'
  validation_handler.handle!(
    summary: message,
    errors: errors,
    request: request,
    stage: name
  )
end

#pathObject



13
14
15
# File 'lib/praxis/request_stages/validate_params_and_headers.rb', line 13

def path
  @path ||= (@parent.path + [name])
end