ird-bank 1.0.0
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 +7 -0
- data/config/banks.yml +143 -0
- data/lib/ird-bank.rb +90 -0
- metadata +45 -0
    
        checksums.yaml
    ADDED
    
    | @@ -0,0 +1,7 @@ | |
| 1 | 
            +
            ---
         | 
| 2 | 
            +
            SHA256:
         | 
| 3 | 
            +
              metadata.gz: dc05d7679921f5c4e8239df0e18adc02b0c81cdaf9d9f4c8d1817543e3626218
         | 
| 4 | 
            +
              data.tar.gz: 74b1bc21213a43cae8bf4118088b60a628b3417126d4e81ff26a56dc5a167950
         | 
| 5 | 
            +
            SHA512:
         | 
| 6 | 
            +
              metadata.gz: 11306f0013c19208cd9f97d90373792a036963bf6570202b740b3f128766fe55091ed53b1f226bfb82f3f764cc2b6cf663d1547db5f9231923998c5cb8c06437
         | 
| 7 | 
            +
              data.tar.gz: 88d7605170a4c318736d32fc2c07ddf9e70da647ec1871af9c007b8433884c03942ffd3a1bcfe181802d7e61e122125978f17dcbe27cf7746724a40e1815df81
         | 
    
        data/config/banks.yml
    ADDED
    
    | @@ -0,0 +1,143 @@ | |
| 1 | 
            +
            ---
         | 
| 2 | 
            +
            - id: 1
         | 
| 3 | 
            +
              branches:
         | 
| 4 | 
            +
              - from: 1
         | 
| 5 | 
            +
                to: 999
         | 
| 6 | 
            +
              - from: 1100
         | 
| 7 | 
            +
                to: 1199
         | 
| 8 | 
            +
              - from: 1800
         | 
| 9 | 
            +
                to: 1899
         | 
| 10 | 
            +
            - id: 2
         | 
| 11 | 
            +
              branches:
         | 
| 12 | 
            +
              - from: 1
         | 
| 13 | 
            +
                to: 999
         | 
| 14 | 
            +
              - from: 1200
         | 
| 15 | 
            +
                to: 1299
         | 
| 16 | 
            +
            - id: 3
         | 
| 17 | 
            +
              branches:
         | 
| 18 | 
            +
              - from: 1
         | 
| 19 | 
            +
                to: 999
         | 
| 20 | 
            +
              - from: 1300
         | 
| 21 | 
            +
                to: 1399
         | 
| 22 | 
            +
              - from: 1500
         | 
| 23 | 
            +
                to: 1599
         | 
| 24 | 
            +
              - from: 1700
         | 
| 25 | 
            +
                to: 1799
         | 
| 26 | 
            +
              - from: 1900
         | 
| 27 | 
            +
                to: 1999
         | 
| 28 | 
            +
            - id: 6
         | 
| 29 | 
            +
              branches:
         | 
| 30 | 
            +
              - from: 1
         | 
| 31 | 
            +
                to: 999
         | 
| 32 | 
            +
              - from: 1400
         | 
| 33 | 
            +
                to: 1499
         | 
| 34 | 
            +
            - id: 8
         | 
| 35 | 
            +
              branches:
         | 
| 36 | 
            +
              - from: 6500
         | 
| 37 | 
            +
                to: 6599
         | 
| 38 | 
            +
            - id: 9
         | 
| 39 | 
            +
              branches:
         | 
| 40 | 
            +
              - from: 0
         | 
| 41 | 
            +
                to: 0
         | 
| 42 | 
            +
            - id: 11
         | 
| 43 | 
            +
              branches:
         | 
| 44 | 
            +
              - from: 5000
         | 
| 45 | 
            +
                to: 6499
         | 
| 46 | 
            +
              - from: 6600
         | 
| 47 | 
            +
                to: 8999
         | 
| 48 | 
            +
            - id: 12
         | 
| 49 | 
            +
              branches:
         | 
| 50 | 
            +
              - from: 3000
         | 
| 51 | 
            +
                to: 3299
         | 
| 52 | 
            +
              - from: 3400
         | 
| 53 | 
            +
                to: 3499
         | 
| 54 | 
            +
              - from: 3600
         | 
| 55 | 
            +
                to: 3699
         | 
| 56 | 
            +
            - id: 13
         | 
| 57 | 
            +
              branches:
         | 
| 58 | 
            +
              - from: 4900
         | 
| 59 | 
            +
                to: 4999
         | 
| 60 | 
            +
            - id: 14
         | 
| 61 | 
            +
              branches:
         | 
| 62 | 
            +
              - from: 4700
         | 
| 63 | 
            +
                to: 4799
         | 
| 64 | 
            +
            - id: 15
         | 
| 65 | 
            +
              branches:
         | 
| 66 | 
            +
              - from: 3900
         | 
| 67 | 
            +
                to: 3999
         | 
| 68 | 
            +
            - id: 16
         | 
| 69 | 
            +
              branches:
         | 
| 70 | 
            +
              - from: 4400
         | 
| 71 | 
            +
                to: 4499
         | 
| 72 | 
            +
            - id: 17
         | 
| 73 | 
            +
              branches:
         | 
| 74 | 
            +
              - from: 3300
         | 
| 75 | 
            +
                to: 3399
         | 
| 76 | 
            +
            - id: 18
         | 
| 77 | 
            +
              branches:
         | 
| 78 | 
            +
              - from: 3500
         | 
| 79 | 
            +
                to: 3599
         | 
| 80 | 
            +
            - id: 19
         | 
| 81 | 
            +
              branches:
         | 
| 82 | 
            +
              - from: 4600
         | 
| 83 | 
            +
                to: 4649
         | 
| 84 | 
            +
            - id: 20
         | 
| 85 | 
            +
              branches:
         | 
| 86 | 
            +
              - from: 4100
         | 
| 87 | 
            +
                to: 4199
         | 
| 88 | 
            +
            - id: 21
         | 
| 89 | 
            +
              branches:
         | 
| 90 | 
            +
              - from: 4800
         | 
| 91 | 
            +
                to: 4899
         | 
| 92 | 
            +
            - id: 22
         | 
| 93 | 
            +
              branches:
         | 
| 94 | 
            +
              - from: 4000
         | 
| 95 | 
            +
                to: 4049
         | 
| 96 | 
            +
            - id: 23
         | 
| 97 | 
            +
              branches:
         | 
| 98 | 
            +
              - from: 3700
         | 
| 99 | 
            +
                to: 3799
         | 
| 100 | 
            +
            - id: 24
         | 
| 101 | 
            +
              branches:
         | 
| 102 | 
            +
              - from: 4300
         | 
| 103 | 
            +
                to: 4349
         | 
| 104 | 
            +
            - id: 25
         | 
| 105 | 
            +
              branches:
         | 
| 106 | 
            +
              - from: 2500
         | 
| 107 | 
            +
                to: 2599
         | 
| 108 | 
            +
            - id: 26
         | 
| 109 | 
            +
              branches:
         | 
| 110 | 
            +
              - from: 2600
         | 
| 111 | 
            +
                to: 2699
         | 
| 112 | 
            +
            - id: 27
         | 
| 113 | 
            +
              branches:
         | 
| 114 | 
            +
              - from: 3800
         | 
| 115 | 
            +
                to: 3849
         | 
| 116 | 
            +
            - id: 28
         | 
| 117 | 
            +
              branches:
         | 
| 118 | 
            +
              - from: 2100
         | 
| 119 | 
            +
                to: 2149
         | 
| 120 | 
            +
            - id: 29
         | 
| 121 | 
            +
              branches:
         | 
| 122 | 
            +
              - from: 2150
         | 
| 123 | 
            +
                to: 2299
         | 
| 124 | 
            +
            - id: 30
         | 
| 125 | 
            +
              branches:
         | 
| 126 | 
            +
              - from: 2900
         | 
| 127 | 
            +
                to: 2949
         | 
| 128 | 
            +
            - id: 31
         | 
| 129 | 
            +
              branches:
         | 
| 130 | 
            +
              - from: 2800
         | 
| 131 | 
            +
                to: 2849
         | 
| 132 | 
            +
            - id: 33
         | 
| 133 | 
            +
              branches:
         | 
| 134 | 
            +
              - from: 6700
         | 
| 135 | 
            +
                to: 6799
         | 
| 136 | 
            +
            - id: 35
         | 
| 137 | 
            +
              branches:
         | 
| 138 | 
            +
              - from: 2400
         | 
| 139 | 
            +
                to: 2499
         | 
| 140 | 
            +
            - id: 38
         | 
| 141 | 
            +
              branches:
         | 
| 142 | 
            +
              - from: 9000
         | 
| 143 | 
            +
                to: 9499
         | 
    
        data/lib/ird-bank.rb
    ADDED
    
    | @@ -0,0 +1,90 @@ | |
| 1 | 
            +
            require "yaml"
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            module IRD
         | 
| 4 | 
            +
              class Bank
         | 
| 5 | 
            +
                def self.all
         | 
| 6 | 
            +
                    return load_banks
         | 
| 7 | 
            +
                end
         | 
| 8 | 
            +
                def self.bank(id)
         | 
| 9 | 
            +
                    load_banks.each { |bank| return bank if bank["id"] == id }
         | 
| 10 | 
            +
                    return false
         | 
| 11 | 
            +
                end
         | 
| 12 | 
            +
                def self.branches(id)
         | 
| 13 | 
            +
                    load_banks.each { |bank| return bank["branches"] if bank["id"] == id }
         | 
| 14 | 
            +
                    return false
         | 
| 15 | 
            +
                end
         | 
| 16 | 
            +
                def self.validate(bk, brch=0, acct=0, suf=0)
         | 
| 17 | 
            +
                    if brch == 0 && acct == 0 && suf == 0
         | 
| 18 | 
            +
                        bank = bk.split('-')[0].to_s.rjust(2, '0')
         | 
| 19 | 
            +
                        branch = bk.split('-')[1].to_s.rjust(4, '0')
         | 
| 20 | 
            +
                        account = bk.split('-')[2].to_s.rjust(8, '0')
         | 
| 21 | 
            +
                        suffix = bk.split('-')[3].to_s.rjust(4, '0')
         | 
| 22 | 
            +
                    else
         | 
| 23 | 
            +
                        bank = bk.to_s.rjust(2, '0')
         | 
| 24 | 
            +
                        branch = brch.to_s.rjust(4, '0')
         | 
| 25 | 
            +
                        account = acct.to_s.rjust(8, '0')
         | 
| 26 | 
            +
                        suffix = suf.to_s.rjust(4, '0')
         | 
| 27 | 
            +
                    end
         | 
| 28 | 
            +
                    bank_check = bank + branch + account + suffix
         | 
| 29 | 
            +
                    return false if account == 0
         | 
| 30 | 
            +
                    return false unless is_valid_branch? bank.to_i, branch.to_i
         | 
| 31 | 
            +
                    return false unless bank_check.length == 18
         | 
| 32 | 
            +
                    weight = weight_check(bank, account)
         | 
| 33 | 
            +
             | 
| 34 | 
            +
                    counter = 0
         | 
| 35 | 
            +
                    check = Array.new
         | 
| 36 | 
            +
                    check_digit = weight.to_s.chars.each do |v| 
         | 
| 37 | 
            +
                      check.push((v == 'A' ? 10 : v.to_i) * bank_check.split[counter].to_i) 
         | 
| 38 | 
            +
                      counter += 1
         | 
| 39 | 
            +
                    end
         | 
| 40 | 
            +
                    counter = 0
         | 
| 41 | 
            +
                    calc = check.inject(0, :+)
         | 
| 42 | 
            +
                    check_num = modulo(bank)
         | 
| 43 | 
            +
                    return calc % check_num === 0
         | 
| 44 | 
            +
             | 
| 45 | 
            +
                end
         | 
| 46 | 
            +
                def self.is_valid_bank?(id)
         | 
| 47 | 
            +
                    return bank(id) ? true : false
         | 
| 48 | 
            +
                end
         | 
| 49 | 
            +
                def self.is_valid_branch?(bank, id)
         | 
| 50 | 
            +
                    begin
         | 
| 51 | 
            +
                        branches(bank).each do |branch| 
         | 
| 52 | 
            +
                            return true if id.between?(branch["from"], branch["to"]) 
         | 
| 53 | 
            +
                        end
         | 
| 54 | 
            +
                        return false
         | 
| 55 | 
            +
                    rescue NoMethodError => e
         | 
| 56 | 
            +
                    return false
         | 
| 57 | 
            +
                    end
         | 
| 58 | 
            +
                end
         | 
| 59 | 
            +
             | 
| 60 | 
            +
                private
         | 
| 61 | 
            +
                def self.modulo(bank)
         | 
| 62 | 
            +
                    case bank
         | 
| 63 | 
            +
                    when 29
         | 
| 64 | 
            +
                        return 10
         | 
| 65 | 
            +
                    when 31
         | 
| 66 | 
            +
                        return 1
         | 
| 67 | 
            +
                    else
         | 
| 68 | 
            +
                        return 11
         | 
| 69 | 
            +
                    end
         | 
| 70 | 
            +
                end
         | 
| 71 | 
            +
                def self.weight_check(bank, account)
         | 
| 72 | 
            +
                    case bank
         | 
| 73 | 
            +
                    when 8
         | 
| 74 | 
            +
                        return '000000076543210000'
         | 
| 75 | 
            +
                    when 9
         | 
| 76 | 
            +
                        return '000000000054320001'
         | 
| 77 | 
            +
                    when 29
         | 
| 78 | 
            +
                        return '000000013713710371'
         | 
| 79 | 
            +
                    when 31
         | 
| 80 | 
            +
                        return '000000000000000000'
         | 
| 81 | 
            +
                    else
         | 
| 82 | 
            +
                        return '00637900A584210000' if account.to_i < 990000
         | 
| 83 | 
            +
                        return '00000000A584210000' 
         | 
| 84 | 
            +
                    end
         | 
| 85 | 
            +
                end
         | 
| 86 | 
            +
                def self.load_banks
         | 
| 87 | 
            +
                    return YAML.load_file(File.join(File.dirname(__FILE__), "../config/banks.yml"))
         | 
| 88 | 
            +
                end
         | 
| 89 | 
            +
              end
         | 
| 90 | 
            +
            end
         | 
    
        metadata
    ADDED
    
    | @@ -0,0 +1,45 @@ | |
| 1 | 
            +
            --- !ruby/object:Gem::Specification
         | 
| 2 | 
            +
            name: ird-bank
         | 
| 3 | 
            +
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            +
              version: 1.0.0
         | 
| 5 | 
            +
            platform: ruby
         | 
| 6 | 
            +
            authors:
         | 
| 7 | 
            +
            - Theo Morra
         | 
| 8 | 
            +
            autorequire: 
         | 
| 9 | 
            +
            bindir: bin
         | 
| 10 | 
            +
            cert_chain: []
         | 
| 11 | 
            +
            date: 2019-05-12 00:00:00.000000000 Z
         | 
| 12 | 
            +
            dependencies: []
         | 
| 13 | 
            +
            description: A validator for New Zealand bank accounts
         | 
| 14 | 
            +
            email: theo@theom.nz
         | 
| 15 | 
            +
            executables: []
         | 
| 16 | 
            +
            extensions: []
         | 
| 17 | 
            +
            extra_rdoc_files: []
         | 
| 18 | 
            +
            files:
         | 
| 19 | 
            +
            - config/banks.yml
         | 
| 20 | 
            +
            - lib/ird-bank.rb
         | 
| 21 | 
            +
            homepage: https://github.com/td512/ird-bank
         | 
| 22 | 
            +
            licenses:
         | 
| 23 | 
            +
            - GPL-3.0+
         | 
| 24 | 
            +
            metadata: {}
         | 
| 25 | 
            +
            post_install_message: 
         | 
| 26 | 
            +
            rdoc_options: []
         | 
| 27 | 
            +
            require_paths:
         | 
| 28 | 
            +
            - lib
         | 
| 29 | 
            +
            required_ruby_version: !ruby/object:Gem::Requirement
         | 
| 30 | 
            +
              requirements:
         | 
| 31 | 
            +
              - - ">="
         | 
| 32 | 
            +
                - !ruby/object:Gem::Version
         | 
| 33 | 
            +
                  version: 2.4.0
         | 
| 34 | 
            +
            required_rubygems_version: !ruby/object:Gem::Requirement
         | 
| 35 | 
            +
              requirements:
         | 
| 36 | 
            +
              - - ">="
         | 
| 37 | 
            +
                - !ruby/object:Gem::Version
         | 
| 38 | 
            +
                  version: '0'
         | 
| 39 | 
            +
            requirements: []
         | 
| 40 | 
            +
            rubyforge_project: 
         | 
| 41 | 
            +
            rubygems_version: 2.7.3
         | 
| 42 | 
            +
            signing_key: 
         | 
| 43 | 
            +
            specification_version: 4
         | 
| 44 | 
            +
            summary: A validator for New Zealand bank accounts
         | 
| 45 | 
            +
            test_files: []
         |