Sha256: 047cc9de069cd0274737ccec3ed5c35205b47d8ed973be3e0a3aef20870809be

Contents?: true

Size: 655 Bytes

Versions: 2

Compression:

Stored size: 655 Bytes

Contents

require "isin_code_generator/version"

module IsinCodeGenerator
  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
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
isin_code_generator-0.0.2 lib/isin_code_generator.rb
isin_code_generator-0.0.1 lib/isin_code_generator.rb