ibanvalidator 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e8535c8b85b64a210fbcc4a5492bf27f732afe16
4
+ data.tar.gz: 25dfcd0a7c08eaef1ca6605754faa77f0fa30166
5
+ SHA512:
6
+ metadata.gz: 0da7380c4341c84821a652189508a9e3dc54bafc8291e50a5f1a4835190985eb1f812b82f788019efc1036225928d147c1c21c3bedb2f66009b8e96473e928ce
7
+ data.tar.gz: ea1f77886949c4f20acd55bc911bd74f462778266ac0ad23a4a0ef071143ef02e9853ac21de00f2236c2b39da93e687c4440b29b2fad0db38ff83c72baf82d81
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.gem
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.15.4
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at TODO: Write your email address. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in ibanvalidator.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 TODO: Write your name
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,57 @@
1
+ # Ibanvalidator (under Construction)
2
+
3
+ Ibanvalidator ist eine Ruby Library zum überüfen von IBANs. [Mehr Infos zur IBAN](https://de.wikipedia.org/wiki/IBAN)
4
+
5
+ Es basiert auf der gem [iban-tools](http://github.com/iulianu/iban-tools) von [Iulianu](http://github.com/iulianu) die dann vom Team
6
+ bei [AlphaSights](https://engineering.alphasights.com) weiterentwickelt wurde.
7
+
8
+ Wir wollen diese gem weiterpflegen und zusätzliche Funtionen integrieren.
9
+
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'ibanvalidator'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install ibanvalidator
26
+
27
+ ## Usage
28
+
29
+ ### quick validation
30
+ require 'ibanvalidator'
31
+ Ibanvalidator::IBAN.valid?("DE89370400440532013000") => true
32
+
33
+ ### advanced
34
+ require 'ibanvalidator'
35
+ iban = Ibanvalidator::IBAN.new("DE89370 40044053201 3000")
36
+ iban.code => "DE89370400440532013000"
37
+ iban.bban => "370400440532013000"
38
+ iban.country_code => "DE"
39
+ iban.to_local => {bank_code: '37040044', account_number: '532013000'}
40
+ iban.check_digits => "89"
41
+
42
+
43
+ ## Development
44
+
45
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
46
+
47
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
48
+
49
+ ## Contributing
50
+
51
+ Bug reports and pull requests are welcome on GitHub at https://github.com/olafkaderka/ibanvalidator. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
52
+
53
+ ## License
54
+
55
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
56
+
57
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ibanvalidator"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,37 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "ibanvalidator/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ibanvalidator"
8
+ spec.version = Ibanvalidator::VERSION
9
+ spec.authors = ["Olaf Kaderka"]
10
+ spec.email = ["okaderka@yahoo.de"]
11
+
12
+ spec.summary = %q{Validate IBAN numbers based on iban-tool}
13
+ spec.description = %q{}
14
+ spec.homepage = "https://github.com/olafkaderka/ibanvalidator"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against " \
23
+ "public gem pushes."
24
+ end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_development_dependency "bundler", "~> 1.15"
34
+ spec.add_development_dependency "rake", "~> 10.0"
35
+ spec.add_development_dependency "rspec", "~> 3.0"
36
+
37
+ end
@@ -0,0 +1,115 @@
1
+ module Ibanvalidator
2
+ class Conversion
3
+
4
+ def self.local2iban(country_code, data)
5
+ config = load_config country_code
6
+
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')
17
+ end.join('')
18
+
19
+ check_digits = "%02d" % checksum(country_code, bban)
20
+
21
+ IBAN.new "#{country_code}#{check_digits}#{bban}"
22
+ end
23
+
24
+
25
+ def self.iban2local(country_code, bban)
26
+ config = load_config country_code
27
+
28
+ local = {}
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] == ''
38
+ end
39
+ local
40
+ end
41
+
42
+ private
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
+
102
+ def self.load_config(country_code)
103
+ default_config = YAML.
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
108
+ default_config[country_code]
109
+ end
110
+
111
+ def self.checksum(country_code, bban)
112
+ 97 - (IBAN.new("#{country_code}00#{bban}").numerify.to_i % 97) + 1
113
+ end
114
+ end
115
+ end
@@ -0,0 +1,325 @@
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
+
79
+ 'DE':
80
+ bank_code: 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
96
+
97
+ 'ES':
98
+ bank_code: 4!n
99
+ branch_code: 4!n
100
+ check_digits: 2!n
101
+ account_number: 8!n
102
+
103
+ 'FI':
104
+ bank_code: 6!n
105
+ account_number: 7!n
106
+ check_digit: 1!n
107
+
108
+ 'FO':
109
+ bank_code: 4!n
110
+ account_number: 9!n
111
+ check_digit: 1!n
112
+
113
+ 'FR':
114
+ bank_code: 5!n
115
+ branch_code: 5!n
116
+ account_number: 11!c
117
+ check_digits: 2!n
118
+
119
+ 'GB':
120
+ bank_code: 4!a
121
+ branch_code: 6!n
122
+ account_number: 8!n
123
+
124
+ 'GE':
125
+ bank_code: 2!c
126
+ account_number: 16!n
127
+
128
+ 'GI':
129
+ bank_code: 4!a
130
+ account_number: 15!c
131
+
132
+ 'GL':
133
+ bank_code: 4!n
134
+ account_number: 10!n
135
+
136
+ 'GR':
137
+ bank_code: 3!n
138
+ branch_code: 4!n
139
+ account_number: 16!c
140
+
141
+ 'HR':
142
+ bank_code: 7!n
143
+ account_number: 10!n
144
+
145
+ 'HU':
146
+ bank_code: 3!n
147
+ branch_code: 4!n
148
+ account_prefix: 1!n
149
+ account_number: 15!n
150
+ check_digit: 1!n
151
+
152
+ 'IE':
153
+ bank_code: 4!c
154
+ branch_code: 6!c
155
+ account_number: 8n
156
+
157
+ 'IL':
158
+ bank_code: 3!n
159
+ branch_code: 3!n
160
+ account_number: 13!n
161
+
162
+ 'IS':
163
+ bank_code: 4!n
164
+ branch_code: 2!n
165
+ account_number: 6!n
166
+ kennitala: 10!n
167
+
168
+ 'IT':
169
+ check_char: 1!a
170
+ bank_code: 5!n
171
+ branch_code: 5!n
172
+ account_number: 12!c
173
+
174
+ 'JO':
175
+ bank_code: 4!a
176
+ branch_code: 4!n
177
+ zeros: 8!n
178
+ account_number: 10!n
179
+
180
+ 'KW':
181
+ bank_code: 4!a
182
+ account_number: 22!n
183
+
184
+ 'KZ':
185
+ bank_code: 3!n
186
+ account_number: 13!c
187
+
188
+ 'LB':
189
+ bank_code: 4!n
190
+ account_number: 20!c
191
+
192
+ 'LI':
193
+ bank_code: 5!n
194
+ account_number: 12!c
195
+
196
+ 'LT':
197
+ bank_code: 5!n
198
+ account_number: 11!n
199
+
200
+ 'LU':
201
+ bank_code: 3!n
202
+ account_number: 13!c
203
+
204
+ 'LV':
205
+ bank_code: 4!a
206
+ account_number: 13!c
207
+
208
+ 'MC':
209
+ bank_code: 5!n
210
+ branch_code: 5!n
211
+ account_number: 11!c
212
+ check_digits: 2!n
213
+
214
+ 'MD':
215
+ bank_code: 2!c
216
+ account_number: 18!n
217
+
218
+ 'ME':
219
+ bank_code: 3!n
220
+ account_number: 13!n
221
+ check_digits: 2!n
222
+
223
+ 'MK':
224
+ bank_code: 3!n
225
+ account_number: 10!c
226
+ check_digits: 2!n
227
+
228
+ 'MR':
229
+ bank_code: 5!n
230
+ branch_code: 5!n
231
+ account_number: 11!n
232
+ check_digits: 2!n
233
+
234
+ 'MT':
235
+ bank_code: 4!a
236
+ branch_code: 5!n
237
+ account_number: 18!c
238
+
239
+ 'MU':
240
+ bank_code: ['4!a', '2!n']
241
+ branch_code: 2!n
242
+ account_number: ['15!n', '3!a']
243
+
244
+ 'NL':
245
+ bank_code: 4!a
246
+ account_number: 10!n
247
+
248
+ 'NO':
249
+ # Don't remove the quotes here, NO == no == false in yaml.
250
+ bank_code: 4!n
251
+ account_number: 6!n
252
+ check_digit: 1!n
253
+
254
+ 'PK':
255
+ bank_code: 4!c
256
+ account_number: 16!n
257
+
258
+ 'PL':
259
+ bank_code: 3!n
260
+ branch_code: 4!n
261
+ check_digit: 1!n
262
+ account_number: 16!n
263
+
264
+ 'PS':
265
+ bank_code: 4!c
266
+ account_number: 21!n
267
+
268
+ 'PT':
269
+ bank_code: 4!n
270
+ branch_code: 4!n
271
+ account_number: 11!n
272
+ check_digits: 2!n
273
+
274
+ 'QA':
275
+ bank_code: 4!a
276
+ account_number: 21!c
277
+
278
+ 'RO':
279
+ bank_code: 4!a
280
+ account_number: 16!c
281
+
282
+ 'RS':
283
+ bank_code: 3!n
284
+ account_number: 13!n
285
+ check_digits: 2!n
286
+
287
+ 'SA':
288
+ bank_code: 2!n
289
+ account_number: 18!c
290
+
291
+ 'SE':
292
+ bank_code: 3!n
293
+ account_number: 16!n
294
+ check_digit: 1!n
295
+
296
+ 'SI':
297
+ bank_code: 2!n
298
+ branch_code: 3!n
299
+ account_number: 8!n
300
+ check_digits: 2!n
301
+
302
+ 'SK':
303
+ bank_code: 4!n
304
+ account_prefix: 6!n
305
+ account_number: 10!n
306
+
307
+ 'SM':
308
+ check_char: 1!a
309
+ bank_code: 5!n
310
+ branch_code: 5!n
311
+ account_number: 12!c
312
+
313
+ 'TN':
314
+ bank_code: 2!n
315
+ branch_code: 3!n
316
+ account_number: 15!n
317
+
318
+ 'TR':
319
+ bank_code: 5!n
320
+ reserved: 1!c
321
+ account_number: 16!c
322
+
323
+ 'VG':
324
+ bank_code: 4!c
325
+ account_number: 16!n
@@ -0,0 +1,111 @@
1
+ #see https://de.wikipedia.org/wiki/IBAN
2
+ module Ibanvalidator
3
+ class IBAN
4
+
5
+ #attr_accessor :code, :bank, :country, :location, :branch
6
+
7
+ def initialize( code )
8
+ @code = IBAN.canonicalize_code(code)
9
+ end
10
+
11
+ # The code in canonical form,
12
+ # suitable for storing in a database
13
+ # or sending over the wire
14
+ def code
15
+ @code
16
+ end
17
+
18
+ def country_code
19
+ @code[0..1]
20
+ end
21
+
22
+ def check_digits
23
+ @code[2..3]
24
+ end
25
+
26
+ def bban
27
+ @code[4..-1]
28
+ end
29
+
30
+
31
+ def self.valid?( code, rules = nil )
32
+ new(code).validation_errors(rules).empty?
33
+ end
34
+
35
+ def self.canonicalize_code( code )
36
+ code.to_s.strip.gsub(/\s+/, '').upcase
37
+ end
38
+
39
+ # Load and cache the default rules from rules.yml
40
+ def self.default_rules
41
+ @default_rules ||= IBANRules.defaults
42
+ end
43
+
44
+ def self.from_local(country_code, data)
45
+ Conversion.local2iban country_code, data
46
+ end
47
+
48
+
49
+ def to_local
50
+ Conversion.iban2local country_code, bban
51
+ end
52
+
53
+ def to_s
54
+ "#<#{self.class}: #{prettify}>"
55
+ end
56
+
57
+ # The IBAN code in a human-readable format
58
+ def prettify
59
+ @code.gsub(/(.{4})/, '\1 ').strip
60
+ end
61
+
62
+ def validation_errors( rules = nil )
63
+ errors = []
64
+ return [:too_short] if @code.size < 5
65
+ return [:too_long] if @code.size > 34
66
+ return [:bad_chars] unless @code =~ /^[A-Z0-9]+$/
67
+ errors += validation_errors_against_rules( rules || IBAN.default_rules )
68
+ errors << :bad_check_digits unless valid_check_digits?
69
+ errors
70
+ end
71
+
72
+
73
+ def validation_errors_against_rules( rules )
74
+ errors = []
75
+ return [:unknown_country_code] if rules[country_code].nil?
76
+ errors << :bad_length if rules[country_code]["length"] != @code.size
77
+ errors << :bad_format unless bban =~ rules[country_code]["bban_pattern"]
78
+ errors
79
+ end
80
+
81
+
82
+ ########### Pruefdsummen siehe https://de.wikipedia.org/wiki/IBAN#Validierung_der_Pr.C3.BCfsum
83
+ #Nun wird der Rest berechnet, der sich beim ganzzahligen Teilen der Zahl durch 97 ergibt (Modulo 97).
84
+ def valid_check_digits?
85
+ ##Das Ergebnis muss 1 sein, ansonsten ist die IBAN falsch.
86
+ numerify.to_i % 97 == 1
87
+ end
88
+
89
+ def numerify
90
+ #Diese setzt sich aus
91
+ #BBAN (in Deutschland z. B. 18 Stellen) + Länderkürzel kodiert + Prüfsumme zusammen.
92
+ #Dabei werden die beiden Buchstaben des Länderkürzels sowie weitere etwa in der Kontonummer enthaltene Buchstaben durch ihre Position im lateinischen Alphabet + 9 ersetzt
93
+ #(A = 10, B = 11, …, Z = 35).
94
+ numerified = ""
95
+ (@code[4..-1] + @code[0..3]).each_byte do |byte|
96
+ numerified += case byte
97
+ # 0..9
98
+ when 48..57 then byte.chr
99
+ # 'A'..'Z'
100
+ when 65..90 then (byte - 55).to_s # 55 = 'A'.ord + 10
101
+ else
102
+ raise RuntimeError.new("Unexpected byte '#{byte}' in IBAN code '#{prettify}'")
103
+ end
104
+ end
105
+ numerified
106
+ end
107
+
108
+ ######################## Pruefsummen
109
+
110
+ end
111
+ end
@@ -0,0 +1,32 @@
1
+ # vim:ts=2:sw=2:et:
2
+
3
+ require 'yaml'
4
+
5
+ module Ibanvalidator
6
+
7
+ class IBANRules
8
+
9
+ def initialize( rules = {} )
10
+ @rules = rules
11
+ end
12
+
13
+ def [](key)
14
+ @rules[key]
15
+ end
16
+
17
+ def self.defaults
18
+ load_from_string( File.read(File.dirname(__FILE__) + "/rules.yml") )
19
+ end
20
+
21
+ def self.load_from_string( string )
22
+ rule_hash = YAML.load(string)
23
+ rule_hash.each do |country_code, specs|
24
+ specs["bban_pattern"] = Regexp.new("^" + specs["bban_pattern"] + "$")
25
+ end
26
+ IBANRules.new(rule_hash)
27
+ end
28
+
29
+ end
30
+
31
+ end
32
+
@@ -0,0 +1,326 @@
1
+ # Data from http://www.tbg5-finance.org/?ibandocs.shtml/
2
+
3
+ 'AD':
4
+ # Andorra
5
+ length: 24
6
+ bban_pattern: '\d{8}[A-Z0-9]{12}'
7
+
8
+ 'AE':
9
+ # United Arab Emirates
10
+ length: 23
11
+ bban_pattern: '\d{19}'
12
+
13
+ 'AL':
14
+ # Albania
15
+ length: 28
16
+ bban_pattern: '\d{8}[A-Z0-9]{16}'
17
+
18
+ 'AT':
19
+ # Austria
20
+ length: 20
21
+ bban_pattern: '\d{16}'
22
+
23
+ 'AZ':
24
+ # Azerbaijan
25
+ length: 28
26
+ bban_pattern: '[A-Z]{4}[A-Z0-9]{20}'
27
+
28
+ 'BA':
29
+ # Bosnia
30
+ length: 20
31
+ bban_pattern: '\d{16}'
32
+
33
+ 'BE':
34
+ # Belgium
35
+ length: 16
36
+ bban_pattern: '\d{12}'
37
+
38
+ 'BG':
39
+ # Bulgaria
40
+ length: 22
41
+ bban_pattern: '[A-Z]{4}\d{6}[A-Z0-9]{8}'
42
+
43
+ 'BH':
44
+ # Bahrain
45
+ length: 22
46
+ bban_pattern: '[A-Z]{4}[A-Z0-9]{14}'
47
+
48
+ 'BR':
49
+ # Brazil
50
+ length: 29
51
+ bban_pattern: '\d{23}[A-Z0-9]{2}'
52
+
53
+ 'CH':
54
+ # Switzerland
55
+ length: 21
56
+ bban_pattern: '\d{5}[A-Z0-9]{12}'
57
+
58
+ 'CY':
59
+ # Cyprus
60
+ length: 28
61
+ bban_pattern: '\d{8}[A-Z0-9]{16}'
62
+
63
+ 'CZ':
64
+ # Czech Republic
65
+ length: 24
66
+ bban_pattern: '\d{20}'
67
+
68
+ 'DE':
69
+ # Germany
70
+ length: 22
71
+ bban_pattern: '\d{18}'
72
+
73
+ 'DK':
74
+ # Denmark
75
+ length: 18
76
+ bban_pattern: '\d{14}'
77
+
78
+ 'DO':
79
+ # Dominican Republic
80
+ length: 28
81
+ bban_pattern: '[A-Z]{4}\d{20}'
82
+
83
+ 'EE':
84
+ # Estonia
85
+ length: 20
86
+ bban_pattern: '\d{16}'
87
+
88
+ 'ES':
89
+ # Spain
90
+ length: 24
91
+ bban_pattern: '\d{20}'
92
+
93
+ 'FI':
94
+ # Finland
95
+ length: 18
96
+ bban_pattern: '\d{14}'
97
+
98
+ 'FO':
99
+ # Faroe Islands
100
+ length: 18
101
+ bban_pattern: '\d{14}'
102
+
103
+ 'FR':
104
+ # France
105
+ length: 27
106
+ bban_pattern: '\d{10}[A-Z0-9]{11}\d{2}'
107
+
108
+ 'GB':
109
+ # United Kingdom
110
+ length: 22
111
+ bban_pattern: '[A-Z]{4}\d{14}'
112
+
113
+ 'GE':
114
+ # Georgia
115
+ length: 22
116
+ bban_pattern: '[A-Z]{2}\d{16}'
117
+
118
+ 'GI':
119
+ # Gibraltar
120
+ length: 23
121
+ bban_pattern: '[A-Z]{4}[A-Z0-9]{15}'
122
+
123
+ 'GL':
124
+ # Greenland
125
+ length: 18
126
+ bban_pattern: '\d{14}'
127
+
128
+ 'GR':
129
+ # Greece
130
+ length: 27
131
+ bban_pattern: '\d{7}[A-Z0-9]{16}'
132
+
133
+ 'HR':
134
+ # Croatia
135
+ length: 21
136
+ bban_pattern: '\d{17}'
137
+
138
+ 'HU':
139
+ # Hungary
140
+ length: 28
141
+ bban_pattern: '\d{24}'
142
+
143
+ 'IE':
144
+ # Ireland
145
+ length: 22
146
+ bban_pattern: '[A-Z]{4}\d{14}'
147
+
148
+ 'IL':
149
+ # Israel
150
+ length: 23
151
+ bban_pattern: '\d{19}'
152
+
153
+ 'IS':
154
+ # Iceland
155
+ length: 26
156
+ bban_pattern: '\d{22}'
157
+
158
+ 'IT':
159
+ # Italy
160
+ length: 27
161
+ bban_pattern: '[A-Z]\d{10}[A-Z0-9]{12}'
162
+
163
+ 'JO':
164
+ # Jordan
165
+ length: 30
166
+ bban_pattern: '[A-Z]{4}\d{4}[A-Z0-9]{18}'
167
+
168
+ 'KW':
169
+ # Kuwait
170
+ length: 30
171
+ bban_pattern: '[A-Z]{4}\d{22}'
172
+
173
+ 'KZ':
174
+ # Kazakhstan
175
+ length: 20
176
+ bban_pattern: '\d{3}[A-Z0-9]{13}'
177
+
178
+ 'LB':
179
+ # Lebanon
180
+ length: 28
181
+ bban_pattern: '\d{4}[A-Z0-9]{20}'
182
+
183
+ 'LI':
184
+ # Liechtenstein
185
+ length: 21
186
+ bban_pattern: '\d{5}[A-Z0-9]{12}'
187
+
188
+ 'LT':
189
+ # Lithuania
190
+ length: 20
191
+ bban_pattern: '\d{16}'
192
+
193
+ 'LU':
194
+ # Luxembourg
195
+ length: 20
196
+ bban_pattern: '\d{3}[A-Z0-9]{13}'
197
+
198
+ 'LV':
199
+ # Latvia
200
+ length: 21
201
+ bban_pattern: '[A-Z]{4}[A-Z0-9]{13}'
202
+
203
+ 'MC':
204
+ # Monaco
205
+ length: 27
206
+ bban_pattern: '\d{10}[A-Z0-9]{11}\d{2}'
207
+
208
+ 'MD':
209
+ # Moldova
210
+ length: 24
211
+ bban_pattern: '[A-Z0-9]{20}'
212
+
213
+ 'ME':
214
+ # Montenegro
215
+ length: 22
216
+ bban_pattern: '\d{18}'
217
+
218
+ 'MK':
219
+ # Macedonia
220
+ length: 19
221
+ bban_pattern: '\d{3}[A-Z0-9]{10}\d{2}'
222
+
223
+ 'MR':
224
+ # Mauritania
225
+ length: 27
226
+ bban_pattern: '\d{23}'
227
+
228
+ 'MT':
229
+ # Malta
230
+ length: 31
231
+ bban_pattern: '[A-Z]{4}\d{5}[A-Z0-9]{18}'
232
+
233
+ 'MU':
234
+ # Mauritius
235
+ length: 30
236
+ bban_pattern: '[A-Z]{4}\d{19}[A-Z]{3}'
237
+
238
+ 'NL':
239
+ # Netherlands
240
+ length: 18
241
+ bban_pattern: '[A-Z]{4}\d{10}'
242
+
243
+ 'NO':
244
+ # Norway
245
+ length: 15
246
+ bban_pattern: '\d{11}'
247
+
248
+ 'PK':
249
+ # Pakistan
250
+ length: 24
251
+ bban_pattern: '[A-Z]{4}[A-Z0-9]{16}'
252
+
253
+ 'PL':
254
+ # Poland
255
+ length: 28
256
+ bban_pattern: '\d{8}[A-Z0-9]{16}'
257
+
258
+ 'PT':
259
+ # Portugal
260
+ length: 25
261
+ bban_pattern: '\d{21}'
262
+
263
+ 'PS':
264
+ # Palestinian
265
+ length: 29
266
+ bban_pattern: '[A-Z0-9]{4}\d{21}'
267
+
268
+ 'QA':
269
+ # Quatar
270
+ length: 29
271
+ bban_pattern: '[A-Z]{4}[A-Z0-9]{21}'
272
+
273
+ 'RO':
274
+ # Romania
275
+ length: 24
276
+ bban_pattern: '[A-Z]{4}[A-Z0-9]{16}'
277
+
278
+ 'RS':
279
+ # Serbia
280
+ length: 22
281
+ bban_pattern: '\d{18}'
282
+
283
+ 'SA':
284
+ # Saudi Arabia
285
+ length: 24
286
+ bban_pattern: '\d{2}[A-Z0-9]{18}'
287
+
288
+ 'SE':
289
+ # Sweden
290
+ length: 24
291
+ bban_pattern: '\d{20}'
292
+
293
+ 'SI':
294
+ # Slovenia
295
+ length: 19
296
+ bban_pattern: '\d{15}'
297
+
298
+ 'SK':
299
+ # Slovakia
300
+ length: 24
301
+ bban_pattern: '\d{20}'
302
+
303
+ 'SM':
304
+ # San Marino
305
+ length: 27
306
+ bban_pattern: '[A-Z]\d{10}[A-Z0-9]{12}'
307
+
308
+ 'TN':
309
+ # Tunisia
310
+ length: 24
311
+ bban_pattern: '\d{20}'
312
+
313
+ 'TR':
314
+ # Turkey
315
+ length: 26
316
+ bban_pattern: '\d{5}[A-Z0-9]{17}'
317
+
318
+ 'UA':
319
+ # Ukraine
320
+ length: 29
321
+ bban_pattern: '\d{25}'
322
+
323
+ 'VG':
324
+ # Virgin Islands
325
+ length: 24
326
+ bban_pattern: '[A-Z0-9]{4}\d{16}'
@@ -0,0 +1,3 @@
1
+ module Ibanvalidator
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,4 @@
1
+ require "ibanvalidator/version"
2
+ require 'ibanvalidator/conversion.rb'
3
+ require 'ibanvalidator/iban.rb'
4
+ require 'ibanvalidator/iban_rules.rb'
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ibanvalidator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Olaf Kaderka
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-10-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: ''
56
+ email:
57
+ - okaderka@yahoo.de
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - CODE_OF_CONDUCT.md
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - ibanvalidator.gemspec
73
+ - lib/ibanvalidator.rb
74
+ - lib/ibanvalidator/conversion.rb
75
+ - lib/ibanvalidator/conversion_rules.yml
76
+ - lib/ibanvalidator/iban.rb
77
+ - lib/ibanvalidator/iban_rules.rb
78
+ - lib/ibanvalidator/rules.yml
79
+ - lib/ibanvalidator/version.rb
80
+ homepage: https://github.com/olafkaderka/ibanvalidator
81
+ licenses:
82
+ - MIT
83
+ metadata:
84
+ allowed_push_host: https://rubygems.org
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 2.6.13
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: Validate IBAN numbers based on iban-tool
105
+ test_files: []