identifiers_valid 0.1.6

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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/identifiers_valid.rb +92 -0
  3. metadata +45 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: deb638e02148f9c6f71455688cb83101647e2496a1fe8b81a620c5f17593c550
4
+ data.tar.gz: 2001a727c5c62eaf0dd7c2c431276ab74455ddd13418de159ddeab9693f59565
5
+ SHA512:
6
+ metadata.gz: 94a6ed391e69fe1274e04e58c1ea0c75fa9dc976b7f6f9fa70bdd0807dee27625e5503d32f42addbc2c9bb28e492d417c1a01175dba746063cabdab546a1de10
7
+ data.tar.gz: 6d1c108cc4e2f1914e0a07a8de047635f21278b063c812f90374b3ebce68302759886b9f282919c0d60caeff04ae45e1b2544734feb3bdca4c8def92d733edb7
@@ -0,0 +1,92 @@
1
+ ##
2
+ #
3
+ module IdentifiersValid
4
+ ISIN_FORMAT = '^[A-Z]{2}[A-Z0-9]{9}[0-9]$'.freeze
5
+ SEDOL_FORMAT = '^[A-Z0-9]{6}\d{1}$'.freeze
6
+ CUSIP_FORMAT = '^[A-Z0-9]{8}\d{1}$'.freeze
7
+ ISIN_REGEX = Regexp.new(ISIN_FORMAT).freeze
8
+ LETTER_TO_CODE_VALUE = 55 # Ascii code for 'A' == 65 && 'A' == 10 in isin calculation code
9
+ CUSIP_SPECIAL_CHAR = { '*' => 36, '@' => 37, "\#" => 38 }.freeze
10
+ SEDOL_VALID_CHAR = '0123456789BCDFGHJKLMNPQRSTVWXYZ'.freeze
11
+ SEDOL_WEIGHT = [1, 3, 1, 7, 3, 9].freeze
12
+
13
+ def self.identifier_valid?(value, field_key)
14
+ case field_key.to_s.upcase
15
+ when 'ISIN'
16
+ isin_valid?(value)
17
+ when 'SEDOL'
18
+ sedol_valid?(value)
19
+ when 'CUSIP'
20
+ cusip_valid?(value)
21
+ end
22
+ end
23
+
24
+ def self.identifier_invalid?(value, field_key)
25
+ !identifier_valid?(value, field_key)
26
+ end
27
+
28
+ def self.cusip_valid?(cusip)
29
+ return nil if cusip.nil? || cusip.size != 9
30
+
31
+ proc_cusip = cusip.split(//)
32
+ key = proc_cusip.pop
33
+ sum = proc_cusip.each_with_index.map do |char, i|
34
+ v = check_cusip_char(char, i)
35
+ (v / 10).to_i + (v % 10)
36
+ end.inject(:+)
37
+
38
+ valid?(sum, key) ? cusip : nil
39
+ end
40
+
41
+ def self.check_cusip_char(char, index)
42
+ v = if char.match?(/\d/)
43
+ char.to_i
44
+ elsif CUSIP_SPECIAL_CHAR[char]
45
+ CUSIP_SPECIAL_CHAR[char]
46
+ else
47
+ char.ord - LETTER_TO_CODE_VALUE + 9
48
+ end
49
+ (index % 2).zero? ? v : v * 2
50
+ end
51
+
52
+ def self.cusip_invalid?(cusip)
53
+ !cusip_valid?(cusip)
54
+ end
55
+
56
+ def self.sedol_valid?(sedol)
57
+ return nil if sedol.nil? || sedol.size != 7
58
+
59
+ proc_sedol = sedol.split(//)
60
+ proc_sedol.each { |char| return nil unless SEDOL_VALID_CHAR.include?(char) }
61
+ key = proc_sedol.pop
62
+ sum = proc_sedol.zip(SEDOL_WEIGHT)
63
+ .map { |ch, weight| ch.to_i(36) * weight }.inject(:+)
64
+ valid?(sum, key) ? sedol : nil
65
+ end
66
+
67
+ def self.sedol_invalid?(sedol)
68
+ !sedol_valid?(sedol)
69
+ end
70
+
71
+ def self.isin_valid?(isin)
72
+ return nil if isin.nil? || !isin.match?(ISIN_REGEX)
73
+
74
+ proc_isin = isin.split(//)
75
+ key = proc_isin.pop.to_i
76
+ proc_isin = proc_isin.map do |e|
77
+ e.match?(/[A-Z]/) ? (e.ord - LETTER_TO_CODE_VALUE).to_s.split(//) : e
78
+ end.flatten.reverse
79
+ proc_isin.each_index do |i|
80
+ proc_isin[i] = (proc_isin[i].to_i * 2).to_s.split(//) if (i % 2).zero?
81
+ end.flatten!
82
+ ((key + proc_isin.map(&:to_i).inject(:+)) % 10).zero? ? isin : nil
83
+ end
84
+
85
+ def self.isin_invalid?(isin)
86
+ !isin_valid?(isin)
87
+ end
88
+
89
+ def self.valid?(sum, key)
90
+ ((10 - (sum % 10)) % 10).to_s == key
91
+ end
92
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: identifiers_valid
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.6
5
+ platform: ruby
6
+ authors:
7
+ - esupper
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-12-06 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email: edouard.supper@epitech.eu
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/identifiers_valid.rb
20
+ homepage: https://rubygems.org
21
+ licenses:
22
+ - Ruby
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 2.7.8
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: A gem to check ISIN/cusip/sedol validity
45
+ test_files: []