Class: SecID::CUSIP
- Includes:
- Checkable
- Defined in:
- lib/sec_id/cusip.rb
Overview
Committee on Uniform Securities Identification Procedures (CUSIP) - a 9-character alphanumeric code that identifies North American securities.
Format: 6-character issuer code (CUSIP-6) + 2-character issue number + 1-digit check digit
Constant Summary collapse
- FULL_NAME =
'Committee on Uniform Securities Identification Procedures'- ID_LENGTH =
9- EXAMPLE =
'037833100'- VALID_CHARS_REGEX =
/\A[A-Z0-9*@#]+\z/- ID_REGEX =
Regular expression for parsing CUSIP components.
/\A (?<identifier> (?<cusip6>[A-Z0-9]{5}[A-Z0-9*@#]) (?<issue>[A-Z0-9*@#]{2})) (?<check_digit>\d)? \z/x
Constants included from Checkable
SecID::Checkable::CHAR_TO_DIGIT, SecID::Checkable::CHAR_TO_DIGITS
Constants included from Validatable
Constants included from Normalizable
Instance Attribute Summary collapse
-
#cusip6 ⇒ String?
readonly
The 6-character issuer code.
-
#issue ⇒ String?
readonly
The 2-character issue number.
Attributes inherited from Base
Instance Method Summary collapse
-
#calculate_check_digit ⇒ Integer
The calculated check digit (0-9).
-
#cins? ⇒ Boolean
True if first character is a letter (CINS identifier).
-
#initialize(cusip) ⇒ CUSIP
constructor
A new instance of CUSIP.
-
#to_isin(country_code) ⇒ ISIN
A new ISIN instance.
- #to_pretty_s ⇒ String?
Methods included from Checkable
included, #restore, #restore!, #to_s, #valid?
Methods inherited from Base
#==, #as_json, #hash, inherited, #to_h
Methods included from Validatable
#errors, included, #valid?, #validate, #validate!
Methods included from Normalizable
included, #normalize!, #normalized, #to_s, #to_str
Methods included from IdentifierMetadata
Constructor Details
#initialize(cusip) ⇒ CUSIP
Returns a new instance of CUSIP.
40 41 42 43 44 45 46 |
# File 'lib/sec_id/cusip.rb', line 40 def initialize(cusip) cusip_parts = parse cusip @identifier = cusip_parts[:identifier] @cusip6 = cusip_parts[:cusip6] @issue = cusip_parts[:issue] @check_digit = cusip_parts[:check_digit]&.to_i end |
Instance Attribute Details
#cusip6 ⇒ String? (readonly)
Returns the 6-character issuer code.
34 35 36 |
# File 'lib/sec_id/cusip.rb', line 34 def cusip6 @cusip6 end |
#issue ⇒ String? (readonly)
Returns the 2-character issue number.
37 38 39 |
# File 'lib/sec_id/cusip.rb', line 37 def issue @issue end |
Instance Method Details
#calculate_check_digit ⇒ Integer
Returns the calculated check digit (0-9).
57 58 59 60 |
# File 'lib/sec_id/cusip.rb', line 57 def calculate_check_digit validate_format_for_calculation! mod10(luhn_sum_double_add_double(reversed_digits_single(identifier))) end |
#cins? ⇒ Boolean
Returns true if first character is a letter (CINS identifier).
81 82 83 |
# File 'lib/sec_id/cusip.rb', line 81 def cins? cusip6[0] < '0' || cusip6[0] > '9' end |
#to_isin(country_code) ⇒ ISIN
Returns a new ISIN instance.
65 66 67 68 69 70 71 |
# File 'lib/sec_id/cusip.rb', line 65 def to_isin(country_code) unless ISIN::CGS_COUNTRY_CODES.include?(country_code) raise(InvalidFormatError, "'#{country_code}' is not a CGS country code!") end ISIN.new(country_code + restore).restore! end |
#to_pretty_s ⇒ String?
49 50 51 52 53 |
# File 'lib/sec_id/cusip.rb', line 49 def to_pretty_s return nil unless valid? "#{cusip6} #{issue} #{check_digit}" end |