Module: IsinCodeGenerator

Defined in:
lib/isin_code_generator.rb,
lib/isin_code_generator/version.rb

Constant Summary collapse

VERSION =
"0.0.2"

Class Method Summary collapse

Class Method Details

.make(home_country_iso) ⇒ Object

Raises:

  • (ArgumentError)


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/isin_code_generator.rb', line 4

def self.make(home_country_iso)
  raise ArgumentError if home_country_iso.length > 2
  raise ArgumentError if home_country_iso.include?(" ")

  code = home_country_iso + (1..9).collect{(rand*10).floor}.join
  code << ("0".."9").detect do |c|
    base10 = ""
    (code + c).each_char{|c| base10 += c.to_i(36).to_s}
    checksum = 0
    len = base10.length-1
    parity = len%2
    (0..len).each do |i|
      i = len-i
      weighted = base10[i, 1].to_i << ((i-parity)&1)
      checksum += weighted%10 + weighted/10
    end
    checksum%10==0
  end
end