Class: SecurityIdentifiers::CUSIP
- Defined in:
- lib/security_identifiers/cusip.rb
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.
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_digit ⇒ Object
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
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 |