Class: SecID::CUSIP

Inherits:
Base
  • Object
show all
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

Examples:

Validate a CUSIP

SecID::CUSIP.valid?('037833100')  #=> true

Convert to ISIN

cusip = SecID::CUSIP.new('037833100')
cusip.to_isin('US')  #=> #<SecID::ISIN>

See Also:

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

Validatable::ERROR_MAP

Constants included from Normalizable

Normalizable::SEPARATORS

Instance Attribute Summary collapse

Attributes inherited from Base

#full_id, #identifier

Instance Method Summary collapse

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

included

Constructor Details

#initialize(cusip) ⇒ CUSIP

Returns a new instance of CUSIP.

Parameters:

  • cusip (String)

    the CUSIP string to parse



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

#cusip6String? (readonly)

Returns the 6-character issuer code.

Returns:

  • (String, nil)

    the 6-character issuer code



34
35
36
# File 'lib/sec_id/cusip.rb', line 34

def cusip6
  @cusip6
end

#issueString? (readonly)

Returns the 2-character issue number.

Returns:

  • (String, nil)

    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_digitInteger

Returns the calculated check digit (0-9).

Returns:

  • (Integer)

    the calculated check digit (0-9)

Raises:



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).

Returns:

  • (Boolean)

    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.

Parameters:

  • country_code (String)

    the ISO 3166-1 alpha-2 country code (must be CGS country)

Returns:

  • (ISIN)

    a new ISIN instance

Raises:



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_sString?

Returns:

  • (String, nil)


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