Module: Ibandit::CheckDigit

Defined in:
lib/ibandit/check_digit.rb

Class Method Summary collapse

Class Method Details

.belgian(string) ⇒ Object

Currently unused in this gem. This method calculates the last two digits of a Belgian account number when given the first ten digits.



64
65
66
67
# File 'lib/ibandit/check_digit.rb', line 64

def self.belgian(string)
  remainder = string.to_i % 97
  format('%02d', remainder)
end

.dutch(string) ⇒ Object

Currently unused in this gem. This method calculates the last digit of a Dutch account number when given the first nine digits.



122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/ibandit/check_digit.rb', line 122

def self.dutch(string)
  scaled_values = string.reverse.chars.map.with_index do |char, index|
    unless char.to_i.to_s == char
      raise InvalidCharacterError,
            "Unexpected non-numeric character '#{char}'"
    end

    char.to_i * (index + 2)
  end

  result = 11 - scaled_values.inject(:+) % 11
  result < 10 ? result.to_s : (11 - result).to_s
end

.estonian(string) ⇒ Object

Currently unused in this gem. This method calculates the last digit of a Estonian account number when given the initial digits.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/ibandit/check_digit.rb', line 71

def self.estonian(string)
  weights = [7, 3, 1]

  scaled_values = string.reverse.chars.map.with_index do |char, index|
    unless char.to_i.to_s == char
      raise InvalidCharacterError,
            "Unexpected non-numeric character '#{char}'"
    end

    char.to_i * weights[index % weights.size]
  end

  (scaled_values.inject(:+) % 10).to_s
end

.iban(country_code, bban) ⇒ Object



162
163
164
# File 'lib/ibandit/check_digit.rb', line 162

def self.iban(country_code, bban)
  mod_97_10(bban + country_code)
end

.italian(string) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ibandit/check_digit.rb', line 35

def self.italian(string)
  odd_mapping = {
    'A' => 1,  'B' => 0,  'C' => 5,  'D' => 7,  'E' => 9,  'F' => 13,
    'G' => 15, 'H' => 17, 'I' => 19, 'J' => 21, 'K' => 2,  'L' => 4,
    'M' => 18, 'N' => 20, 'O' => 11, 'P' => 3,  'Q' => 6,  'R' => 8,
    'S' => 12, 'T' => 14, 'U' => 16, 'V' => 10, 'W' => 22, 'X' => 25,
    'Y' => 24, 'Z' => 23, '0' => 1,  '1' => 0,  '2' => 5,  '3' => 7,
    '4' => 9,  '5' => 13, '6' => 15, '7' => 17, '8' => 19, '9' => 21
  }

  scaled_values = string.chars.map.with_index do |char, index|
    if index.even?
      odd_mapping[char]
    else
      case char.ord
      when 48..57 then char.to_i         # 0..9
      when 65..90 then char.ord - 65     # A..Z
      else
        raise InvalidCharacterError,
              "Unexpected non-alphanumeric character '#{char}'"
      end
    end
  end

  (scaled_values.inject(:+) % 26 + 65).chr
end

.lund(string) ⇒ Object

Currently unused in this gem. This method calculates the last digit of a Finnish account number when given the initial digits (in electronic format).



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/ibandit/check_digit.rb', line 139

def self.lund(string)
  weights = [2, 1]

  scaled_values = string.reverse.chars.map.with_index do |char, index|
    unless char.to_i.to_s == char
      raise InvalidCharacterError,
            "Unexpected non-numeric character '#{char}'"
    end

    scaled_value = char.to_i * weights[index % weights.size]
    scaled_value < 10 ? scaled_value : scaled_value % 10 + 1
  end

  (10 - scaled_values.inject(:+) % 10).to_s
end

.mod_97_10(string) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/ibandit/check_digit.rb', line 3

def self.mod_97_10(string)
  chars = string + '00'
  digits = chars.bytes.map do |byte|
    case byte
    when 48..57 then byte.chr           # 0..9
    when 65..90 then (byte - 55).to_s   # A..Z
    else
      raise InvalidCharacterError,
            "Unexpected non-alphanumeric character '#{byte.chr}'"
    end
  end
  remainder = digits.join.to_i % 97
  format('%02d', 98 - remainder)
end

.rib(bank_code, branch_code, account_number) ⇒ Object



155
156
157
158
159
160
# File 'lib/ibandit/check_digit.rb', line 155

def self.rib(bank_code, branch_code, )
  remainder = 97 - (89 * rib_value(bank_code) +
                    15 * rib_value(branch_code) +
                    3 * rib_value()) % 97
  format('%02d', remainder)
end

.rib_value(string) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/ibandit/check_digit.rb', line 166

def self.rib_value(string)
  rib_mapping = {
    'A' => 1, 'B' => 2, 'C' => 3, 'D' => 4, 'E' => 5, 'F' => 6, 'G' => 7,
    'H' => 8, 'I' => 9, 'J' => 1, 'K' => 2, 'L' => 3, 'M' => 4, 'N' => 5,
    'O' => 6, 'P' => 7, 'Q' => 8, 'R' => 9, 'S' => 2, 'T' => 3, 'U' => 4,
    'V' => 5, 'W' => 6, 'X' => 7, 'Y' => 8, 'Z' => 9, '0' => 0, '1' => 1,
    '2' => 2, '3' => 3, '4' => 4, '5' => 5, '6' => 6, '7' => 7, '8' => 8,
    '9' => 9
  }

  string.chars.map do |character|
    unless rib_mapping[character]
      raise InvalidCharacterError, "Unexpected byte '#{character}' in RIB"
    end
    rib_mapping[character]
  end.join.to_i
end

.slovakian_basic(string) ⇒ Object

Currently unused in this gem. This method calculates the last digit of a Slovakian account number prefix when given the initial digits.



105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/ibandit/check_digit.rb', line 105

def self.slovakian_basic(string)
  weights = [6, 3, 7, 9, 10, 5, 8, 4, 2]

  scaled_values = string.chars.map.with_index do |char, index|
    unless char.to_i.to_s == char
      raise InvalidCharacterError,
            "Unexpected non-numeric character '#{char}'"
    end

    char.to_i * weights[index]
  end

  (11 - scaled_values.inject(:+) % 11).to_s
end

.slovakian_prefix(string) ⇒ Object

Currently unused in this gem. This method calculates the last digit of a Slovakian account number (basic) when given the initial digits.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/ibandit/check_digit.rb', line 88

def self.slovakian_prefix(string)
  weights = [10, 5, 8, 4, 2]

  scaled_values = string.chars.map.with_index do |char, index|
    unless char.to_i.to_s == char
      raise InvalidCharacterError,
            "Unexpected non-numeric character '#{char}'"
    end

    char.to_i * weights[index]
  end

  (11 - scaled_values.inject(:+) % 11).to_s
end

.spanish(string) ⇒ Object

Currently unused in this gem. This method calculates a mod 11 check digit from the string of digits passed in. These check digits are used in the 1st and 2nd digits of local Spanish account numbers.



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ibandit/check_digit.rb', line 22

def self.spanish(string)
  padded_string = string.rjust(10, '0')
  scaled_values = padded_string.chars.map.with_index do |digit, index|
    unless digit.to_i.to_s == digit
      raise InvalidCharacterError,
            "Unexpected non-numeric character '#{digit}'"
    end
    Integer(digit) * (2**index % 11)
  end
  result = 11 - scaled_values.inject(:+) % 11
  result < 10 ? result.to_s : (11 - result).to_s
end