security_identifiers 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/.travis.yml +8 -0
- data/Gemfile +3 -0
- data/Guardfile +5 -0
- data/LICENSE.txt +22 -0
- data/README.md +78 -0
- data/Rakefile +8 -0
- data/lib/security_identifiers.rb +11 -0
- data/lib/security_identifiers/base.rb +38 -0
- data/lib/security_identifiers/cusip.rb +33 -0
- data/lib/security_identifiers/isin.rb +37 -0
- data/lib/security_identifiers/sedol.rb +34 -0
- data/lib/security_identifiers/validations/cusip_validator.rb +23 -0
- data/lib/security_identifiers/validations/isin_validator.rb +23 -0
- data/lib/security_identifiers/validations/sedol_validator.rb +23 -0
- data/lib/security_identifiers/version.rb +5 -0
- data/security_identifiers.gemspec +29 -0
- data/spec/cusip_spec.rb +81 -0
- data/spec/isin_spec.rb +69 -0
- data/spec/sedol_spec.rb +61 -0
- data/spec/spec_helper.rb +22 -0
- data/spec/validations/cusip_validator_spec.rb +23 -0
- data/spec/validations/isin_validator_spec.rb +21 -0
- data/spec/validations/sedol_validator_spec.rb +22 -0
- metadata +159 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 72c50ca8b151d2b24365161cf1349d638bfd71c5
|
4
|
+
data.tar.gz: 1f1f3a6c8607ffda761083b46bf934646409bf53
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 17e22d743cf8fb12c058820cfcea5dcb7aa16b484b848eef444ded844fe715b42b5cc75d35ac6964ec0e9241c80a8549dd191233fb8d7183293a14c4dac5337e
|
7
|
+
data.tar.gz: 6ad951a7bf8c9a5a500fbd01f693455a5403f037b3839e192d06d84d663d5d23a7194f4a331db2ab395ce8e45ee10fa181078cab577abaa272672427f8daaeaa
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Invisiblelines Ltd
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
# Security Identifiers
|
2
|
+
|
3
|
+
[](https://travis-ci.org/invisiblelines/security_identifiers)
|
4
|
+
[](https://codeclimate.com/github/invisiblelines/security_identifiers)
|
5
|
+
[](https://gemnasium.com/invisiblelines/security_identifiers)
|
6
|
+
|
7
|
+
Security identifiers validation library for Ruby.
|
8
|
+
|
9
|
+
Currently supports
|
10
|
+
|
11
|
+
- ISIN
|
12
|
+
- CUSIP
|
13
|
+
- SEDOL
|
14
|
+
|
15
|
+
Validates:
|
16
|
+
|
17
|
+
- Format
|
18
|
+
- Check Digit
|
19
|
+
|
20
|
+
## Installation
|
21
|
+
|
22
|
+
Add this line to your application's Gemfile:
|
23
|
+
|
24
|
+
gem 'security_identifiers'
|
25
|
+
|
26
|
+
And then execute:
|
27
|
+
|
28
|
+
$ bundle
|
29
|
+
|
30
|
+
Or install it yourself as:
|
31
|
+
|
32
|
+
$ gem install security_identifiers
|
33
|
+
|
34
|
+
## Usage
|
35
|
+
|
36
|
+
To validate an ISIN
|
37
|
+
|
38
|
+
isin = SecurityIdentifiers::ISIN.new('US0378331005')
|
39
|
+
isin.valid? # => true
|
40
|
+
|
41
|
+
or with an invalid identifier
|
42
|
+
|
43
|
+
isin = SecurityIdentifiers::ISIN.new('S0378331005') # => raises SecurityIdentifiers::InvalidFormat
|
44
|
+
|
45
|
+
To fix a missing check digit
|
46
|
+
|
47
|
+
isin = SecurityIdentifiers::ISIN.new('US037833100')
|
48
|
+
isin.fix!
|
49
|
+
|
50
|
+
Now you can validate
|
51
|
+
|
52
|
+
isin.valid? # => true
|
53
|
+
|
54
|
+
The same method apply to CUSIPs and SEDOLs.
|
55
|
+
|
56
|
+
CUSIPs and SEDOLs also support converting these identifiers to ISINs.
|
57
|
+
|
58
|
+
cusip = SecurityIdentifiers::CUSIP.new('125509BG3')
|
59
|
+
cusip.to_isin('US') # => SecurityIdentifiers::ISIN
|
60
|
+
|
61
|
+
## ActiveModel Validations
|
62
|
+
|
63
|
+
Includes custom validators for use in ActiveModel/ActiveRecord
|
64
|
+
|
65
|
+
class MyModel < ActiveRecord::Base
|
66
|
+
include SecurityIdentifiers::Validators
|
67
|
+
|
68
|
+
validates :isin, isin: true
|
69
|
+
validates :cusip, cusip: true
|
70
|
+
end
|
71
|
+
|
72
|
+
## Contributing
|
73
|
+
|
74
|
+
1. Fork it
|
75
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
76
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
77
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
78
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'security_identifiers/version'
|
2
|
+
require 'security_identifiers/base'
|
3
|
+
require 'security_identifiers/isin'
|
4
|
+
require 'security_identifiers/cusip'
|
5
|
+
require 'security_identifiers/sedol'
|
6
|
+
|
7
|
+
if defined?(ActiveModel)
|
8
|
+
require 'security_identifiers/validations/isin_validator'
|
9
|
+
require 'security_identifiers/validations/cusip_validator'
|
10
|
+
require 'security_identifiers/validations/sedol_validator'
|
11
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module SecurityIdentifiers
|
2
|
+
|
3
|
+
class InvalidFormat < StandardError; end
|
4
|
+
class InvalidConversion < StandardError; end
|
5
|
+
|
6
|
+
class Base
|
7
|
+
|
8
|
+
attr_reader :original_check_digit
|
9
|
+
|
10
|
+
def valid?
|
11
|
+
@original_check_digit.to_i == check_digit
|
12
|
+
end
|
13
|
+
|
14
|
+
def to_s
|
15
|
+
"#{@identifier}#{check_digit}"
|
16
|
+
end
|
17
|
+
|
18
|
+
def fix!
|
19
|
+
@original_check_digit = check_digit
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def digits
|
25
|
+
@digits ||= @identifier.split('').map { |i| i.match(/[A-Z]/) ? (i.ord - 55) : i.to_i }
|
26
|
+
end
|
27
|
+
|
28
|
+
def even_values
|
29
|
+
@even_values ||= digits.values_at(* digits.each_index.select { |i| i.even? })
|
30
|
+
end
|
31
|
+
|
32
|
+
def odd_values
|
33
|
+
@odd_values ||= digits.values_at(* digits.each_index.select { |i| i.odd? })
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module SecurityIdentifiers
|
2
|
+
|
3
|
+
class CUSIP < Base
|
4
|
+
|
5
|
+
def initialize(str)
|
6
|
+
raise InvalidFormat if str.nil?
|
7
|
+
|
8
|
+
match_data = str.upcase.match(/^([A-Z0-9]{8})(\d{1})?$/)
|
9
|
+
|
10
|
+
raise InvalidFormat if match_data.nil?
|
11
|
+
|
12
|
+
@identifier, @original_check_digit = match_data.captures
|
13
|
+
end
|
14
|
+
|
15
|
+
def check_digit
|
16
|
+
values = odd_values.map { |i| i * 2 }.zip(even_values).flatten
|
17
|
+
|
18
|
+
values = values.inject(0) do |sum, i|
|
19
|
+
sum += (i / 10) + i % 10
|
20
|
+
end
|
21
|
+
|
22
|
+
((10 - values) % 10) % 10
|
23
|
+
end
|
24
|
+
|
25
|
+
def to_isin(iso = 'US')
|
26
|
+
raise InvalidConversion if !['US', 'CA'].include?(iso)
|
27
|
+
|
28
|
+
ISIN.new([iso, @identifier, check_digit].join)
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module SecurityIdentifiers
|
2
|
+
|
3
|
+
class ISIN < Base
|
4
|
+
|
5
|
+
def initialize(str)
|
6
|
+
raise InvalidFormat if str.nil?
|
7
|
+
|
8
|
+
match_data = str.upcase.match(/^(([A-Z]{2})([A-Z0-9]{9}))(\d{1})?$/)
|
9
|
+
|
10
|
+
raise InvalidFormat if match_data.nil?
|
11
|
+
|
12
|
+
@identifier, @country_code, @nsin, @original_check_digit = match_data.captures
|
13
|
+
end
|
14
|
+
|
15
|
+
def check_digit
|
16
|
+
longest, shortest = if even_values.last == digits.last
|
17
|
+
[even_values, odd_values]
|
18
|
+
else
|
19
|
+
[odd_values, even_values]
|
20
|
+
end
|
21
|
+
|
22
|
+
longest = longest.map { |i| i * 2 }
|
23
|
+
values = (longest.concat(shortest)).to_s.scan(/./).map(&:to_i)
|
24
|
+
sum = values.inject(&:+)
|
25
|
+
|
26
|
+
(10 - (sum % 10)) % 10
|
27
|
+
end
|
28
|
+
|
29
|
+
protected
|
30
|
+
|
31
|
+
def digits
|
32
|
+
@digits ||= super.join('').split('').map(&:to_i)
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module SecurityIdentifiers
|
2
|
+
|
3
|
+
class SEDOL < Base
|
4
|
+
|
5
|
+
def initialize(str)
|
6
|
+
raise InvalidFormat if str.nil?
|
7
|
+
|
8
|
+
match_data = str.upcase.match(/^([A-Z0-9]{6})(\d{1})?$/)
|
9
|
+
|
10
|
+
raise InvalidFormat if match_data.nil?
|
11
|
+
|
12
|
+
@identifier, @original_check_digit = match_data.captures
|
13
|
+
end
|
14
|
+
|
15
|
+
def check_digit
|
16
|
+
weights = [1, 3, 1, 7, 3, 9, 1]
|
17
|
+
sum = 0
|
18
|
+
|
19
|
+
digits.each_with_index do |i, idx|
|
20
|
+
sum += weights[idx] * i
|
21
|
+
end
|
22
|
+
|
23
|
+
(10 - sum % 10) % 10
|
24
|
+
end
|
25
|
+
|
26
|
+
def to_isin(iso='GB')
|
27
|
+
raise InvalidConversion if !['GB', 'IE'].include?(iso)
|
28
|
+
|
29
|
+
ISIN.new([iso, '00', @identifier, check_digit].join)
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module SecurityIdentifiers
|
2
|
+
|
3
|
+
module Validations
|
4
|
+
|
5
|
+
class CusipValidator < ActiveModel::EachValidator
|
6
|
+
|
7
|
+
def validate_each(record, attribute, value)
|
8
|
+
begin
|
9
|
+
cusip = CUSIP.new(value)
|
10
|
+
|
11
|
+
unless cusip.valid?
|
12
|
+
record.errors[attribute] << 'does not have a valid check digit'
|
13
|
+
end
|
14
|
+
rescue SecurityIdentifiers::InvalidFormat
|
15
|
+
record.errors[attribute] << 'is not in the correct format'
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module SecurityIdentifiers
|
2
|
+
|
3
|
+
module Validations
|
4
|
+
|
5
|
+
class IsinValidator < ActiveModel::EachValidator
|
6
|
+
|
7
|
+
def validate_each(record, attribute, value)
|
8
|
+
begin
|
9
|
+
isin = ISIN.new(value)
|
10
|
+
|
11
|
+
unless isin.valid?
|
12
|
+
record.errors[attribute] << 'does not have a valid check digit'
|
13
|
+
end
|
14
|
+
rescue SecurityIdentifiers::InvalidFormat
|
15
|
+
record.errors[attribute] << 'is not in the correct format'
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module SecurityIdentifiers
|
2
|
+
|
3
|
+
module Validations
|
4
|
+
|
5
|
+
class SedolValidator < ActiveModel::EachValidator
|
6
|
+
|
7
|
+
def validate_each(record, attribute, value)
|
8
|
+
begin
|
9
|
+
sedol = SEDOL.new(value)
|
10
|
+
|
11
|
+
unless sedol.valid?
|
12
|
+
record.errors[attribute] << 'does not have a valid check digit'
|
13
|
+
end
|
14
|
+
rescue SecurityIdentifiers::InvalidFormat
|
15
|
+
record.errors[attribute] << 'is not in the correct format'
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
|
5
|
+
require 'security_identifiers/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |s|
|
8
|
+
s.name = "security_identifiers"
|
9
|
+
s.version = SecurityIdentifiers::VERSION
|
10
|
+
s.authors = ["Kieran Johnson"]
|
11
|
+
s.email = ["hello@invisiblelines.com"]
|
12
|
+
s.summary = %q{Security Identifier validation library for Ruby}
|
13
|
+
s.description = s.summary
|
14
|
+
s.homepage = ""
|
15
|
+
s.license = "MIT"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split($/)
|
18
|
+
s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
19
|
+
s.test_files = s.files.grep(%r{^(test|spec|features)/})
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
|
22
|
+
s.add_development_dependency 'bundler', '~> 1.3'
|
23
|
+
s.add_development_dependency 'rake'
|
24
|
+
s.add_development_dependency 'activemodel'
|
25
|
+
s.add_development_dependency 'guard', '2.12.1'
|
26
|
+
s.add_development_dependency 'guard-rspec', '4.5.0'
|
27
|
+
|
28
|
+
s.add_development_dependency 'rspec', '~> 3.2.0'
|
29
|
+
end
|
data/spec/cusip_spec.rb
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe CUSIP do
|
4
|
+
|
5
|
+
it 'is invalid without 8 character alpha-numeric identifier' do
|
6
|
+
expect { CUSIP.new('5509BG3') }.to raise_error(SecurityIdentifiers::InvalidFormat)
|
7
|
+
end
|
8
|
+
|
9
|
+
context 'Without a check digit' do
|
10
|
+
let(:cusip) { CUSIP.new('83764912') }
|
11
|
+
|
12
|
+
it 'is invalid' do
|
13
|
+
expect(cusip).to_not be_valid
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
context 'Valid numeric codes' do
|
18
|
+
let(:cusip) { CUSIP.new('837649128') }
|
19
|
+
|
20
|
+
it 'calculates the check digit' do
|
21
|
+
expect(cusip.check_digit).to eql(cusip.original_check_digit.to_i)
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'validates the check digit' do
|
25
|
+
expect(cusip).to be_valid
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
context 'Valid alphanumeric codes' do
|
30
|
+
let(:cusip) { CUSIP.new('125509BG3') }
|
31
|
+
|
32
|
+
it 'calculates the check digit' do
|
33
|
+
expect(cusip.check_digit).to eql(cusip.original_check_digit.to_i)
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'validates the check digit' do
|
37
|
+
expect(cusip).to be_valid
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
context 'Invalid check digit' do
|
42
|
+
let(:cusip) { CUSIP.new('125509BG4') }
|
43
|
+
|
44
|
+
it 'validates the check digit' do
|
45
|
+
expect(cusip).to_not be_valid
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
context '#to_isin' do
|
50
|
+
let(:cusip) { CUSIP.new('125509BG3') }
|
51
|
+
|
52
|
+
it 'returns an ISIN' do
|
53
|
+
expect(cusip.to_isin('US')).to be_instance_of(SecurityIdentifiers::ISIN)
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'calculates the ISIN check digit' do
|
57
|
+
expect(cusip.to_isin('US').check_digit).to_not be_nil
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'raises an error when an invalid country code is supplied' do
|
61
|
+
expect { cusip.to_isin('GB') }.to raise_error(SecurityIdentifiers::InvalidConversion)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
context '#to_s' do
|
66
|
+
let(:cusip) { CUSIP.new('125509BG3') }
|
67
|
+
|
68
|
+
it 'returns the whole identifier' do
|
69
|
+
expect(cusip.to_s).to eql('125509BG3')
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
context '#fix!' do
|
74
|
+
let(:cusip) { CUSIP.new('125509BG') }
|
75
|
+
|
76
|
+
it 'calculates the check digit' do
|
77
|
+
expect { cusip.fix! }.to change(cusip, :original_check_digit).from(nil).to(3)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
data/spec/isin_spec.rb
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ISIN do
|
4
|
+
|
5
|
+
it 'is invalid without a country code' do
|
6
|
+
expect { ISIN.new('120378331004') }.to raise_error(SecurityIdentifiers::InvalidFormat)
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'is invalid without 9 character alpha-numeric identifier' do
|
10
|
+
expect { ISIN.new('US03783315') }.to raise_error(SecurityIdentifiers::InvalidFormat)
|
11
|
+
end
|
12
|
+
|
13
|
+
context 'Without a check digit' do
|
14
|
+
let(:isin) { ISIN.new('US037833100') }
|
15
|
+
|
16
|
+
it 'is invalid' do
|
17
|
+
expect(isin).to_not be_valid
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
context 'Valid numeric ISIN' do
|
22
|
+
let(:isin) { ISIN.new('US0378331005') }
|
23
|
+
|
24
|
+
it 'calculates the check digit' do
|
25
|
+
expect(isin.check_digit).to eql(5)
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'validates the check digit' do
|
29
|
+
expect(isin).to be_valid
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
context 'Valid alphanumeric ISIN' do
|
34
|
+
let(:isin) { ISIN.new('AU0000XVGZA3') }
|
35
|
+
|
36
|
+
it 'calculates the check digit' do
|
37
|
+
expect(isin.check_digit).to eql(3)
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'validates the check digit' do
|
41
|
+
expect(isin).to be_valid
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
context 'Invalid check digit' do
|
46
|
+
let(:isin) { ISIN.new('US0378331004') }
|
47
|
+
|
48
|
+
it 'validates the check digit' do
|
49
|
+
expect(isin).to_not be_valid
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
context '#to_s' do
|
54
|
+
let(:isin) { ISIN.new('AU0000XVGZA3') }
|
55
|
+
|
56
|
+
it 'returns the whole identifier' do
|
57
|
+
expect(isin.to_s).to eql('AU0000XVGZA3')
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
context '#fix!' do
|
62
|
+
let(:isin) { ISIN.new('AU0000XVGZA') }
|
63
|
+
|
64
|
+
it 'calculates the check digit' do
|
65
|
+
expect { isin.fix! }.to change(isin, :original_check_digit).from(nil).to(3)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
data/spec/sedol_spec.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe SEDOL do
|
4
|
+
|
5
|
+
it 'is invalid without 6 character alpha-numeric identifier' do
|
6
|
+
expect { SEDOL.new('B0WNL') }.to raise_error(SecurityIdentifiers::InvalidFormat)
|
7
|
+
end
|
8
|
+
|
9
|
+
context 'Without a check digit' do
|
10
|
+
let(:sedol) { SEDOL.new('B0WNLY') }
|
11
|
+
|
12
|
+
it 'is invalid' do
|
13
|
+
expect(sedol).to_not be_valid
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
context 'Valid numeric codes' do
|
18
|
+
let(:sedol) { SEDOL.new('B0WNLY7') }
|
19
|
+
|
20
|
+
it 'calculates the check digit' do
|
21
|
+
expect(sedol.check_digit).to eql(sedol.original_check_digit.to_i)
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'validates the check digit' do
|
25
|
+
expect(sedol).to be_valid
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
context '#to_isin' do
|
30
|
+
let(:sedol) { SEDOL.new('B0WNLY7') }
|
31
|
+
|
32
|
+
it 'returns an ISIN' do
|
33
|
+
expect(sedol.to_isin('GB')).to be_instance_of(ISIN)
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'calculates the ISIN check digit' do
|
37
|
+
expect(sedol.to_isin('GB').check_digit).to_not be_nil
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'raises an error when an invalid country code is supplied' do
|
41
|
+
expect { sedol.to_isin('US') }.to raise_error(SecurityIdentifiers::InvalidConversion)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
context '#to_s' do
|
46
|
+
let(:sedol) { SEDOL.new('B0WNLY7') }
|
47
|
+
|
48
|
+
it 'returns the whole identifier' do
|
49
|
+
expect(sedol.to_s).to eql('B0WNLY7')
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
context '#fix!' do
|
54
|
+
let(:sedol) { SEDOL.new('B0WNLY') }
|
55
|
+
|
56
|
+
it 'calculates the check digit' do
|
57
|
+
expect { sedol.fix! }.to change(sedol, :original_check_digit).from(nil).to(7)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'bundler/setup'
|
2
|
+
require 'active_model'
|
3
|
+
require 'security_identifiers'
|
4
|
+
|
5
|
+
include SecurityIdentifiers
|
6
|
+
|
7
|
+
class Security
|
8
|
+
include ActiveModel::Validations
|
9
|
+
include SecurityIdentifiers::Validations
|
10
|
+
|
11
|
+
attr_accessor :cusip, :isin, :sedol
|
12
|
+
|
13
|
+
def initialize(options = {})
|
14
|
+
@cusip = options[:cusip]
|
15
|
+
@isin = options[:isin]
|
16
|
+
@sedol = options[:sedol]
|
17
|
+
end
|
18
|
+
|
19
|
+
validates :cusip, cusip: true, allow_blank: true
|
20
|
+
validates :isin, isin: true, allow_blank: true
|
21
|
+
validates :sedol, sedol: true, allow_blank: true
|
22
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Validations::CusipValidator do
|
4
|
+
|
5
|
+
context 'with invalid cusip' do
|
6
|
+
|
7
|
+
subject { Security.new(cusip: '5509BG3') }
|
8
|
+
|
9
|
+
it 'is invalid' do
|
10
|
+
expect(subject).not_to be_valid
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
context 'with cusip' do
|
15
|
+
|
16
|
+
subject { Security.new(cusip: '125509BG3') }
|
17
|
+
|
18
|
+
it 'is valid' do
|
19
|
+
expect(subject).to be_valid
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Validations::IsinValidator do
|
4
|
+
|
5
|
+
context 'without isin' do
|
6
|
+
subject { Security.new(isin: 'US03783310') }
|
7
|
+
|
8
|
+
it 'is invalid' do
|
9
|
+
expect(subject).not_to be_valid
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
context 'with isin' do
|
14
|
+
subject { Security.new(isin: 'US0378331005') }
|
15
|
+
|
16
|
+
it 'is valid' do
|
17
|
+
expect(subject).to be_valid
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Validations::SedolValidator do
|
4
|
+
|
5
|
+
context 'without sedol' do
|
6
|
+
subject { Security.new(sedol: 'BOWN') }
|
7
|
+
|
8
|
+
it 'is invalid' do
|
9
|
+
expect(subject).not_to be_valid
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
context 'with sedol' do
|
14
|
+
|
15
|
+
subject { Security.new(sedol: 'B0WNLY7') }
|
16
|
+
|
17
|
+
it 'is valid' do
|
18
|
+
expect(subject).to be_valid
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,159 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: security_identifiers
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kieran Johnson
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-02-11 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: activemodel
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: guard
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 2.12.1
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 2.12.1
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: guard-rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 4.5.0
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 4.5.0
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 3.2.0
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 3.2.0
|
97
|
+
description: Security Identifier validation library for Ruby
|
98
|
+
email:
|
99
|
+
- hello@invisiblelines.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- ".gitignore"
|
105
|
+
- ".travis.yml"
|
106
|
+
- Gemfile
|
107
|
+
- Guardfile
|
108
|
+
- LICENSE.txt
|
109
|
+
- README.md
|
110
|
+
- Rakefile
|
111
|
+
- lib/security_identifiers.rb
|
112
|
+
- lib/security_identifiers/base.rb
|
113
|
+
- lib/security_identifiers/cusip.rb
|
114
|
+
- lib/security_identifiers/isin.rb
|
115
|
+
- lib/security_identifiers/sedol.rb
|
116
|
+
- lib/security_identifiers/validations/cusip_validator.rb
|
117
|
+
- lib/security_identifiers/validations/isin_validator.rb
|
118
|
+
- lib/security_identifiers/validations/sedol_validator.rb
|
119
|
+
- lib/security_identifiers/version.rb
|
120
|
+
- security_identifiers.gemspec
|
121
|
+
- spec/cusip_spec.rb
|
122
|
+
- spec/isin_spec.rb
|
123
|
+
- spec/sedol_spec.rb
|
124
|
+
- spec/spec_helper.rb
|
125
|
+
- spec/validations/cusip_validator_spec.rb
|
126
|
+
- spec/validations/isin_validator_spec.rb
|
127
|
+
- spec/validations/sedol_validator_spec.rb
|
128
|
+
homepage: ''
|
129
|
+
licenses:
|
130
|
+
- MIT
|
131
|
+
metadata: {}
|
132
|
+
post_install_message:
|
133
|
+
rdoc_options: []
|
134
|
+
require_paths:
|
135
|
+
- lib
|
136
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
137
|
+
requirements:
|
138
|
+
- - ">="
|
139
|
+
- !ruby/object:Gem::Version
|
140
|
+
version: '0'
|
141
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
requirements: []
|
147
|
+
rubyforge_project:
|
148
|
+
rubygems_version: 2.4.5
|
149
|
+
signing_key:
|
150
|
+
specification_version: 4
|
151
|
+
summary: Security Identifier validation library for Ruby
|
152
|
+
test_files:
|
153
|
+
- spec/cusip_spec.rb
|
154
|
+
- spec/isin_spec.rb
|
155
|
+
- spec/sedol_spec.rb
|
156
|
+
- spec/spec_helper.rb
|
157
|
+
- spec/validations/cusip_validator_spec.rb
|
158
|
+
- spec/validations/isin_validator_spec.rb
|
159
|
+
- spec/validations/sedol_validator_spec.rb
|