Class: Lighthouse::DirectDeposit::ErrorParser

Inherits:
Object
  • Object
show all
Defined in:
lib/lighthouse/direct_deposit/error_parser.rb

Class Method Summary collapse

Class Method Details

.data_sourceObject



73
74
75
# File 'lib/lighthouse/direct_deposit/error_parser.rb', line 73

def self.data_source
  'Lighthouse Direct Deposit'
end

.parse(response) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/lighthouse/direct_deposit/error_parser.rb', line 8

def self.parse(response)
  body = parse_body(response[:body])
  detail = parse_detail(body)
  status = parse_status(response[:status], detail)

  errors = [
    {
      title: parse_title(body),
      detail:,
      code: parse_code(detail),
      source: data_source
    }
  ]

  Lighthouse::DirectDeposit::ErrorResponse.new(status, errors)
end

.parse_body(body) ⇒ Object



33
34
35
# File 'lib/lighthouse/direct_deposit/error_parser.rb', line 33

def self.parse_body(body)
  body.to_hash.with_indifferent_access
end

.parse_code(detail) ⇒ Object

rubocop:disable Metrics/MethodLength



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/lighthouse/direct_deposit/error_parser.rb', line 46

def self.parse_code(detail) # rubocop:disable Metrics/MethodLength
  prefix = 'direct.deposit'

  return "#{prefix}.api.rate.limit.exceeded" if detail.include? 'API rate limit exceeded'
  return "#{prefix}.api.gateway.timeout" if detail.include? 'Did not receive a timely response'
  return "#{prefix}.invalid.authentication.creds" if detail.include? 'Invalid authentication credentials'
  return "#{prefix}.invalid.token" if detail.include? 'Invalid token'
  return "#{prefix}.invalid.scopes" if detail.include? 'scopes are not configured'
  return "#{prefix}.icn.not.found" if detail.include? 'No data found for ICN'
  return "#{prefix}.icn.invalid" if detail.include? 'getDirectDeposit.icn size'
  return "#{prefix}.account.number.invalid" if detail.include? 'payment.accountNumber.invalid'
  return "#{prefix}.account.type.invalid" if detail.include? 'payment.accountType.invalid'
  return "#{prefix}.account.number.fraud" if detail.include? 'Flashes on record'
  return "#{prefix}.routing.number.invalid.checksum" if detail.include? 'accountRoutingNumber.invalidCheckSum'
  return "#{prefix}.routing.number.invalid" if detail.include? 'payment.accountRoutingNumber.invalid'
  return "#{prefix}.routing.number.fraud" if detail.include? 'Routing number related to potential fraud'
  return "#{prefix}.restriction.indicators.present" if detail.include? 'restriction.indicators.present'
  return "#{prefix}.day.phone.number.invalid" if detail.include? 'Day phone number is invalid'
  return "#{prefix}.day.area.number.invalid" if detail.include? 'Day area number is invalid'
  return "#{prefix}.night.phone.number.invalid" if detail.include? 'Night phone number is invalid'
  return "#{prefix}.night.area.number.invalid" if detail.include? 'Night area number is invalid'
  return "#{prefix}.mailing.address.invalid" if detail.include? 'field not entered for mailing address update'
  return "#{prefix}.potential.fraud" if %w[GUIE50022 GUIE50041].any? { |code| detail.include?(code) }

  "#{prefix}.generic.error"
end

.parse_detail(body) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/lighthouse/direct_deposit/error_parser.rb', line 37

def self.parse_detail(body)
  messages = []
  messages.push(body[:error_codes][0][:error_code]) if body[:error_codes].present?
  messages.push(body[:error_codes][0][:detail]) if body[:error_codes].present?
  messages.push(body[:error_description] || body[:error] || body[:detail] || body[:message] || 'Unknown error')

  messages.compact.join(': ')
end

.parse_status(status, _detail) ⇒ Object



25
26
27
# File 'lib/lighthouse/direct_deposit/error_parser.rb', line 25

def self.parse_status(status, _detail)
  status
end

.parse_title(body) ⇒ Object



29
30
31
# File 'lib/lighthouse/direct_deposit/error_parser.rb', line 29

def self.parse_title(body)
  body[:error] || body[:title] || status_message_from(body[:status]) || 'Unknown error'
end

.status_message_from(code) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/lighthouse/direct_deposit/error_parser.rb', line 77

def self.status_message_from(code)
  case code
  when 401
    'Not Authorized'
  when 403
    'Forbidden'
  when 413
    'Payload too large'
  when 429
    'Too many requests'
  end
end