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 =
Human-readable name of the standard.
'Committee on Uniform Securities Identification Procedures'- ID_LENGTH =
Valid length(s) of a normalized identifier.
9- EXAMPLE =
A representative valid identifier.
'037833100'- VALID_CHARS_REGEX =
Pattern matching the identifier's permitted character set.
/\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 Generatable
Generatable::ALPHA, Generatable::ALPHANUMERIC, Generatable::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
#restore, #restore!, #to_s, #valid?
Methods inherited from Base
#==, #as_json, example, full_name, has_check_digit?, #hash, id_length, length_specificity, length_values, short_name, #to_h
Methods included from Validatable
#errors, #valid?, #validate, #validate!
Methods included from Normalizable
#normalize!, #normalized, #to_s, #to_str
Constructor Details
#initialize(cusip) ⇒ CUSIP
Returns a new instance of CUSIP.
44 45 46 47 48 49 50 |
# File 'lib/sec_id/cusip.rb', line 44 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.
38 39 40 |
# File 'lib/sec_id/cusip.rb', line 38 def cusip6 @cusip6 end |
#issue ⇒ String? (readonly)
Returns the 2-character issue number.
41 42 43 |
# File 'lib/sec_id/cusip.rb', line 41 def issue @issue end |
Instance Method Details
#calculate_check_digit ⇒ Integer
Returns the calculated check digit (0-9).
61 62 63 64 |
# File 'lib/sec_id/cusip.rb', line 61 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).
87 88 89 |
# File 'lib/sec_id/cusip.rb', line 87 def cins? cusip6[0] < '0' || cusip6[0] > '9' end |
#to_isin(country_code) ⇒ ISIN
Returns a new ISIN instance.
78 79 80 81 82 83 84 |
# File 'lib/sec_id/cusip.rb', line 78 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?
53 54 55 56 57 |
# File 'lib/sec_id/cusip.rb', line 53 def to_pretty_s return nil unless valid? "#{cusip6} #{issue} #{check_digit}" end |