posted on

Ruby , Rubygems , Open Source , Security Identifiers


Way back in 2013 we worked on a project that made use of various security identification codes (ISIN, CUSIP). From this project I've extracted some functionality into the Security Identifiers gem which has been on Github for a while.

Recently we were contacted by a user that could not find the gem - it turns out this was never published to Rubygems, so I thought now would be a good time to cover what this gem does.

What Does It Do?

In a nutshell this gem allows you to validate ISIN, CUSIP and SEDOL codes. You can also convert CUSIP and SEDOL codes to an ISIN.

Usage

Firstly add the gem to your Gemfile

  gem 'security_identifiers', '0.1.1'

then run

  bundle

To validate a CUSIP

  cusip = SecurityIdentifiers::CUSIP.new('125509BG3')
  cusip.valid? # => true

To convert the cusip to an ISIN

  isin = cusip.to_isin # => #<SecurityIdentifiers::ISIN:0x007f991a3d4770... >
  isin.to_s # => US125509BG36

By default #to_isin assumes the company has a US prefix you can pass an alternative prefix to the method.

ActiveModel Validators

Also included in the gem are custom validators for ActiveModel. To use these:

  class MyModel
    include ActiveModel::Validations

    include SecurityIdentifiers::Validations

    attr_accessor :isin

    validates :isin, isin: true

    def initialize(isin)
      self.isin = isin
    end
  end

  m = MyModel.new('0378331005')
  m.valid? # => false
  m.errors['isin'] # => ["is not in the correct format"]

  m = MyModel.new('US037833100')
  m.valid? # => false
  m.errors['isin'] # => ["does not have a valid check digit"]

  m = MyModel.new('US0378331005')
  m.valid? # => true

For further details or to submit a pull request or issue please refer to the repo on Github.