pesel 0.9.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.
- data/.gitignore +3 -0
- data/Gemfile +4 -0
- data/README +65 -0
- data/Rakefile +2 -0
- data/lib/pesel.rb +103 -0
- data/lib/version.rb +3 -0
- data/pesel.gemspec +21 -0
- data/test/pesel_test.rb +79 -0
- metadata +72 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
Pesel
|
2
|
+
=====
|
3
|
+
|
4
|
+
This gem provides the Pesel class to check the PESEL number
|
5
|
+
and get information about its owner.
|
6
|
+
|
7
|
+
PESEL (Polish Powszechny Elektroniczny System Ewidencji Ludności,
|
8
|
+
Universal Electronic System for Registration of the Population)
|
9
|
+
is the national identification number used in Poland since 1979.
|
10
|
+
|
11
|
+
The PESEL number is mandatory for all permanent residents of Poland
|
12
|
+
and for temporary residents living in Poland for over 2 months.
|
13
|
+
Applicants for Polish citizenship must request a PESEL number together
|
14
|
+
with their passport application.
|
15
|
+
|
16
|
+
[http://en.wikipedia.org/wiki/PESEL]
|
17
|
+
|
18
|
+
Interesting information about PESEL (in Polish language):
|
19
|
+
http://wipos.p.lodz.pl/zylla/ut/pesel.html
|
20
|
+
|
21
|
+
If you are looking for the Pesel as a plugin:
|
22
|
+
http://github.com/macuk/pesel-plugin
|
23
|
+
|
24
|
+
Installation
|
25
|
+
============
|
26
|
+
|
27
|
+
gem install pesel
|
28
|
+
|
29
|
+
Example
|
30
|
+
=======
|
31
|
+
|
32
|
+
Standalone class:
|
33
|
+
|
34
|
+
require 'pesel'
|
35
|
+
|
36
|
+
pesel = Pesel.new('75120804355')
|
37
|
+
pesel.valid? # true
|
38
|
+
pesel.birth_date # Date object with '1975-12-08' date
|
39
|
+
pesel.female? # false
|
40
|
+
pesel.male? # true
|
41
|
+
pesel.gender # :male
|
42
|
+
pesel.number # '75120804355'
|
43
|
+
|
44
|
+
pesel = Pesel.new('00000000000')
|
45
|
+
pesel.valid? # false
|
46
|
+
pesel.birth_date # raises Pesel::NumberInvalid exception
|
47
|
+
pesel.female? # raises Pesel::NumberInvalid exception
|
48
|
+
pesel.male? # raises Pesel::NumberInvalid exception
|
49
|
+
pesel.gender # raises Pesel::NumberInvalid exception
|
50
|
+
pesel.number # raises Pesel::NumberInvalid exception
|
51
|
+
|
52
|
+
ActiveRecord:
|
53
|
+
|
54
|
+
class Person < ActiveRecord::Base
|
55
|
+
validate :check_pesel
|
56
|
+
|
57
|
+
private
|
58
|
+
def check_pesel
|
59
|
+
p = Pesel.new(pesel)
|
60
|
+
errors[:pesel] << 'Invalid PESEL number' unless p.valid?
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
|
65
|
+
Copyright (c) 2009-2010 Piotr Macuk, released under the MIT license
|
data/Rakefile
ADDED
data/lib/pesel.rb
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
class Pesel
|
2
|
+
class NumberInvalid < Exception; end
|
3
|
+
|
4
|
+
attr_reader :number
|
5
|
+
|
6
|
+
def initialize(number)
|
7
|
+
@number = number.to_s
|
8
|
+
@digits = @number.split('')
|
9
|
+
@valid = validate
|
10
|
+
end
|
11
|
+
|
12
|
+
def to_s
|
13
|
+
number
|
14
|
+
end
|
15
|
+
|
16
|
+
def valid?
|
17
|
+
@valid
|
18
|
+
end
|
19
|
+
|
20
|
+
def birth_date
|
21
|
+
raise NumberInvalid unless valid?
|
22
|
+
@birth_date
|
23
|
+
end
|
24
|
+
|
25
|
+
def female?
|
26
|
+
raise NumberInvalid unless valid?
|
27
|
+
@digits[9].to_i % 2 == 0
|
28
|
+
end
|
29
|
+
|
30
|
+
def male?
|
31
|
+
raise NumberInvalid unless valid?
|
32
|
+
@digits[9].to_i % 2 != 0
|
33
|
+
end
|
34
|
+
|
35
|
+
def gender
|
36
|
+
raise NumberInvalid unless valid?
|
37
|
+
female? ? :female : :male
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
def validate
|
42
|
+
has_proper_length and \
|
43
|
+
contains_only_digits and \
|
44
|
+
has_proper_date and \
|
45
|
+
has_proper_control_digit
|
46
|
+
end
|
47
|
+
|
48
|
+
def has_proper_length
|
49
|
+
@number.size == 11
|
50
|
+
end
|
51
|
+
|
52
|
+
def contains_only_digits
|
53
|
+
@number =~ /^\d+$/
|
54
|
+
end
|
55
|
+
|
56
|
+
def has_proper_date
|
57
|
+
y, m, d = @number[0..1], @number[2..3], @number[4..5]
|
58
|
+
y, m = convert_year_and_month(y, m)
|
59
|
+
begin
|
60
|
+
require 'date'
|
61
|
+
@birth_date = Date::parse("#{y}-#{m}-#{d}")
|
62
|
+
rescue ArgumentError
|
63
|
+
@birth_date = nil
|
64
|
+
end
|
65
|
+
not @birth_date.nil?
|
66
|
+
end
|
67
|
+
|
68
|
+
def weights
|
69
|
+
[1, 3, 7, 9, 1, 3, 7, 9, 1, 3, 1]
|
70
|
+
end
|
71
|
+
|
72
|
+
def has_proper_control_digit
|
73
|
+
index = 0
|
74
|
+
sum = 0
|
75
|
+
@digits.each do |digit|
|
76
|
+
sum += digit.to_i * weights[index]
|
77
|
+
index += 1
|
78
|
+
end
|
79
|
+
sum % 10 == 0
|
80
|
+
end
|
81
|
+
|
82
|
+
def mods
|
83
|
+
[
|
84
|
+
[1800..1899, 80],
|
85
|
+
[2200..2299, 60],
|
86
|
+
[2100..2199, 40],
|
87
|
+
[2000..2099, 20],
|
88
|
+
[1900..1999, 00],
|
89
|
+
]
|
90
|
+
end
|
91
|
+
|
92
|
+
def convert_year_and_month(year, month)
|
93
|
+
y, m = year.to_i, month.to_i
|
94
|
+
mods.each do |range, mod|
|
95
|
+
if m > mod
|
96
|
+
y += range.begin
|
97
|
+
m -= mod
|
98
|
+
break
|
99
|
+
end
|
100
|
+
end
|
101
|
+
[y, m]
|
102
|
+
end
|
103
|
+
end
|
data/lib/version.rb
ADDED
data/pesel.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "pesel"
|
7
|
+
s.version = Pesel::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Piotr Macuk"]
|
10
|
+
s.email = ["piotr@macuk.pl"]
|
11
|
+
s.homepage = "http://github.com/macuk/pesel"
|
12
|
+
s.summary = %q{PESEL validation and information}
|
13
|
+
s.description = %q{Pesel class to check PESEL number and get information about its owner}
|
14
|
+
|
15
|
+
s.rubyforge_project = "pesel"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
end
|
data/test/pesel_test.rb
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'pesel'
|
3
|
+
|
4
|
+
class PeselTest < Test::Unit::TestCase
|
5
|
+
Valid = %w(75120804355 74082610668 02221407563)
|
6
|
+
Invalid = %w(75120804350 95993823492 00000000000 123)
|
7
|
+
|
8
|
+
def test_number_and_to_s_methods
|
9
|
+
(Valid + Invalid).each do |number|
|
10
|
+
p = Pesel.new(number)
|
11
|
+
assert number, p.number
|
12
|
+
assert number, p.to_s
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_valid_method
|
17
|
+
Valid.each do |number|
|
18
|
+
p = Pesel.new(number)
|
19
|
+
assert p.valid?
|
20
|
+
end
|
21
|
+
|
22
|
+
Invalid.each do |number|
|
23
|
+
p = Pesel.new(number)
|
24
|
+
assert_equal false, p.valid?
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_birth_date_method
|
29
|
+
p = Pesel.new(Valid[0])
|
30
|
+
assert_equal Date.parse('1975-12-08'), p.birth_date
|
31
|
+
p = Pesel.new(Valid[1])
|
32
|
+
assert_equal Date.parse('1974-08-26'), p.birth_date
|
33
|
+
p = Pesel.new(Valid[2])
|
34
|
+
assert_equal Date.parse('2002-02-14'), p.birth_date
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_female_and_male_methods
|
38
|
+
p = Pesel.new(Valid[0])
|
39
|
+
assert_equal false, p.female?
|
40
|
+
assert p.male?
|
41
|
+
|
42
|
+
p = Pesel.new(Valid[1])
|
43
|
+
assert p.female?
|
44
|
+
assert_equal false, p.male?
|
45
|
+
|
46
|
+
p = Pesel.new(Valid[2])
|
47
|
+
assert p.female?
|
48
|
+
assert_equal false, p.male?
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_gender_method
|
52
|
+
p = Pesel.new(Valid[0])
|
53
|
+
assert_equal :male, p.gender
|
54
|
+
|
55
|
+
p = Pesel.new(Valid[1])
|
56
|
+
assert_equal :female, p.gender
|
57
|
+
|
58
|
+
p = Pesel.new(Valid[2])
|
59
|
+
assert_equal :female, p.gender
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_methods_that_raise_exception
|
63
|
+
Invalid.each do |number|
|
64
|
+
p = Pesel.new(number)
|
65
|
+
assert_raise Pesel::NumberInvalid do
|
66
|
+
p.birth_date
|
67
|
+
end
|
68
|
+
assert_raise Pesel::NumberInvalid do
|
69
|
+
p.female?
|
70
|
+
end
|
71
|
+
assert_raise Pesel::NumberInvalid do
|
72
|
+
p.male?
|
73
|
+
end
|
74
|
+
assert_raise Pesel::NumberInvalid do
|
75
|
+
p.gender
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pesel
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 9
|
8
|
+
- 0
|
9
|
+
version: 0.9.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Piotr Macuk
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-12-17 00:00:00 +01:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: Pesel class to check PESEL number and get information about its owner
|
22
|
+
email:
|
23
|
+
- piotr@macuk.pl
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files: []
|
29
|
+
|
30
|
+
files:
|
31
|
+
- .gitignore
|
32
|
+
- Gemfile
|
33
|
+
- README
|
34
|
+
- Rakefile
|
35
|
+
- lib/pesel.rb
|
36
|
+
- lib/version.rb
|
37
|
+
- pesel.gemspec
|
38
|
+
- test/pesel_test.rb
|
39
|
+
has_rdoc: true
|
40
|
+
homepage: http://github.com/macuk/pesel
|
41
|
+
licenses: []
|
42
|
+
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options: []
|
45
|
+
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
segments:
|
54
|
+
- 0
|
55
|
+
version: "0"
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
segments:
|
62
|
+
- 0
|
63
|
+
version: "0"
|
64
|
+
requirements: []
|
65
|
+
|
66
|
+
rubyforge_project: pesel
|
67
|
+
rubygems_version: 1.3.7
|
68
|
+
signing_key:
|
69
|
+
specification_version: 3
|
70
|
+
summary: PESEL validation and information
|
71
|
+
test_files: []
|
72
|
+
|