rpg 1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +29 -0
- data/Rakefile +11 -0
- data/lib/rpg.rb +4 -0
- data/lib/rpg/pesel.rb +55 -0
- data/lib/rpg/version.rb +3 -0
- data/rpg.gemspec +19 -0
- data/test/pesel_test.rb +90 -0
- data/test/test_helper.rb +7 -0
- metadata +74 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Michał Krzyżanowski
|
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,29 @@
|
|
1
|
+
# Rpg
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'rpg'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install rpg
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/rpg.rb
ADDED
data/lib/rpg/pesel.rb
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
module Rpg
|
2
|
+
class Pesel
|
3
|
+
def initialize(options = {})
|
4
|
+
@date = options[:date].is_a?(Date) ? options[:date] : Date.parse(options[:date])
|
5
|
+
@gender = options[:gender] || :male
|
6
|
+
end
|
7
|
+
|
8
|
+
def generate
|
9
|
+
@pesel = [date, serie, gender_num].join
|
10
|
+
@pesel += calculate_control_sum.to_s
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
def serie
|
15
|
+
"%03d" % rand(0..999)
|
16
|
+
end
|
17
|
+
|
18
|
+
def calculate_control_sum
|
19
|
+
mask = [1, 3, 7, 9, 1, 3, 7, 9, 1, 3]
|
20
|
+
val = @pesel.split("").map(&:to_i)
|
21
|
+
modulo = (mask.inject(0){|sum, num| sum + (num * val.shift)} % 10)
|
22
|
+
|
23
|
+
modulo == 0 ? 0 : 10 - modulo
|
24
|
+
end
|
25
|
+
|
26
|
+
def gender_num
|
27
|
+
if @gender == :male
|
28
|
+
(0..9).to_a.select(&:odd?).sample
|
29
|
+
else
|
30
|
+
(0..9).to_a.select(&:even?).sample
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def date
|
35
|
+
[@date.strftime("%y"), month_with_century_offset, @date.strftime("%d")]
|
36
|
+
end
|
37
|
+
|
38
|
+
def month_with_century_offset
|
39
|
+
offset = if (1800..1899).include?(@date.year)
|
40
|
+
80
|
41
|
+
elsif (1900..1999).include?(@date.year)
|
42
|
+
0
|
43
|
+
elsif (2000..2099).include?(@date.year)
|
44
|
+
20
|
45
|
+
elsif (2100..2199).include?(@date.year)
|
46
|
+
40
|
47
|
+
elsif (2200..2299).include?(@date.year)
|
48
|
+
60
|
49
|
+
else
|
50
|
+
raise ArgumentError.new("Year should be between 1800 and 2299")
|
51
|
+
end
|
52
|
+
"%02d" % (@date.month + offset)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
data/lib/rpg/version.rb
ADDED
data/rpg.gemspec
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/rpg/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Michal Krzyzanowski"]
|
6
|
+
gem.email = ["michal.krzyzanowski+github@gmail.com"]
|
7
|
+
gem.description = %q{Real Pesel Generator}
|
8
|
+
gem.summary = %q{Gem for generating valid PESEL numbers}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.add_development_dependency "yapv"
|
12
|
+
|
13
|
+
gem.files = `git ls-files`.split($\)
|
14
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
15
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
16
|
+
gem.name = "rpg"
|
17
|
+
gem.require_paths = ["lib"]
|
18
|
+
gem.version = Rpg::VERSION
|
19
|
+
end
|
data/test/pesel_test.rb
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
require "date"
|
3
|
+
class PeselTest < MiniTest::Unit::TestCase
|
4
|
+
def test_should_generate_valid_pesel_for_19th_century
|
5
|
+
date = Date.parse("1803-10-20")
|
6
|
+
pesel = Rpg::Pesel.new(date: date).generate
|
7
|
+
@pesel = Yapv::Pesel.new(pesel)
|
8
|
+
|
9
|
+
assert @pesel.valid?
|
10
|
+
assert_equal date, @pesel.birth_date
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_should_generate_valid_pesel_for_20th_century
|
14
|
+
date = Date.parse("1920-04-11")
|
15
|
+
pesel = Rpg::Pesel.new(date: date).generate
|
16
|
+
@pesel = Yapv::Pesel.new(pesel)
|
17
|
+
|
18
|
+
assert @pesel.valid?
|
19
|
+
assert_equal date, @pesel.birth_date
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_should_generate_valid_pesel_for_21st_century
|
23
|
+
date = Date.parse("2012-06-02")
|
24
|
+
pesel = Rpg::Pesel.new(date: date).generate
|
25
|
+
@pesel = Yapv::Pesel.new(pesel)
|
26
|
+
|
27
|
+
assert @pesel.valid?
|
28
|
+
assert_equal date, @pesel.birth_date
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_should_generate_valid_pesel_for_22nd_century
|
32
|
+
date = Date.parse("2165-11-06")
|
33
|
+
pesel = Rpg::Pesel.new(date: date).generate
|
34
|
+
@pesel = Yapv::Pesel.new(pesel)
|
35
|
+
|
36
|
+
assert @pesel.valid?
|
37
|
+
assert_equal date, @pesel.birth_date
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_should_generate_valid_pesel_for_23rd_century
|
41
|
+
date = Date.parse("2289-11-12")
|
42
|
+
pesel = Rpg::Pesel.new(date: date).generate
|
43
|
+
@pesel = Yapv::Pesel.new(pesel)
|
44
|
+
|
45
|
+
assert @pesel.valid?
|
46
|
+
assert_equal date, @pesel.birth_date
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_should_raise_argument_error_for_date_below_18th_century
|
50
|
+
assert_raises ArgumentError do
|
51
|
+
Rpg::Pesel.new(date: "1799-04-11").generate
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_should_raise_argument_error_for_date_over_23rd_century
|
56
|
+
assert_raises ArgumentError do
|
57
|
+
Rpg::Pesel.new(date: "2300-04-11").generate
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_should_generate_pesel_for_female
|
62
|
+
@pesel = Rpg::Pesel.new(gender: :female, date: "2020-04-11").generate
|
63
|
+
assert Yapv::Pesel.new(@pesel).valid?
|
64
|
+
assert_equal :female, Yapv::Pesel.new(@pesel).gender
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_should_generate_pesel_for_male
|
68
|
+
@pesel = Rpg::Pesel.new(gender: :male, date: "2020-04-11").generate
|
69
|
+
assert Yapv::Pesel.new(@pesel).valid?
|
70
|
+
assert_equal :male, Yapv::Pesel.new(@pesel).gender
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_should_accept_date_as_string
|
74
|
+
@pesel = Rpg::Pesel.new(date: "2020-04-11").generate
|
75
|
+
assert Yapv::Pesel.new(@pesel).valid?
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_should_accept_date_as_date_object
|
79
|
+
@pesel = Rpg::Pesel.new(date:Date.today).generate
|
80
|
+
assert Yapv::Pesel.new(@pesel).valid?
|
81
|
+
end
|
82
|
+
|
83
|
+
def test_generated_pesel_should_be_unique
|
84
|
+
numbers = [].tap do |content|
|
85
|
+
10.times{ content << Rpg::Pesel.new(date: Date.today).generate }
|
86
|
+
end
|
87
|
+
|
88
|
+
assert numbers.uniq.size != 1
|
89
|
+
end
|
90
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rpg
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '1.0'
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Michal Krzyzanowski
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-02 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: yapv
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
description: Real Pesel Generator
|
31
|
+
email:
|
32
|
+
- michal.krzyzanowski+github@gmail.com
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- .gitignore
|
38
|
+
- Gemfile
|
39
|
+
- LICENSE
|
40
|
+
- README.md
|
41
|
+
- Rakefile
|
42
|
+
- lib/rpg.rb
|
43
|
+
- lib/rpg/pesel.rb
|
44
|
+
- lib/rpg/version.rb
|
45
|
+
- rpg.gemspec
|
46
|
+
- test/pesel_test.rb
|
47
|
+
- test/test_helper.rb
|
48
|
+
homepage: ''
|
49
|
+
licenses: []
|
50
|
+
post_install_message:
|
51
|
+
rdoc_options: []
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ! '>='
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0'
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
requirements: []
|
67
|
+
rubyforge_project:
|
68
|
+
rubygems_version: 1.8.24
|
69
|
+
signing_key:
|
70
|
+
specification_version: 3
|
71
|
+
summary: Gem for generating valid PESEL numbers
|
72
|
+
test_files:
|
73
|
+
- test/pesel_test.rb
|
74
|
+
- test/test_helper.rb
|