Class: LightServiceExt::RecordActions

Inherits:
Object
  • Object
show all
Extended by:
WithErrorHandler
Defined in:
lib/light-service-ext/record_actions.rb

Class Method Summary collapse

Methods included from WithErrorHandler

with_error_handler

Class Method Details

.call(context) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/light-service-ext/record_actions.rb', line 7

def self.call(context)
  with_error_handler(ctx: context) do
    result = yield || context
    return context if outcomes_complete?(ctx: context, result: result)

    invoked_action = result.invoked_action
    return context if invoked_action.nil?

    context.add_to_successful_actions(invoked_action.name)

    merge_api_responses!(ctx: context, result: result)
    context
  end
end

.merge_api_responses!(ctx:, result:) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/light-service-ext/record_actions.rb', line 23

def merge_api_responses!(ctx:, result:)
  api_response = result.current_api_response
  return if api_response.blank?

  ctx.add_to_api_responses(api_response)
  nil
end

.outcomes_complete?(ctx:, result:) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/light-service-ext/record_actions.rb', line 31

def outcomes_complete?(ctx:, result:)
  if result.status == Status::COMPLETE
    ctx.add_status(result.status)

    if ctx.errors.present?
      ctx.add_last_failed_context(result.to_h)
      ctx.fail!
    end
    true
  else
    false
  end
end