common_numbers 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/Gemfile +5 -0
- data/README.markdown +67 -0
- data/Rakefile +2 -0
- data/common_numbers.gemspec +21 -0
- data/lib/common_numbers.rb +7 -0
- data/lib/common_numbers/base.rb +40 -0
- data/lib/common_numbers/nip.rb +34 -0
- data/lib/common_numbers/pesel.rb +25 -0
- data/lib/common_numbers/regon.rb +25 -0
- data/lib/common_numbers/regon14.rb +15 -0
- data/lib/common_numbers/regon9.rb +15 -0
- data/lib/common_numbers/version.rb +3 -0
- data/spec/nip_spec.rb +21 -0
- data/spec/pesel_spec.rb +21 -0
- data/spec/regon_spec.rb +21 -0
- data/spec/spec_helper.rb +3 -0
- metadata +76 -0
data/Gemfile
ADDED
data/README.markdown
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
###MAGICK NUMBERS
|
2
|
+
|
3
|
+
Magic Numbers are numbers verfied with Luhn Algoritm like ISBN, VIN
|
4
|
+
|
5
|
+
magick_numbers gem simple verify this numbers.
|
6
|
+
In first version gem is focused on Polish numbers like:
|
7
|
+
|
8
|
+
- PESEL - (Polish ID Number)
|
9
|
+
- NIP - (Polish Taxation Identification Number)
|
10
|
+
- REGON - (Polish Company Identification Number)
|
11
|
+
|
12
|
+
but in near future will validate also:
|
13
|
+
|
14
|
+
- ISBN
|
15
|
+
- EAN
|
16
|
+
- VIN
|
17
|
+
|
18
|
+
meybe something else :)
|
19
|
+
|
20
|
+
###Instalation:
|
21
|
+
|
22
|
+
gem install common_numbers
|
23
|
+
|
24
|
+
###Usage:
|
25
|
+
|
26
|
+
All numbers are in module MagickNumbers and have simple method `valid?`
|
27
|
+
|
28
|
+
require 'common_numbers'
|
29
|
+
|
30
|
+
CommonNumbers::Polish::Nip.new(nip_number).valid?
|
31
|
+
CommonNumbers::Polish::Pesel.new(pesel_number).valid?
|
32
|
+
CommonNumbers::Polish::Regon.new(regon_number).valid?
|
33
|
+
|
34
|
+
###Valid numbers:
|
35
|
+
|
36
|
+
####NIP
|
37
|
+
|
38
|
+
NIP is valid when has 11 digits witn optional '-' between
|
39
|
+
|
40
|
+
1234563218
|
41
|
+
123-456-32-18
|
42
|
+
123-45-63-218
|
43
|
+
|
44
|
+
are valid numbers
|
45
|
+
|
46
|
+
In international notation NIP has country code as first two signs, and:
|
47
|
+
|
48
|
+
PL1234563218
|
49
|
+
PL123-456-32-18
|
50
|
+
PL123-45-63-218
|
51
|
+
|
52
|
+
are also valid.
|
53
|
+
|
54
|
+
####PESEL
|
55
|
+
|
56
|
+
Pesel's valid format is 11 digits, no other signs
|
57
|
+
|
58
|
+
####REGON
|
59
|
+
|
60
|
+
Regon has two options, first 9 digits for small companies and 14 digits
|
61
|
+
with companies with regional offices.
|
62
|
+
|
63
|
+
Both are validated.
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
Copyright (c) 2011 Mariusz Nosiński, released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "common_numbers/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "common_numbers"
|
7
|
+
s.version = CommonNumbers::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Mariusz Nosinski"]
|
10
|
+
s.email = ["marioosh@5dots.pl"]
|
11
|
+
s.homepage = "http://github.com/marioosh/common_numbers"
|
12
|
+
s.summary = %q{Basic library to validate numbers like PESEL, NIP, etc}
|
13
|
+
s.description = %q{Common Numbers validates popular numbers like polish PESEL, NIP, REGON, or global ISBN, EAN, etc.}
|
14
|
+
|
15
|
+
s.rubyforge_project = "common_numbers"
|
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
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module CommonNumbers
|
2
|
+
|
3
|
+
class Base
|
4
|
+
|
5
|
+
attr_reader :magick_number, :regexp, :mask, :modulo, :length, :magick_array
|
6
|
+
|
7
|
+
def initialize(num)
|
8
|
+
@magick_number = num.to_s.gsub('-', '')
|
9
|
+
end
|
10
|
+
|
11
|
+
def valid?
|
12
|
+
validate
|
13
|
+
end
|
14
|
+
|
15
|
+
def validate
|
16
|
+
validate_regexp && validate_length && validate_sum_control
|
17
|
+
end
|
18
|
+
|
19
|
+
def validate_regexp
|
20
|
+
regexp =~ magick_number
|
21
|
+
end
|
22
|
+
|
23
|
+
def validate_length
|
24
|
+
magick_number.size == length
|
25
|
+
end
|
26
|
+
|
27
|
+
def validate_sum_control
|
28
|
+
mod = checksum % modulo
|
29
|
+
mod = 0 if mod == 10
|
30
|
+
mod === magick_array.shift
|
31
|
+
end
|
32
|
+
|
33
|
+
def checksum
|
34
|
+
@magick_array = magick_number.split( "").collect &:to_i
|
35
|
+
checksum = mask.inject(0) {|sum, weight| sum + weight * magick_array.shift}
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module CommonNumbers
|
2
|
+
|
3
|
+
module Polish
|
4
|
+
|
5
|
+
class Nip < CommonNumbers::Base
|
6
|
+
|
7
|
+
def initialize(num)
|
8
|
+
super(num)
|
9
|
+
@mask = [ 6, 5, 7, 2, 3, 4, 5, 6, 7]
|
10
|
+
@modulo = 11
|
11
|
+
@regexp = /^(PL)?[0-9]*/
|
12
|
+
# @magick_number = num.to_s.gsub('-', '')
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
def validate_length
|
17
|
+
@magick_number.size == (international? ? 12 : 10)
|
18
|
+
end
|
19
|
+
|
20
|
+
def international?
|
21
|
+
/^PL/ =~ @magick_number
|
22
|
+
end
|
23
|
+
|
24
|
+
def validate_sum_control
|
25
|
+
magick_number.slice!(0..1) if international?
|
26
|
+
mod = checksum % modulo
|
27
|
+
mod === magick_array.shift
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module CommonNumbers
|
2
|
+
|
3
|
+
module Polish
|
4
|
+
class Pesel < CommonNumbers::Base
|
5
|
+
|
6
|
+
def initialize(num)
|
7
|
+
super num
|
8
|
+
@mask = [ 1, 3, 7, 9, 1, 3, 7, 9, 1, 3]
|
9
|
+
@modulo = 10
|
10
|
+
@regexp = /\d{11}/
|
11
|
+
@length = 11
|
12
|
+
end
|
13
|
+
|
14
|
+
def validate_sum_control
|
15
|
+
mod = checksum % modulo
|
16
|
+
mod = 10 - mod
|
17
|
+
mod = 0 if mod == 10
|
18
|
+
mod === magick_array.shift
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module CommonNumbers
|
2
|
+
|
3
|
+
module Polish
|
4
|
+
|
5
|
+
class Regon
|
6
|
+
|
7
|
+
def initialize(num)
|
8
|
+
@factory = if(/^\d{9}$/ =~ num)
|
9
|
+
Regon9.new num
|
10
|
+
elsif(/^\d{14}$/ =~ num)
|
11
|
+
Regon14.new num
|
12
|
+
else
|
13
|
+
nil
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
def valid?
|
19
|
+
return @factory.valid? if @factory
|
20
|
+
false
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/spec/nip_spec.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'rspec'
|
3
|
+
|
4
|
+
describe "Nip" do
|
5
|
+
|
6
|
+
it "should be valid"do
|
7
|
+
valid_numbers = %w[123-456-32-18 1234563218 123-45-63-218 PL123-456-32-18]
|
8
|
+
valid_numbers.each do |n|
|
9
|
+
nip = CommonNumbers::Polish::Nip.new(n)
|
10
|
+
nip.should be_valid
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should be invalid" do
|
15
|
+
invalid_numbers = %w[PL5912158688 PP5911158688 591-116-86-88 591-11-48-688 PL_591-115-86-88 PL591 591-11-58-6887]
|
16
|
+
invalid_numbers.each do |n|
|
17
|
+
nip = CommonNumbers::Polish::Nip.new(n)
|
18
|
+
nip.should_not be_valid
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/spec/pesel_spec.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'rspec'
|
3
|
+
|
4
|
+
describe "Pesel" do
|
5
|
+
|
6
|
+
it "should be valid"do
|
7
|
+
valid_numbers = %w[44051401359 02070803628]
|
8
|
+
valid_numbers.each do |n|
|
9
|
+
pesel = CommonNumbers::Polish::Pesel.new(n)
|
10
|
+
pesel.should be_valid
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should be invalid" do
|
15
|
+
invalid_numbers = %w[44051401358 02070803629]
|
16
|
+
invalid_numbers.each do |n|
|
17
|
+
pesel = CommonNumbers::Polish::Pesel.new(n)
|
18
|
+
pesel.should_not be_valid
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/spec/regon_spec.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'rspec'
|
3
|
+
|
4
|
+
describe "Regon" do
|
5
|
+
|
6
|
+
it "should be valid"do
|
7
|
+
valid_numbers = %w[192598184 123456785 12345678512347]
|
8
|
+
valid_numbers.each do |n|
|
9
|
+
regon = CommonNumbers::Polish::Regon.new(n)
|
10
|
+
regon.should be_valid
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should be invalid" do
|
15
|
+
invalid_numbers = %w[192598284 1925982843 19259828 123456786 12345638512347]
|
16
|
+
invalid_numbers.each do |n|
|
17
|
+
regon = CommonNumbers::Polish::Regon.new(n)
|
18
|
+
regon.should_not be_valid
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: common_numbers
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.1.5
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Mariusz Nosinski
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-04-18 00:00:00 +02:00
|
14
|
+
default_executable:
|
15
|
+
dependencies: []
|
16
|
+
|
17
|
+
description: Common Numbers validates popular numbers like polish PESEL, NIP, REGON, or global ISBN, EAN, etc.
|
18
|
+
email:
|
19
|
+
- marioosh@5dots.pl
|
20
|
+
executables: []
|
21
|
+
|
22
|
+
extensions: []
|
23
|
+
|
24
|
+
extra_rdoc_files: []
|
25
|
+
|
26
|
+
files:
|
27
|
+
- .gitignore
|
28
|
+
- Gemfile
|
29
|
+
- README.markdown
|
30
|
+
- Rakefile
|
31
|
+
- common_numbers.gemspec
|
32
|
+
- lib/common_numbers.rb
|
33
|
+
- lib/common_numbers/base.rb
|
34
|
+
- lib/common_numbers/nip.rb
|
35
|
+
- lib/common_numbers/pesel.rb
|
36
|
+
- lib/common_numbers/regon.rb
|
37
|
+
- lib/common_numbers/regon14.rb
|
38
|
+
- lib/common_numbers/regon9.rb
|
39
|
+
- lib/common_numbers/version.rb
|
40
|
+
- spec/nip_spec.rb
|
41
|
+
- spec/pesel_spec.rb
|
42
|
+
- spec/regon_spec.rb
|
43
|
+
- spec/spec_helper.rb
|
44
|
+
has_rdoc: true
|
45
|
+
homepage: http://github.com/marioosh/common_numbers
|
46
|
+
licenses: []
|
47
|
+
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options: []
|
50
|
+
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: "0"
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
none: false
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: "0"
|
65
|
+
requirements: []
|
66
|
+
|
67
|
+
rubyforge_project: common_numbers
|
68
|
+
rubygems_version: 1.6.2
|
69
|
+
signing_key:
|
70
|
+
specification_version: 3
|
71
|
+
summary: Basic library to validate numbers like PESEL, NIP, etc
|
72
|
+
test_files:
|
73
|
+
- spec/nip_spec.rb
|
74
|
+
- spec/pesel_spec.rb
|
75
|
+
- spec/regon_spec.rb
|
76
|
+
- spec/spec_helper.rb
|