Class: SecurityIdentifiers::CUSIP

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

Constant Summary collapse

SYMBOLS =
{
  '*' => [3, 6],
  '@' => [3, 7],
  '#' => [3, 8]
}.freeze

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:



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/security_identifiers/cusip.rb', line 9

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



21
22
23
24
25
26
27
28
29
# File 'lib/security_identifiers/cusip.rb', line 21

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

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

  mod_10(sum)
end

#to_isin(iso = 'US') ⇒ Object

Raises:



31
32
33
34
35
# File 'lib/security_identifiers/cusip.rb', line 31

def to_isin(iso = 'US')
  raise InvalidConversion unless %w(US CA).include?(iso)

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