iban-tools 1.0.0 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 97281d569702141068c2a7a708e8c2575ac867e7
4
- data.tar.gz: 32d623b80cb8878fc2295e27ca7bd987bcef5f1c
2
+ SHA256:
3
+ metadata.gz: 7212d683c7c3b3aacaf1d71a8954cfb325e0f4bcf8d8de768dd1c618e963f791
4
+ data.tar.gz: 0ea198c6321472c427cafbc96b17aaa4b28a15075cd2958ea64f48fdf65306ea
5
5
  SHA512:
6
- metadata.gz: f4528a15faae008626cffd1b92d4979a1d18f0414eea511e55163a07e5b0620796eb3b69f94d100fb43168a59ba28dc7f517501746f02c6bd52c650594b0010b
7
- data.tar.gz: f74d8c5ced292331dd28d3908035265d482161e3929c7d50f04344f4542dd94d037e989ccb219dd606898be7bd111387513563eab36e27eacce605c8762b816c
6
+ metadata.gz: 5db6b71a7245d8396e06b49907878ffc352604013ace9c38cbaed0e575950137c23e32e1094eeb869dd490d5488bfb17f542d7e0b24ce2962abbdeb8f026f12e
7
+ data.tar.gz: 4b76c82fbc3d64096ffe6b60578a11339ffc10c728d63f68f85d2a4eacc69acf309c814fa4742968c4951a52c256463253236a5452dccc7de7c372e4905d975a
data/README.md CHANGED
@@ -2,13 +2,13 @@
2
2
 
3
3
  iban-tools is a Ruby library for manipulating and validating IBAN account numbers. You can [read more about IBAN](http://en.wikipedia.org/wiki/International_Bank_Account_Number) on Wikipedia
4
4
 
5
- [![Build Status](https://travis-ci.org/alphasights/iban-tools.svg)](https://travis-ci.org/alphasights/iban-tools)
5
+ [![Build Status](https://travis-ci.org/alphasights/iban-tools.svg?branch=master)](https://travis-ci.org/alphasights/iban-tools)
6
6
 
7
7
  ## INSTALLATION
8
8
 
9
9
  gem install iban-tools
10
10
 
11
- The gem should be compatible with MRI 1.8 and 1.9.
11
+ The gem should be compatible with most Ruby versions.
12
12
 
13
13
  ## USAGE
14
14
 
@@ -34,7 +34,17 @@ Pretty print, canonicalize, and extract fields from an IBAN code
34
34
 
35
35
  iban.prettify
36
36
  => "RO49 AAAA 1B31 0075 9384 0000"
37
+
38
+ Convert local account numbers to IBAN and back to local.
39
+
40
+ iban = IBANTools::IBAN.from_local('NO', bank_code: '9710', account_number: '1112222', check_digit: '7')
41
+
42
+ iban.code
43
+ => "NO6197101112227"
44
+
45
+ iban.to_local
46
+ => {:bank_code=>"9710", :account_number=>"111222", :check_digit=>"7"}
37
47
 
38
48
  ## Credit
39
49
 
40
- [Iulianu](http://github.com/iulianu) wrote [iban-tools](http://github.com/iulianu/iban-tools). [AlphaSights](https://coderwall.com/team/alphasights) is maintaining the gem.
50
+ [Iulianu](http://github.com/iulianu) originally wrote [iban-tools](http://github.com/iulianu/iban-tools). The team at [AlphaSights](https://engineering.alphasights.com) is currently maintaining the gem.
@@ -4,12 +4,16 @@ module IBANTools
4
4
  def self.local2iban(country_code, data)
5
5
  config = load_config country_code
6
6
 
7
- bban = config.map do |key, value|
8
- insert_pattern = value[1]
9
- # apply integer-typecast if it is intended to format as decimal-value
10
- # this prevent mis-interpretation of integer with leading zeros into octal number!
11
- insert_value = insert_pattern.include?('d') ? data[key.to_sym].to_i : data[key.to_sym]
12
- insert_pattern % insert_value
7
+ bban = config.map do |key, values|
8
+ d = data[key.to_sym].dup
9
+ ret = [values].flatten.map do |value|
10
+ l = bban_format_length(value)
11
+ r = bban_format_to_format_string(value) % bban_format_cast(value, d[0..(l-1)])
12
+ d[0..(l-1)] = ''
13
+ r
14
+ end.join('')
15
+ # "%05s" % "a" -> " a" and not "0000a"
16
+ ret.gsub(/ /, '0')
13
17
  end.join('')
14
18
 
15
19
  check_digits = "%02d" % checksum(country_code, bban)
@@ -22,18 +26,85 @@ module IBANTools
22
26
  config = load_config country_code
23
27
 
24
28
  local = {}
25
- config.map do |key, value|
26
- local[key.to_sym] = bban.scan(/^#{value[0]}/).first.sub(/^0+/, '')
27
- bban.sub! /^#{value[0]}/, ''
29
+ config.map do |key, values|
30
+ local[key.to_sym] = [values].flatten.map do |value|
31
+ regexp = /^#{bban_format_to_regexp(value)}/
32
+ ret = bban.scan(regexp).first
33
+ bban.sub! regexp, ''
34
+ ret
35
+ end.join('')
36
+ local[key.to_sym].sub!(/^0+/, '')
37
+ local[key.to_sym] = '0' if local[key.to_sym] == ''
28
38
  end
29
39
  local
30
40
  end
31
41
 
32
42
  private
33
43
 
44
+ BBAN_REGEXP = /^(\d+)(!?)([nace])$/
45
+
46
+ def self.bban_format_length(format)
47
+ if format =~ BBAN_REGEXP
48
+ return $1.to_i
49
+ else
50
+ raise ArgumentError, "#{format} is not a valid bban format"
51
+ end
52
+ end
53
+
54
+ def self.bban_format_to_format_string(format)
55
+ if format =~ BBAN_REGEXP
56
+ if $3 == "e"
57
+ return " " * $1.to_i
58
+ end
59
+ format = '%0' + $1
60
+ format += case $3
61
+ when 'n' then 'd'
62
+ when 'a' then 's'
63
+ when 'c' then 's'
64
+ end
65
+ return format
66
+ else
67
+ raise ArgumentError, "#{format} is not a valid bban format"
68
+ end
69
+ end
70
+
71
+ def self.bban_format_cast(format, value)
72
+ if format =~ BBAN_REGEXP
73
+ if $3 == "n"
74
+ return value.to_i
75
+ else
76
+ return value
77
+ end
78
+ else
79
+ raise ArgumentError, "#{format} is not a valid bban format"
80
+ end
81
+ end
82
+
83
+ def self.bban_format_to_regexp(format)
84
+ if format =~ BBAN_REGEXP
85
+ regexp = case $3
86
+ when 'n' then '[0-9]'
87
+ when 'a' then '[A-Z]'
88
+ when 'c' then '[a-zA-Z0-9]'
89
+ when 'e' then '[ ]'
90
+ end
91
+ regexp += '{'
92
+ unless $2 == '!'
93
+ regexp += ','
94
+ end
95
+ regexp += $1 + '}'
96
+ return regexp
97
+ else
98
+ raise ArgumentError, "#{format} is not a valid bban format"
99
+ end
100
+ end
101
+
34
102
  def self.load_config(country_code)
35
103
  default_config = YAML.
36
- load(File.read(File.dirname(__FILE__) + '/conversion_rules.yml'))
104
+ load_file(File.join(File.dirname(__FILE__), 'conversion_rules.yml'))
105
+ unless default_config.key?(country_code)
106
+ raise ArgumentError, "Country code #{country_code} not availble"
107
+ end
37
108
  default_config[country_code]
38
109
  end
39
110
 
@@ -1,11 +1,326 @@
1
+ # Data from http://www.swift.com/dsp/resources/documents/IBAN_Registry.pdf
2
+ # And the parsed, readable table on
3
+ # http://en.wikipedia.org/wiki/International_Bank_Account_Number
4
+ #
5
+ # The values of the hashes follow the rules in the IBAN_Registry PDF:
6
+ #
7
+ # Length:
8
+ # nn! - mixed length
9
+ # nn - maximum length
10
+ #
11
+ # Character representation
12
+ # n - Digits (numeric characters 0 to 9 only)
13
+ # a - Upper case letters (alphabetic characters A-Z only)
14
+ # c - Upper and lower case alphanumeric characters (A-Z, a-z and 0-9)
15
+ # e - blank space
16
+ #
17
+ # If a field has more than one kind of character representation, an array can
18
+ # be used (see MU)
19
+ #
20
+
21
+ 'AD':
22
+ bank_code: 4!n
23
+ branch_code: 4!n
24
+ account_number: 12!n
25
+
26
+ 'AE':
27
+ bank_code: 3!n
28
+ account_number: 16!n
29
+
30
+ 'AL':
31
+ bank_code: 3!n
32
+ branch_code: 4!n
33
+ check_digit: 1!n
34
+ account_number: 16!n
35
+
36
+ 'AT':
37
+ bank_code: 5!n
38
+ account_number: 11!n
39
+
40
+ 'AZ':
41
+ bank_code: 4!c
42
+ account_number: 20!n
43
+
44
+ 'BA':
45
+ bank_code: 3!n
46
+ branch_code: 3!n
47
+ account_number: 8!n
48
+ check_digits: 2!n
49
+
50
+ 'BE':
51
+ bank_code: 3!n
52
+ account_number: 7!n
53
+ check_digits: 2!n
54
+
55
+ 'BG':
56
+ bank_code: 4!a
57
+ branch_code: 4!n
58
+ account_type: 2!n
59
+ account_number: 8!c
60
+
61
+ 'BH':
62
+ bank_code: 4!a
63
+ account_number: 14!c
64
+
65
+ 'CH':
66
+ bank_code: 5!n
67
+ account_number: 12!c
68
+
69
+ 'CY':
70
+ bank_code: 3!n
71
+ branch_code: 5!n
72
+ account_number: 16!c
73
+
74
+ 'CZ':
75
+ bank_code: 4!n
76
+ account_prefix: 6!n
77
+ account_number: 10!n
78
+
1
79
  'DE':
2
- blz: ['\d{8}', "%08d"]
3
- account_number: ['\d{10}', "%010d"]
80
+ blz: 8!n
81
+ account_number: 10!n
82
+
83
+ 'DK':
84
+ bank_code: 4!n
85
+ account_number: 10!n
86
+
87
+ 'DO':
88
+ bank_code: 4!a
89
+ account_number: 20!n
90
+
91
+ 'EE':
92
+ bank_code: 2!n
93
+ branch_code: 2!n
94
+ account_number: 10!n
95
+ check_digits: 2!n
4
96
 
5
97
  'ES':
6
- account_number: ['\d{20}', "%020d"]
98
+ account_number: 20!n
99
+
100
+ 'FI':
101
+ bank_code: 6!n
102
+ account_number: 7!n
103
+ check_digit: 1!n
104
+
105
+ 'FO':
106
+ bank_code: 4!n
107
+ account_number: 9!n
108
+ check_digit: 1!n
109
+
110
+ 'FR':
111
+ bank_code: 5!n
112
+ branch_code: 5!n
113
+ account_number: 11!c
114
+ check_digits: 2!n
115
+
116
+ 'GB':
117
+ bank_code: 4!a
118
+ branch_code: 6!n
119
+ account_number: 8!n
120
+
121
+ 'GE':
122
+ bank_code: 2!c
123
+ account_number: 16!n
124
+
125
+ 'GI':
126
+ bank_code: 4!a
127
+ account_number: 15!c
128
+
129
+ 'GL':
130
+ bank_code: 4!n
131
+ account_number: 10!n
132
+
133
+ 'GR':
134
+ bank_code: 3!n
135
+ branch_code: 4!n
136
+ account_number: 16!c
137
+
138
+ 'HR':
139
+ bank_code: 7!n
140
+ account_number: 10!n
141
+
142
+ 'HU':
143
+ bank_code: 3!n
144
+ branch_code: 4!n
145
+ account_prefix: 1!n
146
+ account_number: 15!n
147
+ check_digit: 1!n
148
+
149
+ 'IE':
150
+ bank_code: 4!c
151
+ branch_code: 6!c
152
+ account_number: 8n
153
+
154
+ 'IL':
155
+ bank_code: 3!n
156
+ branch_code: 3!n
157
+ account_number: 13!n
158
+
159
+ 'IS':
160
+ bank_code: 4!n
161
+ branch_code: 2!n
162
+ account_number: 6!n
163
+ kennitala: 10!n
164
+
165
+ 'IT':
166
+ check_char: 1!a
167
+ bank_code: 5!n
168
+ branch_code: 5!n
169
+ account_number: 12!c
170
+
171
+ 'JO':
172
+ bank_code: 4!a
173
+ branch_code: 4!n
174
+ zeros: 8!n
175
+ account_number: 10!n
176
+
177
+ 'KW':
178
+ bank_code: 4!a
179
+ account_number: 22!n
180
+
181
+ 'KZ':
182
+ bank_code: 3!n
183
+ account_number: 13!c
184
+
185
+ 'LB':
186
+ bank_code: 4!n
187
+ account_number: 20!c
188
+
189
+ 'LI':
190
+ bank_code: 5!n
191
+ account_number: 12!c
192
+
193
+ 'LT':
194
+ bank_code: 5!n
195
+ account_number: 11!n
196
+
197
+ 'LU':
198
+ bank_code: 3!n
199
+ account_number: 13!c
200
+
201
+ 'LV':
202
+ bank_code: 4!a
203
+ account_number: 13!c
204
+
205
+ 'MC':
206
+ bank_code: 5!n
207
+ branch_code: 5!n
208
+ account_number: 11!c
209
+ check_digits: 2!n
210
+
211
+ 'MD':
212
+ bank_code: 2!c
213
+ account_number: 18!n
214
+
215
+ 'ME':
216
+ bank_code: 3!n
217
+ account_number: 13!n
218
+ check_digits: 2!n
219
+
220
+ 'MK':
221
+ bank_code: 3!n
222
+ account_number: 10!c
223
+ check_digits: 2!n
224
+
225
+ 'MR':
226
+ bank_code: 5!n
227
+ branch_code: 5!n
228
+ account_number: 11!n
229
+ check_digits: 2!n
230
+
231
+ 'MT':
232
+ bank_code: 4!a
233
+ branch_code: 5!n
234
+ account_number: 18!c
235
+
236
+ 'MU':
237
+ bank_code: ['4!a', '2!n']
238
+ branch_code: 2!n
239
+ account_number: ['15!n', '3!a']
240
+
241
+ 'NL':
242
+ bank_code: 4!a
243
+ account_number: 10!n
244
+
245
+ 'NO':
246
+ # Don't remove the quotes here, NO == no == false in yaml.
247
+ bank_code: 4!n
248
+ account_number: 6!n
249
+ check_digit: 1!n
250
+
251
+ 'PK':
252
+ bank_code: 4!c
253
+ account_number: 16!n
254
+
255
+ 'PL':
256
+ bank_code: 3!n
257
+ branch_code: 4!n
258
+ check_digit: 1!n
259
+ account_number: 16!n
260
+
261
+ 'PS':
262
+ bank_code: 4!c
263
+ account_number: 21!n
264
+
265
+ 'PT':
266
+ bank_code: 4!n
267
+ branch_code: 4!n
268
+ account_number: 11!n
269
+ check_digits: 2!n
270
+
271
+ 'QA':
272
+ bank_code: 4!a
273
+ account_number: 21!c
274
+
275
+ 'RO':
276
+ bank_code: 4!a
277
+ account_number: 16!c
278
+
279
+ 'RS':
280
+ bank_code: 3!n
281
+ account_number: 13!n
282
+ check_digits: 2!n
283
+
284
+ 'SA':
285
+ bank_code: 2!n
286
+ account_number: 18!c
287
+
288
+ 'SE':
289
+ bank_code: 3!n
290
+ account_number: 16!n
291
+ check_digit: 1!n
292
+
293
+ 'SI':
294
+ bank_code: 2!n
295
+ branch_code: 3!n
296
+ account_number: 8!n
297
+ check_digits: 2!n
7
298
 
8
299
  'SK':
9
- bank_code: ['\d{4}', "%04d"]
10
- account_prefix: ['\d{6}', "%06d"]
11
- account_number: ['\d{10}', "%010d"]
300
+ bank_code: 4!n
301
+ account_prefix: 6!n
302
+ account_number: 10!n
303
+
304
+ 'SM':
305
+ check_char: 1!a
306
+ bank_code: 5!n
307
+ branch_code: 5!n
308
+ account_number: 12!c
309
+
310
+ 'TN':
311
+ bank_code: 2!n
312
+ branch_code: 3!n
313
+ account_number: 15!n
314
+
315
+ 'TR':
316
+ bank_code: 5!n
317
+ reserved: 1!c
318
+ account_number: 16!c
319
+
320
+ 'VA':
321
+ bank_code: 3!n
322
+ account_number: 15!n
323
+
324
+ 'VG':
325
+ bank_code: 4!c
326
+ account_number: 16!n
@@ -45,11 +45,36 @@
45
45
  length: 22
46
46
  bban_pattern: '[A-Z]{4}[A-Z0-9]{14}'
47
47
 
48
+ 'BR':
49
+ # Brazil
50
+ length: 29
51
+ bban_pattern: '\d{23}[A-Z0-9]{2}'
52
+
53
+ 'BY':
54
+ # Belarus
55
+ length: 28
56
+ bban_pattern: '[A-Z0-9]{4}\d{4}[A-Z0-9]{16}'
57
+
48
58
  'CH':
49
59
  # Switzerland
50
60
  length: 21
51
61
  bban_pattern: '\d{5}[A-Z0-9]{12}'
52
62
 
63
+ 'CI':
64
+ # Ivory Coast
65
+ length: 28
66
+ bban_pattern: '[A-Z0-9]{24}' # specification not clear
67
+
68
+ 'CR':
69
+ # Costa Rica
70
+ length: 22
71
+ bban_pattern: '\d{1}\d{3}\d{14}'
72
+
73
+ 'CV':
74
+ # Cape Verde
75
+ length: 25
76
+ bban_pattern: '\d{21}'
77
+
53
78
  'CY':
54
79
  # Cyprus
55
80
  length: 28
@@ -75,6 +100,11 @@
75
100
  length: 28
76
101
  bban_pattern: '[A-Z]{4}\d{20}'
77
102
 
103
+ 'EG':
104
+ # Egypt
105
+ length: 27
106
+ bban_pattern: '\d{23}' # specification not clear
107
+
78
108
  'EE':
79
109
  # Estonia
80
110
  length: 20
@@ -100,6 +130,11 @@
100
130
  length: 27
101
131
  bban_pattern: '\d{10}[A-Z0-9]{11}\d{2}'
102
132
 
133
+ 'GA':
134
+ # Gabon
135
+ length: 27
136
+ bban_pattern: '\d{23}' # specification not clear
137
+
103
138
  'GB':
104
139
  # United Kingdom
105
140
  length: 22
@@ -150,6 +185,11 @@
150
185
  length: 26
151
186
  bban_pattern: '\d{22}'
152
187
 
188
+ 'IQ':
189
+ # Iraq
190
+ length: 23
191
+ bban_pattern: '[A-Z]{4}[0-9]{15}'
192
+
153
193
  'IT':
154
194
  # Italy
155
195
  length: 27
@@ -210,6 +250,11 @@
210
250
  length: 22
211
251
  bban_pattern: '\d{18}'
212
252
 
253
+ 'MG':
254
+ # Madagascar
255
+ length: 27
256
+ bban_pattern: '\d{23}'
257
+
213
258
  'MK':
214
259
  # Macedonia
215
260
  length: 19
@@ -255,6 +300,11 @@
255
300
  length: 25
256
301
  bban_pattern: '\d{21}'
257
302
 
303
+ 'PS':
304
+ # Palestinian
305
+ length: 29
306
+ bban_pattern: '[A-Z]{4}[A-Z0-9]{21}'
307
+
258
308
  'QA':
259
309
  # Quatar
260
310
  length: 29
@@ -275,6 +325,11 @@
275
325
  length: 24
276
326
  bban_pattern: '\d{2}[A-Z0-9]{18}'
277
327
 
328
+ 'SC':
329
+ # Seychelles
330
+ length: 31
331
+ bban_pattern: '[A-Z]{4}[0-9]{20}[A-Z]{3}'
332
+
278
333
  'SE':
279
334
  # Sweden
280
335
  length: 24
@@ -295,6 +350,11 @@
295
350
  length: 27
296
351
  bban_pattern: '[A-Z]\d{10}[A-Z0-9]{12}'
297
352
 
353
+ 'SV':
354
+ # El Salvador
355
+ length: 28
356
+ bban_pattern: '[A-Z]{4}\d{20}'
357
+
298
358
  'TN':
299
359
  # Tunisia
300
360
  length: 24
@@ -305,3 +365,22 @@
305
365
  length: 26
306
366
  bban_pattern: '\d{5}[A-Z0-9]{17}'
307
367
 
368
+ 'UA':
369
+ # Ukraine
370
+ length: 29
371
+ bban_pattern: '\d{25}'
372
+
373
+ 'VA':
374
+ # Vatican City
375
+ length: 22
376
+ bban_pattern: '\d{18}'
377
+
378
+ 'VG':
379
+ # Virgin Islands
380
+ length: 24
381
+ bban_pattern: '[A-Z0-9]{4}\d{16}'
382
+
383
+ 'XK':
384
+ # Kosovo
385
+ length: 20
386
+ bban_pattern: '\d{4}\d{12}'
data/lib/iban-tools.rb CHANGED
@@ -22,6 +22,6 @@
22
22
  # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
23
  #++
24
24
 
25
- require 'iban-tools/conversion.rb'
26
- require 'iban-tools/iban.rb'
27
- require 'iban-tools/iban_rules.rb'
25
+ require 'iban-tools/conversion'
26
+ require 'iban-tools/iban'
27
+ require 'iban-tools/iban_rules'
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: iban-tools
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Iulian Dogariu
8
8
  - Tor Erik Linnerud
9
- autorequire:
9
+ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-09-23 00:00:00.000000000 Z
12
+ date: 2023-12-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -58,7 +58,7 @@ homepage: https://github.com/alphasights/iban-tools
58
58
  licenses:
59
59
  - MIT
60
60
  metadata: {}
61
- post_install_message:
61
+ post_install_message:
62
62
  rdoc_options: []
63
63
  require_paths:
64
64
  - lib
@@ -74,10 +74,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
74
74
  version: '0'
75
75
  requirements:
76
76
  - none
77
- rubyforge_project:
78
- rubygems_version: 2.2.2
79
- signing_key:
77
+ rubygems_version: 3.3.7
78
+ signing_key:
80
79
  specification_version: 4
81
80
  summary: IBAN validator
82
81
  test_files: []
83
- has_rdoc: