identitycode 0.2.2 → 0.2.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 93fd0fa7df0107fcf135937b1a2f13a82f521510
4
- data.tar.gz: c53c1a7b1be7d91ce4337f2280312881ca04f54d
3
+ metadata.gz: e23ddc3445d7c98830f65b367a2856e3642fde04
4
+ data.tar.gz: c8b16f6c4b3709a24285368cf94cf907c9c16f25
5
5
  SHA512:
6
- metadata.gz: 506c9cbbd3619f091711ed47d94a222bba5049a2880ccc301aecdc56c82ecb1d43aaee8b076603d427f351d0014cf803aea6a3a3a0c3ccac80c1e88dc64bf836
7
- data.tar.gz: c1089c3c1e5c9bb5594043f9716a1766e315cf78eed6fa2e90445dfc5d6c49ed3c5f8a692e6218c6ae7e1714460175941030e14086f12970f94619f8942d3a9b
6
+ metadata.gz: 7b00105ee6789bbcb39df92faaf712bf26da2ce8d00605fdb49ceec454068890f3a7f438743053007d6b89688f5984a29b0230048f33ea355b9bd78b63b16316
7
+ data.tar.gz: 5fdc313dcacc2877247e6a4192632d226aceaf98087caab270d2566b83768cf60a205df733d31a1fb5a1c5f3afd3b7d273c79cbc9bad16051ae094ef4dbb30d4
data/identitycode.gemspec CHANGED
@@ -21,4 +21,5 @@ Gem::Specification.new do |spec|
21
21
 
22
22
  spec.add_development_dependency 'bundler', '~> 1.11'
23
23
  spec.add_development_dependency 'rake', "~> 10.0"
24
+ spec.add_development_dependency 'pry'
24
25
  end
@@ -40,20 +40,35 @@ module IdentityCode
40
40
  result += "%02d" % day
41
41
  result += HOSPITALS[(rand * HOSPITALS.size - 1).round]
42
42
  result += rand(0..9).to_s
43
- result += new(result).control_code.to_s
43
+ result += control_digit(result).to_s
44
44
  end
45
45
 
46
46
  def self.valid?(code)
47
47
  new(code).valid?
48
48
  end
49
49
 
50
+ def self.control_digit(base)
51
+ scales1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 1]
52
+ checknum = scales1.each_with_index.map do |scale, i|
53
+ base[i].chr.to_i * scale
54
+ end.inject(0, :+) % 11
55
+ return checknum unless checknum == 10
56
+
57
+ scales2 = [3, 4, 5, 6, 7, 8, 9, 1, 2, 3]
58
+ checknum = scales2.each_with_index.map do |scale, i|
59
+ base[i].chr.to_i * scale
60
+ end.inject(0, :+) % 11
61
+
62
+ checknum == 10 ? 0 : checknum
63
+ end
64
+
50
65
  def initialize(code)
51
66
  @code = code.to_s
52
67
  end
53
68
 
54
69
  def valid?
55
70
  @code.length == 11 &&
56
- @code[10].chr.to_i == control_code
71
+ @code[10].chr.to_i == self.class.control_digit(@code)
57
72
  end
58
73
 
59
74
  def birth_date
@@ -76,21 +91,6 @@ module IdentityCode
76
91
  @code[0].to_i.odd? ? :M : :F
77
92
  end
78
93
 
79
- def control_code
80
- scales1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 1]
81
- checknum = scales1.each_with_index.map do |scale, i|
82
- @code[i].chr.to_i * scale
83
- end.inject(0, :+) % 11
84
- return checknum unless checknum == 10
85
-
86
- scales2 = [3, 4, 5, 6, 7, 8, 9, 1, 2, 3]
87
- checknum = scales2.each_with_index.map do |scale, i|
88
- @code[i].chr.to_i * scale
89
- end.inject(0, :+) % 11
90
-
91
- checknum == 10 ? 0 : checknum
92
- end
93
-
94
94
  private
95
95
 
96
96
  def century
@@ -26,18 +26,31 @@ module IdentityCode
26
26
  result += "%02d" % month
27
27
  result += "%02d" % year.to_s[2..3].to_i
28
28
  result += '-' if opts[:separator]
29
- result += century_code
30
- result += "%03d" % rand(0..999).to_s
31
- result += new(result).control_code.to_s
29
+ result += century_code.to_s
30
+ result += "%03d" % rand(1..999).to_s
31
+ result += control_digit(result).to_s
32
32
  end
33
33
 
34
34
  def self.valid?(code)
35
35
  new(code).valid?
36
36
  end
37
37
 
38
+ def self.control_digit(base)
39
+ array = base.gsub('-', '').split('').map(&:to_i)
40
+ multipliers = [1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
41
+ hash = Hash[multipliers.zip(array)]
42
+
43
+ check = 0
44
+ hash.map do |k, v|
45
+ check += k * v
46
+ end
47
+
48
+ ((1 - check) % 11) % 10
49
+ end
50
+
38
51
  def valid?
39
52
  @code.length == 11 &&
40
- @code[10].chr.to_i == control_code
53
+ @code[10].chr.to_i == self.class.control_digit(@code)
41
54
  end
42
55
 
43
56
  def birth_date
@@ -55,19 +68,6 @@ module IdentityCode
55
68
  now.year - (birth_date.year + IdentityCode.age_correction(birth_date))
56
69
  end
57
70
 
58
- def control_code
59
- array = @code.split('').map(&:to_i)
60
- multipliers = [1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
61
- hash = Hash[multipliers.zip(array)]
62
-
63
- check = 0
64
- hash.map do |k, v|
65
- check += k * v
66
- end
67
-
68
- ((1 - check) % 11) % 10
69
- end
70
-
71
71
  private
72
72
 
73
73
  def century
@@ -25,20 +25,30 @@ module IdentityCode
25
25
  result += "%02d" % day
26
26
  result += "%03d" % rand(1..999)
27
27
  result += sex_digit.to_s
28
- result += new(result).control_code.to_s
28
+ result += control_digit(result).to_s
29
29
  end
30
30
 
31
31
  def self.valid?(code)
32
32
  new(code).valid?
33
33
  end
34
34
 
35
+ def self.control_digit(base)
36
+ multipliers = [9, 7, 3, 1, 9, 7, 3, 1, 9, 7]
37
+ id_ary = base.split(//).map(&:to_i)
38
+ sum = 0
39
+
40
+ (0...multipliers.count).each { |i| sum += id_ary[i] * multipliers[i] }
41
+
42
+ sum % 10
43
+ end
44
+
35
45
  def initialize(code)
36
46
  @code = code.to_s
37
47
  end
38
48
 
39
49
  def valid?
40
50
  @code.length == 11 &&
41
- @code[10].chr.to_i == control_code
51
+ @code[10].chr.to_i == self.class.control_digit(@code)
42
52
  end
43
53
 
44
54
  def birth_date
@@ -70,16 +80,6 @@ module IdentityCode
70
80
  @code[9].to_i.odd? ? :M : :F
71
81
  end
72
82
 
73
- def control_code
74
- multipliers = [9, 7, 3, 1, 9, 7, 3, 1, 9, 7]
75
- id_ary = @code.split(//).map(&:to_i)
76
- sum = 0
77
-
78
- (0...multipliers.count).each { |i| sum += id_ary[i] * multipliers[i] }
79
-
80
- sum % 10
81
- end
82
-
83
83
  private
84
84
 
85
85
  def century
@@ -1,3 +1,3 @@
1
1
  module IdentityCode
2
- VERSION = '0.2.2'
2
+ VERSION = '0.2.3'
3
3
  end
data/lib/identity_code.rb CHANGED
@@ -31,6 +31,17 @@ module IdentityCode
31
31
  Object.const_get("IdentityCode::#{country_code.upcase}").generate(opts)
32
32
  end
33
33
 
34
+ def self.validate(opts = {})
35
+ country_code = opts.delete(:country)
36
+ raise 'Country param is missing or invalid (ee | lv | pl)' unless begin
37
+ country_code &&
38
+ SUPPORTED_COUNTRY_CODES.include?(country_code.downcase.to_sym)
39
+ end
40
+
41
+ code = opts.delete(:code)
42
+ Object.const_get("IdentityCode::#{country_code.upcase}").new(code)
43
+ end
44
+
34
45
  def self.valid?(opts = {})
35
46
  country_code = opts.delete(:country)
36
47
  raise 'Country param is missing or invalid (ee | lv | pl)' unless begin
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: identitycode
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Artem Pakk
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
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'
41
55
  description: Ruby gem to generate and validate Estonian, Latvian, and Polish identity
42
56
  codes
43
57
  email: