validate_nz_bank_acc 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in validate_nz_bank_acc.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Localist Ltd.
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,30 @@
1
+ # ValidateNzBankAcc
2
+
3
+ An implimentation of the process described on page 15 of
4
+ http://www.ird.govt.nz/resources/a/c/ac60890040b57d0e9bd4df41f9f3ce1d/rwt-nrwt-spec-2010-v3.pdf
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'validate_nz_bank_acc'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install validate_nz_bank_acc
19
+
20
+ ## Usage
21
+
22
+ ValidateNzBankAcc.new(bank_id, branch_id, account_number, suffix).valid?
23
+
24
+ ## Contributing
25
+
26
+ 1. Fork it
27
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
28
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
29
+ 4. Push to the branch (`git push origin my-new-feature`)
30
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,3 @@
1
+ class ValidateNzBankAcc
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,124 @@
1
+ require "validate_nz_bank_acc/version"
2
+
3
+ class ValidateNzBankAcc
4
+
5
+ BANKS = { 1 => {:ranges => [1..999, 1100...1199]},
6
+ 2 => {:ranges => [1..999, 1200.1299]},
7
+ 3 => {:ranges => [1..999, 1300..1399, 1500..1599, 1700..1799, 1900..1999]},
8
+ 6 => {:ranges => [1..999, 1400..1499]},
9
+ 8 => {:ranges => [6500..6599], :algo => :d},
10
+ 9 => {:ranges => [], :algo => :e}, # range was 0000
11
+ 11 => {:ranges => [5000..6499, 6600..8999]},
12
+ 12 => {:ranges => [3000..3299, 3400..3499,3600..3699]},
13
+ 13 => {:ranges => [4900..4999]},
14
+ 14 => {:ranges => [4700..4799]},
15
+ 15 => {:ranges => [3900..3999]},
16
+ 16 => {:ranges => [4400..4499]},
17
+ 17 => {:ranges => [3300..3399]},
18
+ 18 => {:ranges => [3500..3599]},
19
+ 19 => {:ranges => [4600..4649]},
20
+ 20 => {:ranges => [4100..4199]},
21
+ 22 => {:ranges => [4000..4049]},
22
+ 23 => {:ranges => [3700..3799]},
23
+ 24 => {:ranges => [4300..4349]},
24
+ 25 => {:ranges => [2500..2599], :algo => :f},
25
+ 26 => {:ranges => [2600..2699], :algo => :g},
26
+ 27 => {:ranges => [3800..3849]},
27
+ 28 => {:ranges => [2100..2149], :algo => :g},
28
+ 29 => {:ranges => [2150..2299], :algo => :g},
29
+ 30 => {:ranges => [2900..2949]},
30
+ 31 => {:ranges => [2800..2849], :algo => :x},
31
+ 33 => {:ranges => [6700..6799], :algo => :f},
32
+ 35 => {:ranges => [2400..2499]},
33
+ 38 => {:ranges => [9000..9499]}
34
+ }
35
+
36
+ ALGOS = {
37
+ :a =>[0, 0, 6, 3, 7, 9, 0, 0, 10, 5, 8, 4, 2, 1, 0, 0, 0, 0, 11],
38
+ :b => [0, 0, 0, 0, 0, 0, 0, 0, 10, 5, 8, 4, 2, 1, 0, 0, 0, 0, 11],
39
+ :c => [3, 7, 0, 0, 0, 0, 9, 1, 10, 5, 3, 4, 2, 1, 0, 0, 0, 0, 11],
40
+ :d => [0, 0, 0, 0, 0, 0, 0, 7, 6, 5, 4, 3, 2, 1, 0, 0, 0, 0, 11],
41
+ :e => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 4, 3, 2, 0, 0, 0, 1, 11],
42
+ :f => [ 0, 0, 0, 0, 0, 0, 0, 1, 7, 3, 1, 7, 3, 1, 0, 0, 0, 0, 10],
43
+ :g => [0, 0, 0, 0, 0, 0, 0, 1, 3, 7, 1, 3, 7, 1, 0, 3, 7, 1, 10],
44
+ :x => [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
45
+ }
46
+ attr_accessor :bank_id, :branch_id, :account_number, :suffix
47
+ def initialize(bank_id, branch_id, account_number, suffix)
48
+ @bank_id = process_input(bank_id)
49
+ @branch_id = process_input(branch_id)
50
+ @account_number = process_input(account_number)
51
+ @suffix = process_input(suffix)
52
+ end
53
+
54
+ def valid?
55
+ valid_branch_code? && valid_modulo?
56
+ end
57
+
58
+ def valid_bank_code?
59
+ BANKS.has_key? bank_id
60
+ end
61
+
62
+ def valid_branch_code?
63
+ return false unless valid_bank_code?
64
+ if BANKS[bank_id][:ranges].empty?
65
+ # Bank 9. Anything is valid? # DOUBLE CHECK
66
+ true
67
+ else
68
+ BANKS[bank_id][:ranges].any? do |range|
69
+ range.include? branch_id
70
+ end
71
+ end
72
+ end
73
+
74
+ def algo_code
75
+ # If the account base number is below 00990000 then apply algorithm A, otherwise apply algorithm B.
76
+ BANKS[bank_id][:algo] || (account_number < 990000 ? :a : :b)
77
+ end
78
+
79
+ def algo
80
+ ALGOS[algo_code]
81
+ end
82
+
83
+ def checksum_sum
84
+ if [:e, :g].include? algo_code
85
+ (0..17).inject(0) do |sum, index|
86
+ s = number[index].to_i * algo[index]
87
+ 2.times { s = s.to_s.chars.map(&:to_i).inject(:+) }
88
+ sum += s
89
+ end
90
+ else
91
+ (0..17).inject(0) {|sum, index| sum += number[index].to_i * algo[index]; sum }
92
+ end
93
+ end
94
+
95
+ def valid_modulo?
96
+ checksum_sum % algo[18] == 0
97
+ end
98
+
99
+ def number # account number padded to 8 chars
100
+ bank_code + branch_code + account_number_code + suffix_code
101
+ end
102
+
103
+ private
104
+
105
+ def bank_code
106
+ @bank_id.to_s.rjust(2,'0')
107
+ end
108
+
109
+ def branch_code
110
+ @branch_id.to_s.rjust(4,'0')
111
+ end
112
+
113
+ def account_number_code
114
+ @account_number.to_s.rjust(8,'0')
115
+ end
116
+
117
+ def suffix_code
118
+ @suffix.to_s.rjust(4,'0')
119
+ end
120
+
121
+ def process_input(num)
122
+ num.to_s.gsub(/[^0-9]/,'').to_i
123
+ end
124
+ end
@@ -0,0 +1 @@
1
+ require File.expand_path("../../lib/validate_nz_bank_acc.rb", __FILE__)
@@ -0,0 +1,72 @@
1
+ require 'spec_helper'
2
+ describe ValidateNzBankAcc do
3
+
4
+ let(:ex1) { ValidateNzBankAcc.new("01","902","-0068389",'00') }
5
+ let(:ex2) { ValidateNzBankAcc.new("08","6523","1954512","001") }
6
+ let(:ex3) { ValidateNzBankAcc.new("26","2600","0320871","032") }
7
+
8
+
9
+ describe "valid_bank_code?" do
10
+ it "should check against BANKS for the bank code" do
11
+ ValidateNzBankAcc.new("03","0123","0034141","03").valid_bank_code?.should be_true
12
+ ValidateNzBankAcc.new("01","1113","0034141","03").valid_bank_code?.should be_true
13
+ ValidateNzBankAcc.new("20","0123","1111111","11").valid_bank_code?.should be_true
14
+ ValidateNzBankAcc.new("05","0123","0034141","03").valid_bank_code?.should be_false # no back 05
15
+ end
16
+ end
17
+
18
+ describe "valid_bank_branch?" do
19
+ it "should validate the branch code against the range of valid branches" do
20
+ # VALIDS
21
+ ValidateNzBankAcc.new("03","0123","0034141","03").valid_branch_code?.should be_true
22
+ ValidateNzBankAcc.new("26","2600","0034141","03").valid_branch_code?.should be_true
23
+ ValidateNzBankAcc.new("26","2699","0034141","0003").valid_branch_code?.should be_true
24
+ ValidateNzBankAcc.new("11","6666","0034141","0003").valid_branch_code?.should be_true
25
+
26
+ # INVALIDS
27
+ ValidateNzBankAcc.new("11","1111","0034141","0003").valid_branch_code?.should be_false
28
+ ValidateNzBankAcc.new("01","2012","0034141","0003").valid_branch_code?.should be_false
29
+ end
30
+ end
31
+
32
+ describe "algo_code" do
33
+ it "should select the correct algo_code code" do
34
+ ValidateNzBankAcc.new("08","0123","0034141","03").algo_code.should == :d
35
+ ValidateNzBankAcc.new("31","0123","0034141","03").algo_code.should == :x
36
+
37
+ # If the account base number is below 00990000 then apply algorithm A, otherwise apply algorithm B
38
+ ValidateNzBankAcc.new("30","0123","0034141","03").algo_code.should == :a
39
+ ValidateNzBankAcc.new("30","0123","1034141","03").algo_code.should == :b
40
+ ValidateNzBankAcc.new("30","0123","0990000","03").algo_code.should == :b
41
+ ValidateNzBankAcc.new("30","0123","0989999","03").algo_code.should == :a
42
+ end
43
+ it "should select the correct algo_code code according to the pdf" do
44
+ ex1.algo_code.should == :a
45
+ ex2.algo_code.should == :d
46
+ ex3.algo_code.should == :g
47
+ end
48
+ end
49
+
50
+ describe "number_18_char" do
51
+ it "should zero pad the 7 digit account number to 8 characters" do
52
+ ValidateNzBankAcc.new("08","0123","0034141","03").number.length.should == 18
53
+ ValidateNzBankAcc.new("08","0123","0034141","03").number.should == '080123000341410003'
54
+ end
55
+ end
56
+
57
+ describe "checksum_sum" do
58
+ it "should return the same numbers as in the pdf" do
59
+ ex1.checksum_sum.should == 176
60
+ ex2.checksum_sum.should == 121
61
+ ex3.checksum_sum.should == 30
62
+ end
63
+ end
64
+
65
+ describe "valid_modulo?" do
66
+ it "should valid modulo for the example in the pdf" do
67
+ ex1.valid_modulo?.should be_true
68
+ ex1.valid_modulo?.should be_true
69
+ ex1.valid_modulo?.should be_true
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,18 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/validate_nz_bank_acc/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Eaden McKee"]
6
+ gem.email = ["eadz@eadz.co.nz"]
7
+ gem.description = %q{Validates a NZ bank account against checksum rules}
8
+ gem.summary = %q{Validates a NZ bank account against checksum rules}
9
+ gem.homepage = "https://github.com/localist/ValidateNzBankAcc"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "validate_nz_bank_acc"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = ValidateNzBankAcc::VERSION
17
+ gem.add_development_dependency "rspec"
18
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: validate_nz_bank_acc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Eaden McKee
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-01 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &17470780 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *17470780
25
+ description: Validates a NZ bank account against checksum rules
26
+ email:
27
+ - eadz@eadz.co.nz
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - .gitignore
33
+ - Gemfile
34
+ - LICENSE
35
+ - README.md
36
+ - Rakefile
37
+ - lib/validate_nz_bank_acc.rb
38
+ - lib/validate_nz_bank_acc/version.rb
39
+ - spec/spec_helper.rb
40
+ - spec/validate_nz_bank_acc_spec.rb
41
+ - validate_nz_bank_acc.gemspec
42
+ homepage: https://github.com/localist/ValidateNzBankAcc
43
+ licenses: []
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ! '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ requirements: []
61
+ rubyforge_project:
62
+ rubygems_version: 1.8.11
63
+ signing_key:
64
+ specification_version: 3
65
+ summary: Validates a NZ bank account against checksum rules
66
+ test_files:
67
+ - spec/spec_helper.rb
68
+ - spec/validate_nz_bank_acc_spec.rb
69
+ has_rdoc: