identitycode 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e8b44a31ac2b3fe6561a1246c9c05bb6aedc8c99
4
- data.tar.gz: 902ae92f359bca5ab11d0f2194c22631a89fa92b
3
+ metadata.gz: 93fd0fa7df0107fcf135937b1a2f13a82f521510
4
+ data.tar.gz: c53c1a7b1be7d91ce4337f2280312881ca04f54d
5
5
  SHA512:
6
- metadata.gz: 035fc895ae32c37d0c46e4d9d0af4c8ddd9c5bab11bed050077a7e918e0507ad1f2b9cec18c2e8d878bea9588e43f0bad315cb9d76bc93c721b20a27d192daa5
7
- data.tar.gz: 944d1a39960e3e60fbed5cba63c5bdd9c72aed397b284709e455a706afdfbbecec2300e97c2d32bc6e008a377e10807bc4f39c832778e5e6d799aedd2fc08294
6
+ metadata.gz: 506c9cbbd3619f091711ed47d94a222bba5049a2880ccc301aecdc56c82ecb1d43aaee8b076603d427f351d0014cf803aea6a3a3a0c3ccac80c1e88dc64bf836
7
+ data.tar.gz: c1089c3c1e5c9bb5594043f9716a1766e315cf78eed6fa2e90445dfc5d6c49ed3c5f8a692e6218c6ae7e1714460175941030e14086f12970f94619f8942d3a9b
data/README.md CHANGED
@@ -31,6 +31,8 @@ This gem supports Estonian and Latvian identity codes. Just specify `EE` or `LV`
31
31
  => true
32
32
  > IdentityCode::LV.valid?('20128315289')
33
33
  => true
34
+ > IdentityCode.valid?(country: :lv, code: '20128315289')
35
+ => true
34
36
  > code.sex
35
37
  => 'M'
36
38
  > code.birth_date.to_s
@@ -38,6 +40,8 @@ This gem supports Estonian and Latvian identity codes. Just specify `EE` or `LV`
38
40
  > code.age
39
41
  => 31
40
42
  # Generate random valid identity code
43
+ > IdentityCode.generate(country: :ee)
44
+ => '37504163700'
41
45
  > IdentityCode::EE.generate
42
46
  => '37504163700'
43
47
  > IdentityCode::EE.generate(sex: 'M', year: 1983, month: 12, day: 20)
data/identitycode.gemspec CHANGED
@@ -9,8 +9,8 @@ Gem::Specification.new do |spec|
9
9
  spec.authors = ['Artem Pakk']
10
10
  spec.email = ['apakk@me.com']
11
11
 
12
- spec.summary = %q{Ruby gem to generate and validate Estonian and Latvian identity codes}
13
- spec.description = %q{Ruby gem to generate and validate Estonian and Latvian identity codes}
12
+ spec.summary = %q{Ruby gem to generate and validate Estonian, Latvian, and Polish identity codes}
13
+ spec.description = %q{Ruby gem to generate and validate Estonian, Latvian, and Polish identity codes}
14
14
  spec.homepage = 'https://github.com/defeed/identitycode'
15
15
  spec.license = "MIT"
16
16
 
@@ -19,6 +19,6 @@ Gem::Specification.new do |spec|
19
19
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
20
  spec.require_paths = ['lib']
21
21
 
22
- spec.add_development_dependency 'bundler', '~> 1.10'
22
+ spec.add_development_dependency 'bundler', '~> 1.11'
23
23
  spec.add_development_dependency 'rake', "~> 10.0"
24
24
  end
@@ -0,0 +1,155 @@
1
+ require 'date'
2
+
3
+ module IdentityCode
4
+ class PL
5
+ def self.generate(opts = {})
6
+ sex = opts[:sex] || (rand.round == 0 ? :M : :F)
7
+ year = opts[:year] || rand(Date.today.year - 90..Date.today.year - 1)
8
+ year = rand(Date.today.year - 50..Date.today.year - 19) if opts[:safe_age]
9
+ month = opts[:month] || rand(1..12)
10
+ calc_month = begin
11
+ offset = case year.to_s[0..1]
12
+ when '18' then 80
13
+ when '19' then 0
14
+ when '20' then 20
15
+ end
16
+ month + offset
17
+ end
18
+ day = opts[:day] || rand(1..NUM_DAYS[month])
19
+
20
+ sex_digit = [0, 2, 4, 6, 8].sample
21
+ sex_digit += 1 if sex.upcase.to_sym == :M
22
+
23
+ result = "%02d" % year.to_s[2..3].to_i
24
+ result += "%02d" % calc_month
25
+ result += "%02d" % day
26
+ result += "%03d" % rand(1..999)
27
+ result += sex_digit.to_s
28
+ result += new(result).control_code.to_s
29
+ end
30
+
31
+ def self.valid?(code)
32
+ new(code).valid?
33
+ end
34
+
35
+ def initialize(code)
36
+ @code = code.to_s
37
+ end
38
+
39
+ def valid?
40
+ @code.length == 11 &&
41
+ @code[10].chr.to_i == control_code
42
+ end
43
+
44
+ def birth_date
45
+ return unless valid?
46
+ year = century + @code[0..1].to_i
47
+ day = @code[4..5].to_i
48
+ return unless Date.valid_date?(year, month, day)
49
+ Date.new(year, month, day)
50
+ end
51
+
52
+ def month
53
+ raw_num = @code[2..3].to_i
54
+
55
+ case raw_num
56
+ when 81..92 then raw_num - 80
57
+ when 1..12 then raw_num
58
+ when 21..32 then raw_num - 20
59
+ end
60
+ end
61
+
62
+ def age
63
+ return unless valid?
64
+ now = Time.now.utc.to_date
65
+ now.year - (birth_date.year + IdentityCode::age_correction(birth_date))
66
+ end
67
+
68
+ def sex
69
+ return unless valid?
70
+ @code[9].to_i.odd? ? :M : :F
71
+ end
72
+
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
+ private
84
+
85
+ def century
86
+ c = @code[2..3].to_i
87
+
88
+ case c
89
+ when 81..92 then 1800
90
+ when 1..12 then 1900
91
+ when 21..32 then 2000
92
+ end
93
+ end
94
+ end
95
+ end
96
+
97
+ # CONTROL_SUM = [9, 7, 3, 1, 9, 7, 3, 1, 9, 7]
98
+ #
99
+ # def length_valid?
100
+ # identity_code && identity_code.length == 11
101
+ # end
102
+ #
103
+ # def checksum_valid?
104
+ # id_ary = identity_code.split(//).map(&:to_i)
105
+ # sum = 0
106
+ #
107
+ # (0...CONTROL_SUM.count).each { |i| sum += id_ary[i] * CONTROL_SUM[i] }
108
+ #
109
+ # sum % 10 == id_ary[10]
110
+ # end
111
+ #
112
+ # def calculate_birthday
113
+ # @birthday = Date.new(year, month, day)
114
+ # rescue
115
+ # @birthday = nil
116
+ # end
117
+ # alias_method :birthday_valid?, :calculate_birthday
118
+ #
119
+ # def century
120
+ # c = identity_code[2..3].to_i
121
+ #
122
+ # case c
123
+ # when 81..92
124
+ # '18'
125
+ # when 1..12
126
+ # '19'
127
+ # when 21..32
128
+ # '20'
129
+ # end
130
+ # end
131
+ #
132
+ # def year
133
+ # (century + identity_code[0..1]).to_i
134
+ # end
135
+ #
136
+ # def month
137
+ # raw_num = identity_code[2..3].to_i
138
+ #
139
+ # case raw_num
140
+ # when 81..92
141
+ # raw_num - 80
142
+ # when 1..12
143
+ # raw_num
144
+ # when 21..32
145
+ # raw_num - 20
146
+ # end
147
+ # end
148
+ #
149
+ # def day
150
+ # identity_code[4..5].to_i
151
+ # end
152
+ #
153
+ # def available_gender
154
+ # identity_code[9].to_i.even? ? :F : :M
155
+ # end
@@ -1,3 +1,3 @@
1
1
  module IdentityCode
2
- VERSION = '0.2.1'
2
+ VERSION = '0.2.2'
3
3
  end
data/lib/identity_code.rb CHANGED
@@ -1,28 +1,29 @@
1
1
  require 'identity_code/version'
2
2
  require 'identity_code/ee'
3
3
  require 'identity_code/lv'
4
+ require 'identity_code/pl'
4
5
 
5
6
  module IdentityCode
6
7
  NUM_DAYS = {
7
- 1 => 31,
8
- 2 => 28,
9
- 3 => 31,
10
- 4 => 30,
11
- 5 => 31,
12
- 6 => 30,
13
- 7 => 31,
14
- 8 => 31,
15
- 9 => 30,
8
+ 1 => 31,
9
+ 2 => 28,
10
+ 3 => 31,
11
+ 4 => 30,
12
+ 5 => 31,
13
+ 6 => 30,
14
+ 7 => 31,
15
+ 8 => 31,
16
+ 9 => 30,
16
17
  10 => 31,
17
18
  11 => 30,
18
19
  12 => 31
19
20
  }.freeze
20
21
 
21
- SUPPORTED_COUNTRY_CODES = %i(ee lv).freeze
22
+ SUPPORTED_COUNTRY_CODES = %i(ee lv pl).freeze
22
23
 
23
24
  def self.generate(opts = {})
24
25
  country_code = opts[:country]
25
- raise 'Country param is missing or invalid (ee or lv)' unless begin
26
+ raise 'Country param is missing or invalid (ee | lv | pl)' unless begin
26
27
  country_code &&
27
28
  SUPPORTED_COUNTRY_CODES.include?(country_code.downcase.to_sym)
28
29
  end
@@ -32,7 +33,7 @@ module IdentityCode
32
33
 
33
34
  def self.valid?(opts = {})
34
35
  country_code = opts.delete(:country)
35
- raise 'Country param is missing or invalid (ee or lv)' unless begin
36
+ raise 'Country param is missing or invalid (ee | lv | pl)' unless begin
36
37
  country_code &&
37
38
  SUPPORTED_COUNTRY_CODES.include?(country_code.downcase.to_sym)
38
39
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: identitycode
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Artem Pakk
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-02-14 00:00:00.000000000 Z
11
+ date: 2017-03-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.10'
19
+ version: '1.11'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '1.10'
26
+ version: '1.11'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -38,7 +38,8 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.0'
41
- description: Ruby gem to generate and validate Estonian and Latvian identity codes
41
+ description: Ruby gem to generate and validate Estonian, Latvian, and Polish identity
42
+ codes
42
43
  email:
43
44
  - apakk@me.com
44
45
  executables: []
@@ -57,6 +58,7 @@ files:
57
58
  - lib/identity_code.rb
58
59
  - lib/identity_code/ee.rb
59
60
  - lib/identity_code/lv.rb
61
+ - lib/identity_code/pl.rb
60
62
  - lib/identity_code/version.rb
61
63
  homepage: https://github.com/defeed/identitycode
62
64
  licenses:
@@ -81,5 +83,6 @@ rubyforge_project:
81
83
  rubygems_version: 2.5.1
82
84
  signing_key:
83
85
  specification_version: 4
84
- summary: Ruby gem to generate and validate Estonian and Latvian identity codes
86
+ summary: Ruby gem to generate and validate Estonian, Latvian, and Polish identity
87
+ codes
85
88
  test_files: []