Class: SecurityIdentifiers::CUSIP

Inherits:
Base
  • Object
show all
Defined in:
lib/security_identifiers/cusip.rb

Instance Attribute Summary

Attributes inherited from Base

#original_check_digit

Instance Method Summary collapse

Methods inherited from Base

#to_s, #valid?

Constructor Details

#initialize(str) ⇒ CUSIP

Returns a new instance of CUSIP.

Raises:



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/security_identifiers/cusip.rb', line 5

def initialize(str)
  raise InvalidFormat if str.nil?

  match_data = str.upcase.match(/^([A-Z0-9]{8})(\d{1})?$/)

  raise InvalidFormat if match_data.nil?

  @identifier, @original_check_digit = match_data.captures

  fix! if @original_check_digit.nil?
end

Instance Method Details

#check_digitObject



17
18
19
20
21
22
23
24
25
# File 'lib/security_identifiers/cusip.rb', line 17

def check_digit
  values = odd_values.map { |i| i * 2 }.zip(even_values).flatten

  values = values.inject(0) do |sum, i|
    sum += (i / 10) + i % 10
  end

  ((10 - values) % 10) % 10
end

#to_isin(iso = 'US') ⇒ Object

Raises:



27
28
29
30
31
# File 'lib/security_identifiers/cusip.rb', line 27

def to_isin(iso = 'US')
  raise InvalidConversion if !['US', 'CA'].include?(iso)

  ISIN.new([iso, @identifier, check_digit].join)
end