nz_bank_account_validator 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/CHANGELOG.txt +8 -0
- data/Gemfile +3 -0
- data/LICENSE.md +21 -0
- data/README.md +42 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +6 -0
- data/lib/nz_bank_account_validator.rb +132 -0
- data/lib/nz_bank_account_validator/version.rb +3 -0
- data/validate_nz_bank_acc.gemspec +23 -0
- metadata +100 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c851a0b46247ae865d59bac22f1c982b58cc1422
|
4
|
+
data.tar.gz: 8afecdcd65e79a97950894874a70b21144babb11
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9b51753e2f12832625ae6bc7d3f698a2c791a60e1a7be4a9e3af96a55d173bc1b16082c61d7b242df40e23fdcfdc92cbe240fa5236674dce1219e90ad99072b2
|
7
|
+
data.tar.gz: edb846b0d34f07dc14232f64e4596f7f2ed190c86f76d31dd57dd38ebde1ead06bd2de2a04596ddabeba5a70a735d2bccf19e09c1e7e73381713d832b2e8431e
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CHANGELOG.txt
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
0.9.0 - Add new ranges from updated IRD document
|
2
|
+
Use new RSpec syntax
|
3
|
+
Accept a string of a full bank account number rather than a collection of parts
|
4
|
+
Add class method to validate without instantiating
|
5
|
+
Rename gem
|
6
|
+
0.0.3 - Bugfix
|
7
|
+
0.0.2 - Never happened
|
8
|
+
0.0.1 - Initial Release
|
data/Gemfile
ADDED
data/LICENSE.md
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Steve Hoeksema
|
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,42 @@
|
|
1
|
+
# NzBankAccountValidator
|
2
|
+
|
3
|
+
An implementation of the process described on page 10 of the [IRD RWT - NRWT 2008 Specification](https://www.ird.govt.nz/resources/d/8/d8e49dce-1bda-4875-8acf-9ebf908c6e17/rwt-nrwt-spec-2014.pdf).
|
4
|
+
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem "nz_bank_account_validator"
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
gem install nz_bank_account_validator
|
21
|
+
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
NzBankAccountValidator.valid?(string)
|
26
|
+
|
27
|
+
|
28
|
+
## Development
|
29
|
+
|
30
|
+
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.
|
31
|
+
|
32
|
+
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).
|
33
|
+
|
34
|
+
|
35
|
+
## Contributing
|
36
|
+
|
37
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/steveh/nz_bank_account_validator.
|
38
|
+
|
39
|
+
|
40
|
+
## License
|
41
|
+
|
42
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "nz_bank_account_validator"
|
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
|
data/bin/setup
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
require "nz_bank_account_validator/version"
|
2
|
+
|
3
|
+
# frozen_string_literal: true
|
4
|
+
class NzBankAccountValidator
|
5
|
+
PATTERN = /\A^(?<bank_id>\d{1,2})[- ]?(?<bank_branch>\d{1,4})[- ]?(?<account_base_number>\d{1,8})[- ]?(?<account_suffix>\d{1,4})\z/
|
6
|
+
|
7
|
+
RADIX = 10
|
8
|
+
|
9
|
+
CHECKSUM_DIGITS = 18
|
10
|
+
|
11
|
+
ACCOUNT_BASE_NUMBER_CUTOFF = 990000
|
12
|
+
|
13
|
+
BANKS = { 1 => { ranges: [1..999, 1100..1199, 1800..1899] },
|
14
|
+
2 => { ranges: [1..999, 1200..1299] },
|
15
|
+
3 => { ranges: [1..999, 1300..1399, 1500..1599, 1700..1799, 1900..1999] },
|
16
|
+
6 => { ranges: [1..999, 1400..1499] },
|
17
|
+
8 => { ranges: [6500..6599], algo: :d },
|
18
|
+
9 => { ranges: [], algo: :e }, # range was 0000
|
19
|
+
11 => { ranges: [5000..6499, 6600..8999] },
|
20
|
+
12 => { ranges: [3000..3299, 3400..3499, 3600..3699] },
|
21
|
+
13 => { ranges: [4900..4999] },
|
22
|
+
14 => { ranges: [4700..4799] },
|
23
|
+
15 => { ranges: [3900..3999] },
|
24
|
+
16 => { ranges: [4400..4499] },
|
25
|
+
17 => { ranges: [3300..3399] },
|
26
|
+
18 => { ranges: [3500..3599] },
|
27
|
+
19 => { ranges: [4600..4649] },
|
28
|
+
20 => { ranges: [4100..4199] },
|
29
|
+
21 => { ranges: [4800..4899] },
|
30
|
+
22 => { ranges: [4000..4049] },
|
31
|
+
23 => { ranges: [3700..3799] },
|
32
|
+
24 => { ranges: [4300..4349] },
|
33
|
+
25 => { ranges: [2500..2599], algo: :f },
|
34
|
+
26 => { ranges: [2600..2699], algo: :g },
|
35
|
+
27 => { ranges: [3800..3849] },
|
36
|
+
28 => { ranges: [2100..2149], algo: :g },
|
37
|
+
29 => { ranges: [2150..2299], algo: :g },
|
38
|
+
30 => { ranges: [2900..2949] },
|
39
|
+
31 => { ranges: [2800..2849], algo: :x },
|
40
|
+
33 => { ranges: [6700..6799], algo: :f },
|
41
|
+
35 => { ranges: [2400..2499] },
|
42
|
+
38 => { ranges: [9000..9499] } }.freeze
|
43
|
+
|
44
|
+
ALGOS = {
|
45
|
+
a: [0, 0, 6, 3, 7, 9, 0, 0, 10, 5, 8, 4, 2, 1, 0, 0, 0, 0, 11],
|
46
|
+
b: [0, 0, 0, 0, 0, 0, 0, 0, 10, 5, 8, 4, 2, 1, 0, 0, 0, 0, 11],
|
47
|
+
c: [3, 7, 0, 0, 0, 0, 9, 1, 10, 5, 3, 4, 2, 1, 0, 0, 0, 0, 11],
|
48
|
+
d: [0, 0, 0, 0, 0, 0, 0, 7, 6, 5, 4, 3, 2, 1, 0, 0, 0, 0, 11],
|
49
|
+
e: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 4, 3, 2, 0, 0, 0, 1, 11],
|
50
|
+
f: [0, 0, 0, 0, 0, 0, 0, 1, 7, 3, 1, 7, 3, 1, 0, 0, 0, 0, 10],
|
51
|
+
g: [0, 0, 0, 0, 0, 0, 0, 1, 3, 7, 1, 3, 7, 1, 0, 3, 7, 1, 10],
|
52
|
+
x: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
|
53
|
+
}.freeze
|
54
|
+
|
55
|
+
def self.valid?(string)
|
56
|
+
new(string).valid?
|
57
|
+
end
|
58
|
+
|
59
|
+
def initialize(string)
|
60
|
+
match = string.match(PATTERN)
|
61
|
+
|
62
|
+
if match
|
63
|
+
@bank_id = Integer(match[:bank_id], RADIX)
|
64
|
+
@bank_branch = Integer(match[:bank_branch], RADIX)
|
65
|
+
@account_base_number = Integer(match[:account_base_number], RADIX)
|
66
|
+
@account_suffix = Integer(match[:account_suffix], RADIX)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
attr_accessor :bank_id, :bank_branch, :account_base_number, :account_suffix
|
71
|
+
|
72
|
+
def valid?
|
73
|
+
return false unless valid_bank_id?
|
74
|
+
return false unless valid_bank_branch?
|
75
|
+
return false unless account_base_number
|
76
|
+
return false unless account_suffix
|
77
|
+
return false unless valid_modulo?
|
78
|
+
true
|
79
|
+
end
|
80
|
+
|
81
|
+
def valid_bank_id?
|
82
|
+
return false unless bank_id
|
83
|
+
|
84
|
+
BANKS.key?(bank_id)
|
85
|
+
end
|
86
|
+
|
87
|
+
def valid_bank_branch?
|
88
|
+
return false unless valid_bank_id?
|
89
|
+
return false unless bank_branch
|
90
|
+
|
91
|
+
if BANKS[bank_id][:ranges].empty?
|
92
|
+
# Bank 9. Anything is valid? # TODO: confirm
|
93
|
+
true
|
94
|
+
else
|
95
|
+
BANKS[bank_id][:ranges].any? do |range|
|
96
|
+
range.include?(bank_branch)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
def valid_modulo?
|
102
|
+
(checksum % algo[CHECKSUM_DIGITS]).zero?
|
103
|
+
end
|
104
|
+
|
105
|
+
def algo_code
|
106
|
+
# If the account base number is below 00990000 then apply algorithm A
|
107
|
+
# Otherwise apply algorithm B.
|
108
|
+
BANKS[bank_id][:algo] || (account_base_number < ACCOUNT_BASE_NUMBER_CUTOFF ? :a : :b)
|
109
|
+
end
|
110
|
+
|
111
|
+
def algo
|
112
|
+
ALGOS[algo_code]
|
113
|
+
end
|
114
|
+
|
115
|
+
def number_for_checksum
|
116
|
+
format("%02d%04d%08d%04d", bank_id, bank_branch, account_base_number, account_suffix)
|
117
|
+
end
|
118
|
+
|
119
|
+
def checksum
|
120
|
+
if [:e, :g].include?(algo_code)
|
121
|
+
(0...CHECKSUM_DIGITS).inject(0) do |sum, index|
|
122
|
+
s = number_for_checksum[index].to_i * algo[index]
|
123
|
+
2.times { s = s.to_s.chars.map(&:to_i).inject(:+) }
|
124
|
+
sum + s
|
125
|
+
end
|
126
|
+
else
|
127
|
+
(0...CHECKSUM_DIGITS).inject(0) do |sum, index|
|
128
|
+
sum + number_for_checksum[index].to_i * algo[index]
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "nz_bank_account_validator/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "nz_bank_account_validator"
|
8
|
+
spec.version = NzBankAccountValidator::VERSION
|
9
|
+
spec.authors = ["Steve Hoeksema", "Eaden McKee"]
|
10
|
+
spec.email = ["steve@kotiri.com"]
|
11
|
+
spec.summary = "Validate a New Zealand bank account number according to IRD specifications"
|
12
|
+
spec.description = "Validate a New Zealand bank account number according to IRD specifications"
|
13
|
+
spec.homepage = "https://github.com/steveh/nz_bank_account_validator"
|
14
|
+
spec.license = "MIT"
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
16
|
+
spec.bindir = "exe"
|
17
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler"
|
21
|
+
spec.add_development_dependency "rake"
|
22
|
+
spec.add_development_dependency "rspec"
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nz_bank_account_validator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.9.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Steve Hoeksema
|
8
|
+
- Eaden McKee
|
9
|
+
autorequire:
|
10
|
+
bindir: exe
|
11
|
+
cert_chain: []
|
12
|
+
date: 2016-08-30 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - ">="
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '0'
|
21
|
+
type: :development
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '0'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: rake
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: rspec
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
description: Validate a New Zealand bank account number according to IRD specifications
|
57
|
+
email:
|
58
|
+
- steve@kotiri.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- ".rspec"
|
65
|
+
- ".travis.yml"
|
66
|
+
- CHANGELOG.txt
|
67
|
+
- Gemfile
|
68
|
+
- LICENSE.md
|
69
|
+
- README.md
|
70
|
+
- Rakefile
|
71
|
+
- bin/console
|
72
|
+
- bin/setup
|
73
|
+
- lib/nz_bank_account_validator.rb
|
74
|
+
- lib/nz_bank_account_validator/version.rb
|
75
|
+
- validate_nz_bank_acc.gemspec
|
76
|
+
homepage: https://github.com/steveh/nz_bank_account_validator
|
77
|
+
licenses:
|
78
|
+
- MIT
|
79
|
+
metadata: {}
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options: []
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
requirements: []
|
95
|
+
rubyforge_project:
|
96
|
+
rubygems_version: 2.5.1
|
97
|
+
signing_key:
|
98
|
+
specification_version: 4
|
99
|
+
summary: Validate a New Zealand bank account number according to IRD specifications
|
100
|
+
test_files: []
|