ibandit 0.11.6 → 0.11.7

Sign up to get free protection for your applications and to get access to all the features.
data/lib/ibandit/iban.rb CHANGED
@@ -1,26 +1,32 @@
1
- require 'yaml'
1
+ require "yaml"
2
2
 
3
3
  module Ibandit
4
4
  class IBAN
5
5
  attr_reader :errors, :iban, :country_code, :check_digits, :bank_code,
6
6
  :branch_code, :account_number, :swift_bank_code,
7
- :swift_branch_code, :swift_account_number
7
+ :swift_branch_code, :swift_account_number, :source
8
8
 
9
9
  def initialize(argument)
10
10
  if argument.is_a?(String)
11
- input = argument.to_s.gsub(/\s+/, '').upcase
12
-
13
- if pseudo_iban?(input) && !PseudoIBANSplitter.new(input).split.nil?
14
- local_details = PseudoIBANSplitter.new(input).split
11
+ input = argument.to_s.gsub(/\s+/, "").upcase
12
+
13
+ pseudo_iban_splitter = PseudoIBANSplitter.new(input)
14
+ is_pseudo_iban_country = Constants::PSEUDO_IBAN_COUNTRY_CODES.
15
+ include?(pseudo_iban_splitter.country_code)
16
+ if pseudo_iban?(input) && is_pseudo_iban_country
17
+ @source = :pseudo_iban
18
+ local_details = pseudo_iban_splitter.split
15
19
  build_iban_from_local_details(local_details)
16
20
  else
21
+ @source = :iban
17
22
  @iban = input
18
23
  extract_swift_details_from_iban!
19
24
  end
20
25
  elsif argument.is_a?(Hash)
26
+ @source = :local_details
21
27
  build_iban_from_local_details(argument)
22
28
  else
23
- raise TypeError, 'Must pass an IBAN string or hash of local details'
29
+ raise TypeError, "Must pass an IBAN string or hash of local details"
24
30
  end
25
31
 
26
32
  @errors = {}
@@ -51,7 +57,7 @@ module Ibandit
51
57
 
52
58
  iban.slice(
53
59
  structure[:local_check_digit_position] - 1,
54
- structure[:local_check_digit_length]
60
+ structure[:local_check_digit_length],
55
61
  )
56
62
  end
57
63
 
@@ -64,7 +70,7 @@ module Ibandit
64
70
  country_code: country_code,
65
71
  bank_code: bank_code,
66
72
  branch_code: branch_code,
67
- account_number: account_number
73
+ account_number: account_number,
68
74
  ).assemble
69
75
  end
70
76
 
@@ -73,21 +79,27 @@ module Ibandit
73
79
  ###############
74
80
 
75
81
  def valid?
76
- [
77
- valid_country_code?,
78
- valid_characters?,
79
- valid_check_digits?,
80
- valid_length?,
81
- valid_bank_code_length?,
82
- valid_branch_code_length?,
83
- valid_account_number_length?,
84
- valid_format?,
85
- valid_bank_code_format?,
86
- valid_branch_code_format?,
87
- valid_account_number_format?,
88
- valid_local_modulus_check?,
89
- passes_country_specific_checks?
90
- ].all?
82
+ has_iban = !iban.nil?
83
+ has_pseudo_iban = !pseudo_iban.nil?
84
+
85
+ if has_pseudo_iban && !has_iban
86
+ valid_pseudo_iban?
87
+ else
88
+ valid_iban?
89
+ end
90
+ end
91
+
92
+ def valid_pseudo_iban_check_digits?
93
+ return true unless source == :pseudo_iban
94
+ return true if check_digits == Constants::PSEUDO_IBAN_CHECK_DIGITS
95
+
96
+ @errors[:check_digits] =
97
+ Ibandit.translate(
98
+ :invalid_check_digits,
99
+ expected_check_digits: Constants::PSEUDO_IBAN_CHECK_DIGITS,
100
+ check_digits: check_digits,
101
+ )
102
+ false
91
103
  end
92
104
 
93
105
  def valid_country_code?
@@ -131,8 +143,9 @@ module Ibandit
131
143
 
132
144
  def valid_bank_code_length?
133
145
  return unless valid_country_code?
146
+ return true if structure[:bank_code_length] == 0
134
147
 
135
- if swift_bank_code.nil? || swift_bank_code.length == 0
148
+ if swift_bank_code.nil? || swift_bank_code.empty?
136
149
  @errors[:bank_code] = Ibandit.translate(:is_required)
137
150
  return false
138
151
  end
@@ -153,7 +166,7 @@ module Ibandit
153
166
  if structure[:branch_code_length] == 0
154
167
  @errors[:branch_code] = Ibandit.translate(:not_used_in_country,
155
168
  country_code: country_code)
156
- elsif swift_branch_code.nil? || swift_branch_code.length == 0
169
+ elsif swift_branch_code.nil? || swift_branch_code.empty?
157
170
  @errors[:branch_code] = Ibandit.translate(:is_required)
158
171
  else
159
172
  @errors[:branch_code] =
@@ -186,7 +199,7 @@ module Ibandit
186
199
  if iban.scan(/[^A-Z0-9]/).any?
187
200
  @errors[:characters] =
188
201
  Ibandit.translate(:non_alphanumeric_characters,
189
- characters: iban.scan(/[^A-Z\d]/).join(' '))
202
+ characters: iban.scan(/[^A-Z\d]/).join(" "))
190
203
  false
191
204
  else
192
205
  true
@@ -195,6 +208,7 @@ module Ibandit
195
208
 
196
209
  def valid_format?
197
210
  return unless valid_country_code?
211
+ return unless structure[:bban_format]
198
212
 
199
213
  if bban =~ Regexp.new(structure[:bban_format])
200
214
  true
@@ -207,6 +221,7 @@ module Ibandit
207
221
 
208
222
  def valid_bank_code_format?
209
223
  return unless valid_bank_code_length?
224
+ return true if structure[:bank_code_length] == 0
210
225
 
211
226
  if swift_bank_code =~ Regexp.new(structure[:bank_code_format])
212
227
  true
@@ -252,21 +267,21 @@ module Ibandit
252
267
  return unless valid_country_code?
253
268
 
254
269
  case country_code
255
- when 'DE' then supports_iban_determination?
256
- when 'SE' then valid_swedish_details?
270
+ when "DE" then supports_iban_determination?
271
+ when "SE" then valid_swedish_details?
257
272
  else true
258
273
  end
259
274
  end
260
275
 
261
276
  def supports_iban_determination?
262
277
  return unless valid_format?
263
- return true unless country_code == 'DE'
278
+ return true unless country_code == "DE"
264
279
 
265
280
  begin
266
281
  GermanDetailsConverter.convert(
267
282
  country_code: country_code,
268
283
  bank_code: swift_bank_code,
269
- account_number: swift_account_number
284
+ account_number: swift_account_number,
270
285
  )
271
286
  true
272
287
  rescue UnsupportedAccountDetails
@@ -276,7 +291,7 @@ module Ibandit
276
291
  end
277
292
 
278
293
  def valid_swedish_details?
279
- return true unless country_code == 'SE'
294
+ return true unless country_code == "SE"
280
295
 
281
296
  if branch_code
282
297
  valid_swedish_local_details?
@@ -296,7 +311,7 @@ module Ibandit
296
311
  length_valid =
297
312
  Sweden::Validator.account_number_length_valid_for_bank_code?(
298
313
  bank_code: swift_bank_code,
299
- account_number: swift_account_number
314
+ account_number: swift_account_number,
300
315
  )
301
316
 
302
317
  unless length_valid
@@ -315,7 +330,7 @@ module Ibandit
315
330
 
316
331
  valid_serial_number = Sweden::Validator.valid_serial_number_length?(
317
332
  clearing_code: branch_code,
318
- serial_number: account_number
333
+ serial_number: account_number,
319
334
  )
320
335
 
321
336
  unless valid_serial_number
@@ -332,6 +347,38 @@ module Ibandit
332
347
 
333
348
  private
334
349
 
350
+ def valid_pseudo_iban?
351
+ [
352
+ valid_pseudo_iban_check_digits?,
353
+ valid_country_code?,
354
+ valid_bank_code_length?,
355
+ valid_branch_code_length?,
356
+ valid_account_number_length?,
357
+ valid_bank_code_format?,
358
+ valid_branch_code_format?,
359
+ valid_account_number_format?,
360
+ passes_country_specific_checks?,
361
+ ].all?
362
+ end
363
+
364
+ def valid_iban?
365
+ [
366
+ valid_country_code?,
367
+ valid_characters?,
368
+ valid_check_digits?,
369
+ valid_length?,
370
+ valid_bank_code_length?,
371
+ valid_branch_code_length?,
372
+ valid_account_number_length?,
373
+ valid_format?,
374
+ valid_bank_code_format?,
375
+ valid_branch_code_format?,
376
+ valid_account_number_format?,
377
+ valid_local_modulus_check?,
378
+ passes_country_specific_checks?,
379
+ ].all?
380
+ end
381
+
335
382
  def decomposable?
336
383
  [iban, country_code, swift_bank_code, swift_account_number].none?(&:nil?)
337
384
  end
@@ -349,7 +396,12 @@ module Ibandit
349
396
  @swift_bank_code = try_dup(local_details[:swift_bank_code])
350
397
 
351
398
  @iban = IBANAssembler.assemble(swift_details)
352
- @check_digits = @iban.slice(2, 2) unless @iban.nil?
399
+
400
+ if source == :pseudo_iban
401
+ @check_digits = try_dup(local_details[:check_digits])
402
+ else
403
+ @check_digits = @iban.slice(2, 2) unless @iban.nil?
404
+ end
353
405
  end
354
406
 
355
407
  def extract_swift_details_from_iban!
@@ -363,7 +415,7 @@ module Ibandit
363
415
  @swift_account_number = swift_details[:account_number]
364
416
 
365
417
  return if Constants::PSEUDO_IBAN_COUNTRY_CODES.
366
- include?(@country_code)
418
+ include?(@country_code)
367
419
 
368
420
  @bank_code = swift_details[:bank_code]
369
421
  @branch_code = swift_details[:branch_code]
@@ -410,11 +462,12 @@ module Ibandit
410
462
  country_code: @country_code,
411
463
  account_number: @swift_account_number,
412
464
  branch_code: @swift_branch_code,
413
- bank_code: @swift_bank_code
465
+ bank_code: @swift_bank_code,
414
466
  }
415
467
  end
416
468
 
417
469
  def pseudo_iban?(input)
470
+ return false if input.nil?
418
471
  input.slice(2, 2) == Constants::PSEUDO_IBAN_CHECK_DIGITS
419
472
  end
420
473
  end
@@ -1,6 +1,6 @@
1
1
  module Ibandit
2
2
  module IBANAssembler
3
- EXCEPTION_COUNTRY_CODES = %w(IT SM BE).freeze
3
+ EXCEPTION_COUNTRY_CODES = %w[IT SM BE].freeze
4
4
 
5
5
  def self.assemble(local_details)
6
6
  country_code = local_details[:country_code]
@@ -47,7 +47,7 @@ module Ibandit
47
47
  partial_bban = [
48
48
  opts[:bank_code],
49
49
  opts[:branch_code],
50
- opts[:account_number]
50
+ opts[:account_number],
51
51
  ].join
52
52
 
53
53
  check_digit = opts[:check_digit] || CheckDigit.italian(partial_bban)
@@ -69,7 +69,8 @@ module Ibandit
69
69
  end
70
70
 
71
71
  def self.supported_country_code?(local_details)
72
- Constants::SUPPORTED_COUNTRY_CODES.include?(local_details[:country_code])
72
+ Constants::CONSTRUCTABLE_IBAN_COUNTRY_CODES.
73
+ include?(local_details[:country_code])
73
74
  end
74
75
 
75
76
  def self.valid_arguments?(local_details)
@@ -86,22 +87,23 @@ module Ibandit
86
87
 
87
88
  def self.required_fields(country_code)
88
89
  case country_code
89
- when *%w(AT CY CZ DE DK EE FI HR IS LT LU LV NL NO PL RO SE SI SK)
90
- %i(bank_code account_number)
91
- when 'BE'
92
- %i(account_number)
90
+ when "AT", "CY", "CZ", "DE", "DK", "EE", "FI", "HR", "IS", "LT", "LU",
91
+ "LV", "NL", "NO", "PL", "RO", "SE", "SI", "SK"
92
+ %i[bank_code account_number]
93
+ when "BE"
94
+ %i[account_number]
93
95
  else
94
- %i(bank_code branch_code account_number)
96
+ %i[bank_code branch_code account_number]
95
97
  end
96
98
  end
97
99
 
98
100
  def self.allowed_fields(country_code)
99
101
  # Some countries have additional optional fields
100
102
  case country_code
101
- when 'BE' then %i(bank_code account_number)
102
- when 'CY' then %i(bank_code branch_code account_number)
103
- when 'IT' then %i(bank_code branch_code account_number check_digit)
104
- when 'CZ', 'SK' then %i(bank_code account_number account_number_prefix)
103
+ when "BE" then %i[bank_code account_number]
104
+ when "CY" then %i[bank_code branch_code account_number]
105
+ when "IT" then %i[bank_code branch_code account_number check_digit]
106
+ when "CZ", "SK" then %i[bank_code account_number account_number_prefix]
105
107
  else required_fields(country_code)
106
108
  end
107
109
  end
@@ -110,7 +112,7 @@ module Ibandit
110
112
  [
111
113
  country_code,
112
114
  CheckDigit.iban(country_code, bban),
113
- bban
115
+ bban,
114
116
  ].join
115
117
  rescue InvalidCharacterError
116
118
  nil
@@ -6,7 +6,7 @@ module Ibandit
6
6
  check_digits: check_digits_from(iban),
7
7
  bank_code: bank_code_from(iban),
8
8
  branch_code: branch_code_from(iban),
9
- account_number: account_number_from(iban)
9
+ account_number: account_number_from(iban),
10
10
  }
11
11
  end
12
12
 
@@ -31,7 +31,7 @@ module Ibandit
31
31
 
32
32
  iban.slice(
33
33
  structure(iban)[:bank_code_position] - 1,
34
- structure(iban)[:bank_code_length]
34
+ structure(iban)[:bank_code_length],
35
35
  )
36
36
  end
37
37
 
@@ -42,7 +42,7 @@ module Ibandit
42
42
 
43
43
  iban.slice(
44
44
  structure(iban)[:branch_code_position] - 1,
45
- structure(iban)[:branch_code_length]
45
+ structure(iban)[:branch_code_length],
46
46
  )
47
47
  end
48
48
 
@@ -51,7 +51,7 @@ module Ibandit
51
51
 
52
52
  iban.slice(
53
53
  structure(iban)[:account_number_position] - 1,
54
- structure(iban)[:account_number_length]
54
+ structure(iban)[:account_number_length],
55
55
  )
56
56
  end
57
57
 
@@ -2,13 +2,13 @@ module Ibandit
2
2
  module LocalDetailsCleaner
3
3
  def self.clean(local_details)
4
4
  country_code = local_details[:country_code]
5
-
6
5
  if can_clean?(country_code, local_details)
7
6
  local_details = local_details.merge(
8
- public_send(:"clean_#{country_code.downcase}_details", local_details))
7
+ public_send(:"clean_#{country_code.downcase}_details", local_details),
8
+ )
9
9
  end
10
10
 
11
- return local_details if explicit_swift_details?(country_code)
11
+ return local_details if explicit_swift_details?(local_details)
12
12
 
13
13
  swift_details_for(local_details).merge(local_details)
14
14
  end
@@ -22,8 +22,8 @@ module Ibandit
22
22
  fields_for?(country_code, local_details)
23
23
  end
24
24
 
25
- def self.explicit_swift_details?(country_code)
26
- Constants::PSEUDO_IBAN_COUNTRY_CODES.include?(country_code)
25
+ def self.explicit_swift_details?(local_details)
26
+ local_details.include?(:swift_account_number)
27
27
  end
28
28
 
29
29
  def self.fields_for?(country_code, opts)
@@ -32,16 +32,18 @@ module Ibandit
32
32
 
33
33
  def self.required_fields(country_code)
34
34
  case country_code
35
- when 'AT', 'CY', 'DE', 'FI', 'LT', 'LU', 'LV', 'NL', 'RO', 'SI', 'SK'
36
- %i(bank_code account_number)
37
- when 'BE', 'CZ', 'DK', 'EE', 'ES', 'HR', 'HU', 'IS', 'NO', 'PL', 'SE'
38
- %i(account_number)
39
- when 'GB', 'IE', 'MT'
40
- if Ibandit.bic_finder.nil? then %i(bank_code branch_code account_number)
41
- else %i(branch_code account_number)
35
+ when "AT", "CY", "DE", "FI", "LT", "LU", "LV", "NL", "RO", "SI", "SK"
36
+ %i[bank_code account_number]
37
+ when "BE", "CZ", "DK", "EE", "ES", "HR", "HU", "IS", "NO", "PL", "SE"
38
+ %i[account_number]
39
+ when "GB", "IE", "MT"
40
+ if Ibandit.bic_finder.nil? then %i[bank_code branch_code account_number]
41
+ else %i[branch_code account_number]
42
42
  end
43
+ when "AU"
44
+ %i[branch_code account_number]
43
45
  else
44
- %i(bank_code branch_code account_number)
46
+ %i[bank_code branch_code account_number]
45
47
  end
46
48
  end
47
49
 
@@ -55,16 +57,23 @@ module Ibandit
55
57
  return {} unless local_details[:account_number].length >= 4
56
58
  {
57
59
  bank_code: local_details[:bank_code],
58
- account_number: local_details[:account_number].rjust(11, '0')
60
+ account_number: local_details[:account_number].rjust(11, "0"),
61
+ }
62
+ end
63
+
64
+ def self.clean_au_details(local_details)
65
+ {
66
+ branch_code: local_details[:branch_code].delete("-"),
67
+ account_number: local_details[:account_number],
59
68
  }
60
69
  end
61
70
 
62
71
  def self.clean_be_details(local_details)
63
- account_number = local_details[:account_number].tr('-', '')
72
+ account_number = local_details[:account_number].tr("-", "")
64
73
 
65
74
  {
66
75
  bank_code: local_details[:bank_code] || account_number.slice(0, 3),
67
- account_number: account_number
76
+ account_number: account_number,
68
77
  }
69
78
  end
70
79
 
@@ -76,7 +85,7 @@ module Ibandit
76
85
  def self.clean_cy_details(local_details)
77
86
  # Account number may be 7-16 digits long.
78
87
  # Add leading zeros to account number if < 16 digits.
79
- cleaned_bank_code = local_details[:bank_code].gsub(/[-\s]/, '')
88
+ cleaned_bank_code = local_details[:bank_code].gsub(/[-\s]/, "")
80
89
 
81
90
  bank_code = cleaned_bank_code.slice(0, 3)
82
91
  branch_code =
@@ -87,7 +96,7 @@ module Ibandit
87
96
  end
88
97
  account_number =
89
98
  if local_details[:account_number].length >= 7
90
- local_details[:account_number].rjust(16, '0')
99
+ local_details[:account_number].rjust(16, "0")
91
100
  else
92
101
  local_details[:account_number]
93
102
  end
@@ -95,7 +104,7 @@ module Ibandit
95
104
  {
96
105
  bank_code: bank_code,
97
106
  branch_code: branch_code,
98
- account_number: account_number
107
+ account_number: account_number,
99
108
  }
100
109
  end
101
110
 
@@ -106,16 +115,16 @@ module Ibandit
106
115
  account_number =
107
116
  if local_details.include?(:account_number_prefix)
108
117
  [
109
- local_details[:account_number_prefix].rjust(6, '0'),
110
- local_details[:account_number].rjust(10, '0')
118
+ local_details[:account_number_prefix].rjust(6, "0"),
119
+ local_details[:account_number].rjust(10, "0"),
111
120
  ].join
112
121
  else
113
- local_details[:account_number].tr('-', '').rjust(16, '0')
122
+ local_details[:account_number].tr("-", "").rjust(16, "0")
114
123
  end
115
124
 
116
125
  {
117
126
  bank_code: local_details[:bank_code],
118
- account_number: account_number
127
+ account_number: account_number,
119
128
  }
120
129
  end
121
130
 
@@ -137,7 +146,7 @@ module Ibandit
137
146
 
138
147
  {
139
148
  bank_code: converted_details[:bank_code],
140
- account_number: converted_details[:account_number].rjust(10, '0')
149
+ account_number: converted_details[:account_number].rjust(10, "0"),
141
150
  }
142
151
  end
143
152
 
@@ -145,12 +154,12 @@ module Ibandit
145
154
  # This method supports being passed the component IBAN parts, as defined
146
155
  # by SWIFT, or a single traditional-format string split by a '-'.
147
156
  if local_details[:bank_code]
148
- bank_code = local_details[:bank_code].rjust(4, '0')
149
- account_number = local_details[:account_number].rjust(10, '0')
150
- elsif local_details[:account_number].include?('-')
151
- bank_code, account_number = local_details[:account_number].split('-', 2)
152
- elsif local_details[:account_number].gsub(/\s/, '').length == 14
153
- cleaned_account_number = local_details[:account_number].gsub(/\s/, '')
157
+ bank_code = local_details[:bank_code].rjust(4, "0")
158
+ account_number = local_details[:account_number].rjust(10, "0")
159
+ elsif local_details[:account_number].include?("-")
160
+ bank_code, account_number = local_details[:account_number].split("-", 2)
161
+ elsif local_details[:account_number].gsub(/\s/, "").length == 14
162
+ cleaned_account_number = local_details[:account_number].gsub(/\s/, "")
154
163
  bank_code = cleaned_account_number.slice(0, 4)
155
164
  account_number = cleaned_account_number.slice(4, 10)
156
165
  else
@@ -158,8 +167,8 @@ module Ibandit
158
167
  end
159
168
 
160
169
  {
161
- bank_code: bank_code.rjust(4, '0'),
162
- account_number: account_number.delete('-').rjust(10, '0')
170
+ bank_code: bank_code.rjust(4, "0"),
171
+ account_number: account_number.delete("-").rjust(10, "0"),
163
172
  }
164
173
  end
165
174
 
@@ -171,16 +180,16 @@ module Ibandit
171
180
  # account number and converted using the rules at
172
181
  # http://www.pangaliit.ee/en/settlements-and-standards/bank-codes-of-estonian-banks
173
182
  domestic_bank_code =
174
- local_details[:account_number].gsub(/\A0+/, '').slice(0, 2)
183
+ local_details[:account_number].gsub(/\A0+/, "").slice(0, 2)
175
184
 
176
185
  iban_bank_code =
177
186
  case domestic_bank_code
178
- when '11' then '22'
179
- when '93' then '00'
187
+ when "11" then "22"
188
+ when "93" then "00"
180
189
  else domestic_bank_code
181
190
  end
182
191
 
183
- account_number = local_details[:account_number].rjust(14, '0')
192
+ account_number = local_details[:account_number].rjust(14, "0")
184
193
 
185
194
  { bank_code: iban_bank_code, account_number: account_number }
186
195
  end
@@ -191,10 +200,10 @@ module Ibandit
191
200
  if local_details[:bank_code] && local_details[:branch_code]
192
201
  bank_code = local_details[:bank_code]
193
202
  branch_code = local_details[:branch_code]
194
- account_number = local_details[:account_number].gsub(/[-\s]/, '')
203
+ account_number = local_details[:account_number].gsub(/[-\s]/, "")
195
204
  else
196
205
  cleaned_account_number =
197
- local_details[:account_number].gsub(/[-\s]/, '')
206
+ local_details[:account_number].gsub(/[-\s]/, "")
198
207
 
199
208
  bank_code = cleaned_account_number.slice(0, 4)
200
209
  branch_code = cleaned_account_number.slice(4, 4)
@@ -204,7 +213,7 @@ module Ibandit
204
213
  {
205
214
  bank_code: bank_code,
206
215
  branch_code: branch_code,
207
- account_number: account_number
216
+ account_number: account_number,
208
217
  }
209
218
  end
210
219
 
@@ -213,18 +222,18 @@ module Ibandit
213
222
  # by adding zero-padding. The expansion method depends on the first
214
223
  # character of the bank code.
215
224
  account_number =
216
- if %w(4 5 6).include?(local_details[:bank_code][0])
225
+ if %w[4 5 6].include?(local_details[:bank_code][0])
217
226
  [
218
227
  local_details[:account_number][0],
219
- local_details[:account_number][1..-1].rjust(7, '0')
228
+ local_details[:account_number][1..-1].rjust(7, "0"),
220
229
  ].join
221
230
  else
222
- local_details[:account_number].rjust(8, '0')
231
+ local_details[:account_number].rjust(8, "0")
223
232
  end
224
233
 
225
234
  {
226
235
  bank_code: local_details[:bank_code],
227
- account_number: account_number
236
+ account_number: account_number,
228
237
  }
229
238
  end
230
239
 
@@ -232,29 +241,29 @@ module Ibandit
232
241
  {
233
242
  bank_code: local_details[:bank_code],
234
243
  branch_code: local_details[:branch_code],
235
- account_number: local_details[:account_number].gsub(/[-\s]/, '')
244
+ account_number: local_details[:account_number].gsub(/[-\s]/, ""),
236
245
  }
237
246
  end
238
247
 
239
248
  def self.clean_gb_details(local_details)
240
249
  # Account number may be 6-8 digits
241
250
  # Add leading zeros to account number if < 8 digits.
242
- branch_code = local_details[:branch_code].gsub(/[-\s]/, '')
251
+ branch_code = local_details[:branch_code].gsub(/[-\s]/, "")
243
252
 
244
253
  if local_details[:bank_code]
245
254
  bank_code = local_details[:bank_code]
246
255
  else
247
- bic = Ibandit.find_bic('GB', branch_code)
256
+ bic = Ibandit.find_bic("GB", branch_code)
248
257
  bank_code = bic.nil? ? nil : bic.slice(0, 4)
249
258
  end
250
259
 
251
- account_number = local_details[:account_number].gsub(/[-\s]/, '')
252
- account_number = account_number.rjust(8, '0') if account_number.length > 5
260
+ account_number = local_details[:account_number].gsub(/[-\s]/, "")
261
+ account_number = account_number.rjust(8, "0") if account_number.length > 5
253
262
 
254
263
  {
255
264
  bank_code: bank_code,
256
265
  branch_code: branch_code,
257
- account_number: account_number
266
+ account_number: account_number,
258
267
  }
259
268
  end
260
269
 
@@ -268,13 +277,13 @@ module Ibandit
268
277
  # This method supports being passed the component IBAN parts, as defined
269
278
  # by SWIFT, or a single traditional-format string split by a '-'.
270
279
  return local_details if local_details[:bank_code]
271
- return local_details unless local_details[:account_number].include?('-')
280
+ return local_details unless local_details[:account_number].include?("-")
272
281
 
273
- bank_code, account_number = local_details[:account_number].split('-', 2)
282
+ bank_code, account_number = local_details[:account_number].split("-", 2)
274
283
 
275
284
  {
276
285
  bank_code: bank_code,
277
- account_number: account_number
286
+ account_number: account_number,
278
287
  }
279
288
  end
280
289
 
@@ -285,20 +294,20 @@ module Ibandit
285
294
  return local_details
286
295
  end
287
296
 
288
- cleaned_acct_number = local_details[:account_number].gsub(/[-\s]/, '')
297
+ cleaned_acct_number = local_details[:account_number].gsub(/[-\s]/, "")
289
298
 
290
299
  case cleaned_acct_number.length
291
300
  when 16
292
301
  {
293
302
  bank_code: cleaned_acct_number.slice(0, 3),
294
303
  branch_code: cleaned_acct_number.slice(3, 4),
295
- account_number: cleaned_acct_number.slice(7, 9).ljust(17, '0')
304
+ account_number: cleaned_acct_number.slice(7, 9).ljust(17, "0"),
296
305
  }
297
306
  when 24
298
307
  {
299
308
  bank_code: cleaned_acct_number.slice(0, 3),
300
309
  branch_code: cleaned_acct_number.slice(3, 4),
301
- account_number: cleaned_acct_number.slice(7, 17)
310
+ account_number: cleaned_acct_number.slice(7, 17),
302
311
  }
303
312
  else local_details
304
313
  end
@@ -306,39 +315,39 @@ module Ibandit
306
315
 
307
316
  def self.clean_ie_details(local_details)
308
317
  # Ireland uses the same local details as the United Kingdom
309
- branch_code = local_details[:branch_code].gsub(/[-\s]/, '')
318
+ branch_code = local_details[:branch_code].gsub(/[-\s]/, "")
310
319
 
311
320
  if local_details[:bank_code]
312
321
  bank_code = local_details[:bank_code]
313
322
  else
314
- bic = Ibandit.find_bic('IE', branch_code)
323
+ bic = Ibandit.find_bic("IE", branch_code)
315
324
  bank_code = bic.nil? ? nil : bic.slice(0, 4)
316
325
  end
317
326
 
318
- account_number = local_details[:account_number].gsub(/[-\s]/, '')
319
- account_number = account_number.rjust(8, '0') if account_number.length > 5
327
+ account_number = local_details[:account_number].gsub(/[-\s]/, "")
328
+ account_number = account_number.rjust(8, "0") if account_number.length > 5
320
329
 
321
330
  {
322
331
  bank_code: bank_code,
323
332
  branch_code: branch_code,
324
- account_number: account_number
333
+ account_number: account_number,
325
334
  }
326
335
  end
327
336
 
328
337
  def self.clean_is_details(local_details)
329
338
  if local_details[:bank_code]
330
339
  bank_code = local_details[:bank_code]
331
- parts = local_details[:account_number].split('-')
332
- elsif local_details[:account_number].include?('-')
333
- bank_code, *parts = local_details[:account_number].split('-')
340
+ parts = local_details[:account_number].split("-")
341
+ elsif local_details[:account_number].include?("-")
342
+ bank_code, *parts = local_details[:account_number].split("-")
334
343
  else
335
344
  bank_code = local_details[:account_number].slice(0, 4)
336
345
  parts = Array(local_details[:account_number][4..-1])
337
346
  end
338
347
 
339
348
  {
340
- bank_code: bank_code.rjust(4, '0'),
341
- account_number: pad_is_account_number(parts)
349
+ bank_code: bank_code.rjust(4, "0"),
350
+ account_number: pad_is_account_number(parts),
342
351
  }
343
352
  end
344
353
 
@@ -347,7 +356,7 @@ module Ibandit
347
356
  {
348
357
  bank_code: local_details[:bank_code],
349
358
  branch_code: local_details[:branch_code],
350
- account_number: local_details[:account_number].rjust(12, '0')
359
+ account_number: local_details[:account_number].rjust(12, "0"),
351
360
  }
352
361
  end
353
362
 
@@ -378,17 +387,17 @@ module Ibandit
378
387
  if local_details[:bank_code]
379
388
  bank_code = local_details[:bank_code]
380
389
  else
381
- bic = Ibandit.find_bic('MT', branch_code)
390
+ bic = Ibandit.find_bic("MT", branch_code)
382
391
  bank_code = bic.nil? ? nil : bic.slice(0, 4)
383
392
  end
384
393
 
385
- account_number = local_details[:account_number].gsub(/[-\s]/, '')
386
- account_number = account_number.rjust(18, '0')
394
+ account_number = local_details[:account_number].gsub(/[-\s]/, "")
395
+ account_number = account_number.rjust(18, "0")
387
396
 
388
397
  {
389
398
  bank_code: bank_code,
390
399
  branch_code: branch_code,
391
- account_number: account_number
400
+ account_number: account_number,
392
401
  }
393
402
  end
394
403
 
@@ -396,7 +405,7 @@ module Ibandit
396
405
  # Add leading zeros to account number if < 10 digits.
397
406
  {
398
407
  bank_code: local_details[:bank_code],
399
- account_number: local_details[:account_number].rjust(10, '0')
408
+ account_number: local_details[:account_number].rjust(10, "0"),
400
409
  }
401
410
  end
402
411
 
@@ -407,7 +416,7 @@ module Ibandit
407
416
  bank_code = local_details[:bank_code]
408
417
  account_number = local_details[:account_number]
409
418
  else
410
- cleaned_acct_number = local_details[:account_number].gsub(/[-.\s]/, '')
419
+ cleaned_acct_number = local_details[:account_number].gsub(/[-.\s]/, "")
411
420
 
412
421
  bank_code = cleaned_acct_number.slice(0, 4)
413
422
  account_number = cleaned_acct_number[4..-1]
@@ -415,7 +424,7 @@ module Ibandit
415
424
 
416
425
  {
417
426
  bank_code: bank_code,
418
- account_number: account_number
427
+ account_number: account_number,
419
428
  }
420
429
  end
421
430
 
@@ -426,7 +435,7 @@ module Ibandit
426
435
  bank_code = local_details[:bank_code]
427
436
  account_number = local_details[:account_number]
428
437
  else
429
- cleaned_acct_number = local_details[:account_number].gsub(/[\s]/, '')
438
+ cleaned_acct_number = local_details[:account_number].gsub(/[\s]/, "")
430
439
 
431
440
  bank_code = cleaned_acct_number.slice(2, 8)
432
441
  account_number = cleaned_acct_number[10..-1]
@@ -434,7 +443,7 @@ module Ibandit
434
443
 
435
444
  {
436
445
  bank_code: bank_code,
437
- account_number: account_number
446
+ account_number: account_number,
438
447
  }
439
448
  end
440
449
 
@@ -451,14 +460,14 @@ module Ibandit
451
460
  if local_details[:bank_code]
452
461
  # If a bank_code was provided without a branch code we're (probably)
453
462
  # dealing with SWIFT details and should just return them.
454
- return {
463
+ {
455
464
  swift_account_number: local_details[:account_number],
456
- swift_bank_code: local_details[:bank_code]
465
+ swift_bank_code: local_details[:bank_code],
457
466
  }
458
467
  else
459
468
  Sweden::LocalDetailsConverter.new(
460
469
  branch_code: local_details[:branch_code],
461
- account_number: local_details[:account_number]
470
+ account_number: local_details[:account_number],
462
471
  ).convert
463
472
  end
464
473
  end
@@ -467,7 +476,7 @@ module Ibandit
467
476
  # Add leading zeros to account number if < 10 digits.
468
477
  {
469
478
  bank_code: local_details[:bank_code],
470
- account_number: local_details[:account_number].rjust(10, '0')
479
+ account_number: local_details[:account_number].rjust(10, "0"),
471
480
  }
472
481
  end
473
482
 
@@ -482,12 +491,12 @@ module Ibandit
482
491
  end
483
492
 
484
493
  def self.pad_is_account_number(parts)
485
- hufo = parts[0].nil? ? '' : parts[0].rjust(2, '0')
486
- reikningsnumer = parts[1].nil? ? '' : parts[1].rjust(6, '0')
487
- ken_1 = parts[2].nil? ? '' : parts[2].rjust(6, '0')
488
- ken_2 = parts[3].nil? ? '' : parts[3].rjust(4, '0')
494
+ hufo = parts[0].nil? ? "" : parts[0].rjust(2, "0")
495
+ reikningsnumer = parts[1].nil? ? "" : parts[1].rjust(6, "0")
496
+ ken_1 = parts[2].nil? ? "" : parts[2].rjust(6, "0")
497
+ ken_2 = parts[3].nil? ? "" : parts[3].rjust(4, "0")
489
498
 
490
- kennitala = ken_1.empty? ? '' : (ken_1 + ken_2).rjust(10, '0')
499
+ kennitala = ken_1.empty? ? "" : (ken_1 + ken_2).rjust(10, "0")
491
500
 
492
501
  hufo + reikningsnumer + kennitala
493
502
  end
@@ -497,7 +506,7 @@ module Ibandit
497
506
  {
498
507
  swift_bank_code: local_details[:bank_code],
499
508
  swift_branch_code: local_details[:branch_code],
500
- swift_account_number: local_details[:account_number]
509
+ swift_account_number: local_details[:account_number],
501
510
  }
502
511
  end
503
512
  private_class_method :swift_details_for