Class: SecurityIdentifiers::CUSIP
- 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
Instance Method Summary collapse
- #check_digit ⇒ Object
-
#initialize(str) ⇒ CUSIP
constructor
A new instance of CUSIP.
- #to_isin(iso = 'US') ⇒ Object
Methods inherited from Base
Constructor Details
#initialize(str) ⇒ CUSIP
Returns a new instance of CUSIP.
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_digit ⇒ Object
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
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 |