Module: Ibandit::LocalDetailsCleaner

Defined in:
lib/ibandit/local_details_cleaner.rb

Class Method Summary collapse

Class Method Details

.can_clean?(country_code, local_details) ⇒ Boolean

Helpers #

Returns:

  • (Boolean)


22
23
24
25
# File 'lib/ibandit/local_details_cleaner.rb', line 22

def self.can_clean?(country_code, local_details)
  Constants::SUPPORTED_COUNTRY_CODES.include?(country_code) &&
    fields_for?(country_code, local_details)
end

.clean(local_details) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/ibandit/local_details_cleaner.rb', line 5

def self.clean(local_details)
  country_code = local_details[:country_code]
  if can_clean?(country_code, local_details)
    local_details = local_details.merge(
      public_send(:"clean_#{country_code.downcase}_details", local_details),
    )
  end

  return local_details if explicit_swift_details?(local_details)

  swift_details_for(local_details).merge(local_details)
end

.clean_at_details(local_details) ⇒ Object

Country-specific logic #



59
60
61
62
63
64
65
66
67
68
# File 'lib/ibandit/local_details_cleaner.rb', line 59

def self.clean_at_details(local_details)
  # Account number may be 4-11 digits long.
  # Add leading zeros to account number if < 11 digits.
  return {} unless local_details[:account_number].length >= 4

  {
    bank_code: local_details[:bank_code],
    account_number: local_details[:account_number].rjust(11, "0"),
  }
end

.clean_au_details(local_details) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/ibandit/local_details_cleaner.rb', line 70

def self.clean_au_details(local_details)
  # Account number may be up to 10 digits long.
  #
  # Minimum account_number length is 5
  return {} unless local_details[:account_number].length >= 5

  {
    branch_code: local_details[:branch_code].delete("-"),
    account_number: local_details[:account_number],
  }
end

.clean_be_details(local_details) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/ibandit/local_details_cleaner.rb', line 82

def self.clean_be_details(local_details)
   = local_details[:account_number].tr("-", "")

  {
    bank_code: local_details[:bank_code] || .slice(0, 3),
    account_number: ,
  }
end

.clean_bg_details(local_details) ⇒ Object



120
121
122
123
# File 'lib/ibandit/local_details_cleaner.rb', line 120

def self.clean_bg_details(local_details)
  # Bulgarian national bank details were replaced with IBANs in 2006.
  local_details
end

.clean_ca_details(local_details) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/ibandit/local_details_cleaner.rb', line 91

def self.clean_ca_details(local_details)
   = local_details[:account_number].tr("-", "")

  return {} unless (7..12).cover?(.length)

  bank_code = if local_details[:bank_code].length == 3
                local_details[:bank_code].rjust(4, "0")
              else
                local_details[:bank_code]
              end

  {
    account_number: ,
    bank_code: bank_code,
  }
end

.clean_cy_details(local_details) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/ibandit/local_details_cleaner.rb', line 125

def self.clean_cy_details(local_details)
  # Account number may be 7-16 digits long.
  # Add leading zeros to account number if < 16 digits.
  cleaned_bank_code = local_details[:bank_code].gsub(/[-\s]/, "")

  bank_code      = cleaned_bank_code.slice(0, 3)
  branch_code    =
    if local_details[:branch_code]
      local_details[:branch_code]
    elsif cleaned_bank_code.length > 3
      cleaned_bank_code[3..-1]
    end
   =
    if local_details[:account_number].length >= 7
      local_details[:account_number].rjust(16, "0")
    else
      local_details[:account_number]
    end

  {
    bank_code: bank_code,
    branch_code: branch_code,
    account_number: ,
  }
end

.clean_cz_details(local_details) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/ibandit/local_details_cleaner.rb', line 151

def self.clean_cz_details(local_details)
  #   The SWIFT definition of a Czech IBAN includes both the account
  #   number prefix and the account number. This method therefore supports
  #   passing those fields concatenated.
   =
    if local_details.include?(:account_number_prefix)
      [
        local_details[:account_number_prefix].rjust(6, "0"),
        local_details[:account_number].rjust(10, "0"),
      ].join
    else
      local_details[:account_number].tr("-", "").rjust(16, "0")
    end

  {
    bank_code: local_details[:bank_code],
    account_number: ,
  }
end

.clean_de_details(local_details) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/ibandit/local_details_cleaner.rb', line 171

def self.clean_de_details(local_details)
  # Account number may be up to 10 digits long.
  # Add leading zeros to account number if < 10 digits.
  #
  # There are many exceptions to the way German bank details translate
  # into an IBAN, detailed into a 200 page document compiled by the
  # Bundesbank, and handled by the GermanDetailsConverter class.
  converted_details =
    begin
      GermanDetailsConverter.convert(local_details)
    rescue UnsupportedAccountDetails
      local_details.dup
    end

  return {} unless converted_details[:account_number].length >= 4

  {
    bank_code: converted_details[:bank_code],
    account_number: converted_details[:account_number].rjust(10, "0"),
  }
end

.clean_dk_details(local_details) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/ibandit/local_details_cleaner.rb', line 193

def self.clean_dk_details(local_details)
  # This method supports being passed the component IBAN parts, as defined
  # by SWIFT, or a single traditional-format string split by a '-'.
  if local_details[:bank_code]
    bank_code      = local_details[:bank_code].rjust(4, "0")
     = local_details[:account_number].rjust(10, "0")
  elsif local_details[:account_number].include?("-")
    bank_code,  = local_details[:account_number].split("-", 2)
  elsif local_details[:account_number].gsub(/\s/, "").length == 14
     = local_details[:account_number].gsub(/\s/, "")
    bank_code = .slice(0, 4)
     = .slice(4, 10)
  else
    return {}
  end

  {
    bank_code: bank_code.rjust(4, "0"),
    account_number: .delete("-").rjust(10, "0"),
  }
end

.clean_ee_details(local_details) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/ibandit/local_details_cleaner.rb', line 215

def self.clean_ee_details(local_details)
  # Account number may be up to 14 characters long.
  # Add leading zeros to account number if < 14 digits.
  #
  # Bank code can be found by extracted from the first two digits of the
  # account number and converted using the rules at
  # http://www.pangaliit.ee/en/settlements-and-standards/bank-codes-of-estonian-banks
  domestic_bank_code =
    local_details[:account_number].gsub(/\A0+/, "").slice(0, 2)

  iban_bank_code =
    case domestic_bank_code
    when "11" then "22"
    when "93" then "00"
    else domestic_bank_code
    end

   = local_details[:account_number].rjust(14, "0")

  { bank_code: iban_bank_code, account_number:  }
end

.clean_es_details(local_details) ⇒ Object



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/ibandit/local_details_cleaner.rb', line 237

def self.clean_es_details(local_details)
  # This method supports being passed the component IBAN parts, as defined
  # by SWIFT, or a single 20 digit string.
  if local_details[:bank_code] && local_details[:branch_code]
    bank_code      = local_details[:bank_code]
    branch_code    = local_details[:branch_code]
     = local_details[:account_number].gsub(/[-\s]/, "")
  else
     =
      local_details[:account_number].gsub(/[-\s]/, "")

    bank_code      = .slice(0, 4)
    branch_code    = .slice(4, 4)
     = [8..-1]
  end

  {
    bank_code: bank_code,
    branch_code: branch_code,
    account_number: ,
  }
end

.clean_fi_details(local_details) ⇒ Object



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/ibandit/local_details_cleaner.rb', line 260

def self.clean_fi_details(local_details)
  #   Finnish account numbers need to be expanded into "electronic format"
  #   by adding zero-padding. The expansion method depends on the first
  #   character of the bank code.
   =
    if %w[4 5 6].include?(local_details[:bank_code][0])
      [
        local_details[:account_number][0],
        local_details[:account_number][1..-1].rjust(7, "0"),
      ].join
    else
      local_details[:account_number].rjust(8, "0")
    end

  {
    bank_code: local_details[:bank_code],
    account_number: ,
  }
end

.clean_fr_details(local_details) ⇒ Object



280
281
282
283
284
285
286
# File 'lib/ibandit/local_details_cleaner.rb', line 280

def self.clean_fr_details(local_details)
  {
    bank_code: local_details[:bank_code],
    branch_code: local_details[:branch_code],
    account_number: local_details[:account_number].gsub(/[-\s]/, ""),
  }
end

.clean_gb_details(local_details) ⇒ Object



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/ibandit/local_details_cleaner.rb', line 288

def self.clean_gb_details(local_details)
  # Account number may be 6-8 digits
  # Add leading zeros to account number if < 8 digits.
  branch_code = local_details[:branch_code].gsub(/[-\s]/, "")

  if local_details[:bank_code]
    bank_code = local_details[:bank_code]
  else
    bic = Ibandit.find_bic("GB", branch_code)
    bank_code = bic.nil? ? nil : bic.slice(0, 4)
  end

   = local_details[:account_number].gsub(/[-\s]/, "")
   = .rjust(8, "0") if .length > 5

  {
    bank_code: bank_code,
    branch_code: branch_code,
    account_number: ,
  }
end

.clean_gr_details(local_details) ⇒ Object



310
311
312
313
314
# File 'lib/ibandit/local_details_cleaner.rb', line 310

def self.clean_gr_details(local_details)
  # Greek IBANs construction is idiosyncratic to the individual banks, and
  # no central specification is published.
  local_details
end

.clean_hr_details(local_details) ⇒ Object



316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/ibandit/local_details_cleaner.rb', line 316

def self.clean_hr_details(local_details)
  # This method supports being passed the component IBAN parts, as defined
  # by SWIFT, or a single traditional-format string split by a '-'.
  return local_details if local_details[:bank_code]
  return local_details unless local_details[:account_number].include?("-")

  bank_code,  = local_details[:account_number].split("-", 2)

  {
    bank_code: bank_code,
    account_number: ,
  }
end

.clean_hu_details(local_details) ⇒ Object



330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/ibandit/local_details_cleaner.rb', line 330

def self.clean_hu_details(local_details)
  # This method supports being passed the component IBAN parts, as defined
  # by SWIFT, or a single 16 or 24 digit string.
  return local_details if local_details[:bank_code] || local_details[:branch_code]

  cleaned_acct_number = local_details[:account_number].gsub(/[-\s]/, "")

  case cleaned_acct_number.length
  when 16
    {
      bank_code: cleaned_acct_number.slice(0, 3),
      branch_code: cleaned_acct_number.slice(3, 4),
      account_number: cleaned_acct_number.slice(7, 9).ljust(17, "0"),
    }
  when 24
    {
      bank_code: cleaned_acct_number.slice(0, 3),
      branch_code: cleaned_acct_number.slice(3, 4),
      account_number: cleaned_acct_number.slice(7, 17),
    }
  else local_details
  end
end

.clean_ie_details(local_details) ⇒ Object



354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'lib/ibandit/local_details_cleaner.rb', line 354

def self.clean_ie_details(local_details)
  # Ireland uses the same local details as the United Kingdom
  branch_code = local_details[:branch_code].gsub(/[-\s]/, "")

  if local_details[:bank_code]
    bank_code = local_details[:bank_code]
  else
    bic = Ibandit.find_bic("IE", branch_code)
    bank_code = bic.nil? ? nil : bic.slice(0, 4)
  end

   = local_details[:account_number].gsub(/[-\s]/, "")
   = .rjust(8, "0") if .length > 5

  {
    bank_code: bank_code,
    branch_code: branch_code,
    account_number: ,
  }
end

.clean_is_details(local_details) ⇒ Object



375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# File 'lib/ibandit/local_details_cleaner.rb', line 375

def self.clean_is_details(local_details)
  if local_details[:bank_code]
    bank_code = local_details[:bank_code]
    parts = local_details[:account_number].split("-")
  elsif local_details[:account_number].include?("-")
    bank_code, *parts = local_details[:account_number].split("-")
  else
    bank_code = local_details[:account_number].slice(0, 4)
    parts = Array(local_details[:account_number][4..-1])
  end

  {
    bank_code: bank_code.rjust(4, "0"),
    account_number: (parts),
  }
end

.clean_it_details(local_details) ⇒ Object



392
393
394
395
396
397
398
399
# File 'lib/ibandit/local_details_cleaner.rb', line 392

def self.clean_it_details(local_details)
  # Add leading zeros to account number if < 12 digits.
  {
    bank_code: local_details[:bank_code],
    branch_code: local_details[:branch_code],
    account_number: local_details[:account_number].rjust(12, "0"),
  }
end

.clean_lt_details(local_details) ⇒ Object



401
402
403
404
# File 'lib/ibandit/local_details_cleaner.rb', line 401

def self.clean_lt_details(local_details)
  # Lithuanian national bank details were replaced with IBANs in 2004.
  local_details
end

.clean_lu_details(local_details) ⇒ Object



406
407
408
409
# File 'lib/ibandit/local_details_cleaner.rb', line 406

def self.clean_lu_details(local_details)
  # Luxembourgian national bank details were replaced with IBANs in 2002.
  local_details
end

.clean_lv_details(local_details) ⇒ Object



411
412
413
414
# File 'lib/ibandit/local_details_cleaner.rb', line 411

def self.clean_lv_details(local_details)
  # Latvian national bank details were replaced with IBANs in 2004.
  local_details
end

.clean_mc_details(local_details) ⇒ Object



416
417
418
419
# File 'lib/ibandit/local_details_cleaner.rb', line 416

def self.clean_mc_details(local_details)
  # Monaco uses the same local details method as France
  clean_fr_details(local_details)
end

.clean_mt_details(local_details) ⇒ Object



421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# File 'lib/ibandit/local_details_cleaner.rb', line 421

def self.clean_mt_details(local_details)
  # Add leading zeros to account number if < 18 digits.
  branch_code = local_details[:branch_code]

  if local_details[:bank_code]
    bank_code = local_details[:bank_code]
  else
    bic = Ibandit.find_bic("MT", branch_code)
    bank_code = bic.nil? ? nil : bic.slice(0, 4)
  end

   = local_details[:account_number].gsub(/[-\s]/, "")
   = .rjust(18, "0")

  {
    bank_code: bank_code,
    branch_code: branch_code,
    account_number: ,
  }
end

.clean_nl_details(local_details) ⇒ Object



442
443
444
445
446
447
448
# File 'lib/ibandit/local_details_cleaner.rb', line 442

def self.clean_nl_details(local_details)
  # Add leading zeros to account number if < 10 digits.
  {
    bank_code: local_details[:bank_code],
    account_number: local_details[:account_number].rjust(10, "0"),
  }
end

.clean_no_details(local_details) ⇒ Object



450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
# File 'lib/ibandit/local_details_cleaner.rb', line 450

def self.clean_no_details(local_details)
  # This method supports being passed the component IBAN parts, as defined
  # by SWIFT, or a single 11 digit string.
  if local_details[:bank_code]
    bank_code      = local_details[:bank_code]
     = local_details[:account_number]
  else
    cleaned_acct_number = local_details[:account_number].gsub(/[-.\s]/, "")

    bank_code      = cleaned_acct_number.slice(0, 4)
     = cleaned_acct_number[4..-1]
  end

  {
    bank_code: bank_code,
    account_number: ,
  }
end

.clean_nz_details(local_details) ⇒ Object



469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
# File 'lib/ibandit/local_details_cleaner.rb', line 469

def self.clean_nz_details(local_details)
  # This method supports being passed the component parts of the NZ account
  # number, or a single 15/16 digit string (BBbbbb-AAAAAAA-0SS) in the
  # account number field.
  #
  # When given a 15/16 digit string, the component parts (i.e bank_code,
  # branch_code and account_number) are extracted, with the 7-digit account
  # number body and 2/3-digit account number suffix making up the actual
  # account_number field.
  if local_details[:bank_code] && local_details[:branch_code]
    bank_code = local_details[:bank_code]
    branch_code = local_details[:branch_code]
     = local_details[:account_number].tr("-", "")
  else
     = local_details[:account_number].tr("-", "")
    bank_code = .slice(0, 2)
    branch_code = .slice(2, 4)
     = [6..-1]
  end

  if  && .length == 9
    # > Some banks (such as BNZ) include three digits of the suffix in their
    # > presentation of the account number to the end customer. Other banks
    # > only show the last two digits of the suffix to the end customer.
    # > Technically, all banks have three digit suffixes, it's just that the
    # > first digit of the suffix is always 0 so it's usually ignored.
    # > [https://en.wikipedia.org/wiki/New_Zealand_bank_account_number]
    #
    # Here, we insert the zero in cases where it is ignored.
     = [0..6] + "0" + [7..8]
  end

  {
    bank_code: bank_code,
    branch_code: branch_code,
    account_number: ,
  }
end

.clean_pl_details(local_details) ⇒ Object



508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
# File 'lib/ibandit/local_details_cleaner.rb', line 508

def self.clean_pl_details(local_details)
  # This method supports being passed the component IBAN parts, as defined
  # by SWIFT, or a single 26 digit string.
  if local_details[:bank_code]
    bank_code      = local_details[:bank_code]
     = local_details[:account_number]
  else
    cleaned_acct_number = local_details[:account_number].gsub(/\s/, "")

    bank_code      = cleaned_acct_number.slice(2, 8)
     = cleaned_acct_number[10..-1]
  end

  {
    bank_code: bank_code,
    account_number: ,
  }
end

.clean_pt_details(local_details) ⇒ Object



527
528
529
# File 'lib/ibandit/local_details_cleaner.rb', line 527

def self.clean_pt_details(local_details)
  local_details
end

.clean_ro_details(local_details) ⇒ Object



531
532
533
534
# File 'lib/ibandit/local_details_cleaner.rb', line 531

def self.clean_ro_details(local_details)
  # Romanian national bank details were replaced with IBANs in 2004.
  local_details
end

.clean_se_details(local_details) ⇒ Object



536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
# File 'lib/ibandit/local_details_cleaner.rb', line 536

def self.clean_se_details(local_details)
  if local_details[:bank_code]
    # If a bank_code was provided without a branch code we're (probably)
    # dealing with SWIFT details and should just return them.
    {
      swift_account_number: local_details[:account_number],
      swift_bank_code: local_details[:bank_code],
    }
  else
    Sweden::LocalDetailsConverter.new(
      branch_code: local_details[:branch_code],
      account_number: local_details[:account_number],
    ).convert
  end
end

.clean_si_details(local_details) ⇒ Object



552
553
554
555
556
557
558
# File 'lib/ibandit/local_details_cleaner.rb', line 552

def self.clean_si_details(local_details)
  # Add leading zeros to account number if < 10 digits.
  {
    bank_code: local_details[:bank_code],
    account_number: local_details[:account_number].rjust(10, "0"),
  }
end

.clean_sk_details(local_details) ⇒ Object



560
561
562
563
# File 'lib/ibandit/local_details_cleaner.rb', line 560

def self.clean_sk_details(local_details)
  # Slovakia uses the same local details method as the Czech Republic
  clean_cz_details(local_details)
end

.clean_sm_details(local_details) ⇒ Object



565
566
567
568
# File 'lib/ibandit/local_details_cleaner.rb', line 565

def self.clean_sm_details(local_details)
  # San Marino uses the same local details method as France
  clean_it_details(local_details)
end

.clean_us_details(local_details) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/ibandit/local_details_cleaner.rb', line 108

def self.clean_us_details(local_details)
  return {} unless local_details[:bank_code].length == 9

   = local_details[:account_number].delete(" ")
  return {} unless (1..17).cover?(.length)

  {
    bank_code: local_details[:bank_code],
    account_number: ,
  }
end

.explicit_swift_details?(local_details) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/ibandit/local_details_cleaner.rb', line 27

def self.explicit_swift_details?(local_details)
  local_details.include?(:swift_account_number)
end

.fields_for?(country_code, opts) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/ibandit/local_details_cleaner.rb', line 31

def self.fields_for?(country_code, opts)
  required_fields(country_code).all? { |argument| opts[argument] }
end

.required_fields(country_code) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ibandit/local_details_cleaner.rb', line 35

def self.required_fields(country_code)
  case country_code
  when "AT", "CY", "DE", "FI", "LT", "LU",
        "LV", "NL", "RO", "SI", "SK", "US"
    %i[bank_code account_number]
  when "BE", "CZ", "DK", "EE", "ES", "HR",
        "HU", "IS", "NO", "PL", "SE", "NZ"
    %i[account_number]
  when "GB", "IE", "MT"
    if Ibandit.bic_finder.nil? then %i[bank_code branch_code account_number]
    else
      %i[branch_code account_number]
    end
  when "AU"
    %i[branch_code account_number]
  else
    %i[bank_code branch_code account_number]
  end
end