Module: WolfCore::LambdaFunctionOperations

Includes:
ExceptionOperations
Included in:
ApplicationService, Barton::Routing, Integrations::RoutingOperations
Defined in:
lib/wolf_core/infrastructure/lambda_function_operations.rb

Instance Method Summary collapse

Methods included from ExceptionOperations

#raise_service_error

Instance Method Details

#deep_parse_json(input) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/wolf_core/infrastructure/lambda_function_operations.rb', line 35

def deep_parse_json(input)
  while input.is_a?(String)
    begin
      input = JSON.parse(input)
    rescue JSON::ParserError
      break
    end
  end

  input&.with_indifferent_access || {}
end

#get_event_params(event) ⇒ Object



29
30
31
32
33
# File 'lib/wolf_core/infrastructure/lambda_function_operations.rb', line 29

def get_event_params(event)
  event_body = event['body']
  return deep_parse_json(event_body) if event_body.present?
  event.with_indifferent_access
end

#invoke_lambda(function_name:, payload:, invocation_type: nil) ⇒ Object



5
6
7
8
9
# File 'lib/wolf_core/infrastructure/lambda_function_operations.rb', line 5

def invoke_lambda(function_name:, payload:, invocation_type: nil)
  WolfCore::LambdaFunctionDataSource.invoke(
    function_name: function_name, payload: payload, invocation_type: invocation_type
  )
end

#parse_lambda_response(response) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/wolf_core/infrastructure/lambda_function_operations.rb', line 21

def parse_lambda_response(response)
  payload = JSON.parse(response.payload.string) rescue response.payload
  OpenStruct.new({
    status_code: response.status_code,
    payload: payload,
  })
end

#result_to_response(result) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/wolf_core/infrastructure/lambda_function_operations.rb', line 47

def result_to_response(result)
  if result.success?
    { statusCode: 200, body: result.data.to_h.to_json }
  else
    { statusCode: result.error.status || 422, body: result.error.to_h.to_json }
  end
end

#validate_lambda_response(response:, message:, error_data: nil) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/wolf_core/infrastructure/lambda_function_operations.rb', line 11

def validate_lambda_response(response:, message:, error_data: nil)
  unless response.status_code == 200
    error_data = {
      message: message,
      response: parse_lambda_response(response)
    }.merge(error_data || {})
    raise_service_error(error_data)
  end
end