brazilian_id 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.rspec +1 -0
- data/Gemfile +6 -0
- data/LICENSE +22 -0
- data/README.md +28 -0
- data/Rakefile +5 -0
- data/brazilian_id.gemspec +17 -0
- data/lib/brazilian_id/brazilian_id.rb +45 -0
- data/lib/brazilian_id/ids/cnpj.rb +18 -0
- data/lib/brazilian_id/ids/cpf.rb +31 -0
- data/lib/brazilian_id/ids/titulo_eleitor.rb +33 -0
- data/lib/brazilian_id/mod11.rb +31 -0
- data/lib/brazilian_id/version.rb +3 -0
- data/lib/brazilian_id.rb +11 -0
- data/spec/cnpj_spec.rb +5 -0
- data/spec/cpf_spec.rb +11 -0
- data/spec/mod11_spec.rb +6 -0
- data/spec/titulo_spec.rb +6 -0
- metadata +87 -0
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Halan Pinheiro
|
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,28 @@
|
|
1
|
+
# BrazilianId
|
2
|
+
|
3
|
+
|
4
|
+
## Installation
|
5
|
+
|
6
|
+
Add this line to your application's Gemfile:
|
7
|
+
|
8
|
+
gem 'brazilian_id'
|
9
|
+
|
10
|
+
And then execute:
|
11
|
+
|
12
|
+
$ bundle
|
13
|
+
|
14
|
+
Or install it yourself as:
|
15
|
+
|
16
|
+
$ gem install brazilian_id
|
17
|
+
|
18
|
+
## Usage
|
19
|
+
|
20
|
+
TODO: Write usage instructions here
|
21
|
+
|
22
|
+
## Contributing
|
23
|
+
|
24
|
+
1. Fork it
|
25
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
26
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
27
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
28
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/brazilian_id/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Halan Pinheiro"]
|
6
|
+
gem.email = ["halan.pinheiro@gmail.com"]
|
7
|
+
gem.description = %q{Brazilian identifications like CPF, CNPJ, Título de Eleitor}
|
8
|
+
gem.summary = %q{Brazilian identifications like CPF, CNPJ, Título de Eleitor}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "brazilian_id"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = BrazilianId::VERSION
|
17
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
class BrazilianID
|
2
|
+
def number_length; 0; end;
|
3
|
+
def check_digits_length; 2; end;
|
4
|
+
|
5
|
+
def initialize number
|
6
|
+
@number = clear_number(number)[0..number_length]
|
7
|
+
end
|
8
|
+
|
9
|
+
def check_digits
|
10
|
+
@number[/..$/]
|
11
|
+
end
|
12
|
+
|
13
|
+
def sequential
|
14
|
+
@number[/^(.*)..$/, 1]
|
15
|
+
end
|
16
|
+
|
17
|
+
def valid?
|
18
|
+
@number == valid_number and not invalid_sequential?
|
19
|
+
end
|
20
|
+
|
21
|
+
def invalid_sequential?
|
22
|
+
sequential.chars.to_a.uniq.count.eql? 1
|
23
|
+
end
|
24
|
+
|
25
|
+
def to_s
|
26
|
+
"#{sequential.scan(/\d{3}/).join('.')}-#{check_digits}"
|
27
|
+
end
|
28
|
+
|
29
|
+
protected
|
30
|
+
def mod11 number
|
31
|
+
Mod11.new(number).check_digit
|
32
|
+
end
|
33
|
+
|
34
|
+
def valid_number
|
35
|
+
(1..check_digits_length).reduce(sequential) do |number, digit|
|
36
|
+
number += mod11(number).to_s
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def clear_number number
|
41
|
+
number.to_s.scan(/\d/).join
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
|
@@ -0,0 +1,18 @@
|
|
1
|
+
class CNPJ < BrazilianID
|
2
|
+
def number_length; 14; end;
|
3
|
+
|
4
|
+
def to_s
|
5
|
+
"#{sequential.scan(/\d{3}/).join('.')}-#{check_digits}"
|
6
|
+
end
|
7
|
+
|
8
|
+
def inspect
|
9
|
+
"#<CNPJ: #{to_s} #{valid? ? 'valid': 'not valid'} >"
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
def mod11 number
|
14
|
+
Mod11.new(number, :base => 2..9).check_digit
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
|
@@ -0,0 +1,31 @@
|
|
1
|
+
class CPF < BrazilianID
|
2
|
+
def number_length; 11; end;
|
3
|
+
|
4
|
+
def to_s
|
5
|
+
"#{sequential.scan(/\d{3}/).join('.')}-#{check_digits}"
|
6
|
+
end
|
7
|
+
|
8
|
+
def inspect
|
9
|
+
"#<CPF: #{to_s} #{valid? ? 'valid': 'not valid'} >"
|
10
|
+
end
|
11
|
+
|
12
|
+
def origin
|
13
|
+
[
|
14
|
+
[:rs],
|
15
|
+
[:df, :go, :ms, :mt, :to],
|
16
|
+
[:ac, :am, :ap, :pa, :ro, :rr],
|
17
|
+
[:ce, :ma, :pi],
|
18
|
+
[:al, :pb, :pe, :rn],
|
19
|
+
[:ba, :se],
|
20
|
+
[:mg],
|
21
|
+
[:es, :rj],
|
22
|
+
[:sp],
|
23
|
+
[:pr, :sc]
|
24
|
+
][sequential[-1,1].to_i]
|
25
|
+
end
|
26
|
+
|
27
|
+
def from? uf
|
28
|
+
origin.include? uf
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
@@ -0,0 +1,33 @@
|
|
1
|
+
class TituloEleitor < BrazilianID
|
2
|
+
def number_length; 10; end;
|
3
|
+
|
4
|
+
def initialize number
|
5
|
+
@number = clear_number(number)
|
6
|
+
end
|
7
|
+
|
8
|
+
def sequential
|
9
|
+
@number[/^(.*)....$/, 1]
|
10
|
+
end
|
11
|
+
|
12
|
+
def uf_digit
|
13
|
+
@number[/(..)..$/, 1]
|
14
|
+
end
|
15
|
+
|
16
|
+
def origin
|
17
|
+
[ nil, :sp, :mg, :rj, :rs, :ba, :pr, :ce,
|
18
|
+
:pe, :sc, :go, :ma, :pb, :pa, :es,
|
19
|
+
:pi, :rn, :al, :mt, :ms, :df, :se,
|
20
|
+
:am, :ro, :ac, :ap, :rr, :to, :exterior
|
21
|
+
][uf_digit.to_i]
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
def valid_number
|
26
|
+
first_check_digit = mod11(sequential).to_s
|
27
|
+
second_check_digit = mod11(uf_digit+first_check_digit).to_s
|
28
|
+
|
29
|
+
sequential + uf_digit + first_check_digit + second_check_digit
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
|
@@ -0,0 +1,31 @@
|
|
1
|
+
class Mod11
|
2
|
+
def initialize number, options = {}
|
3
|
+
options = {:base => 2..11, :mod => 11}.merge options
|
4
|
+
|
5
|
+
@number = number.to_s.scan(/\d/).join
|
6
|
+
@base = options[:base]
|
7
|
+
@mod = options[:mod]
|
8
|
+
end
|
9
|
+
|
10
|
+
def check_digit
|
11
|
+
if block_given?
|
12
|
+
yield sum, @mod
|
13
|
+
else
|
14
|
+
((10 * sum) % @mod) % 10
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
def bases
|
20
|
+
multipliers = ([*@base] * (@number.length/@base.count+1))[0..(@number.length-1)].reverse
|
21
|
+
end
|
22
|
+
|
23
|
+
def numbers_list
|
24
|
+
@number.chars.map &:to_i
|
25
|
+
end
|
26
|
+
|
27
|
+
def sum
|
28
|
+
bases.zip(numbers_list).reduce(0) {|total, n| total += n[0] * n[1]}
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
data/lib/brazilian_id.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
['version.rb', 'mod11.rb', 'brazilian_id.rb'].each do |filename|
|
2
|
+
require File.join(File.dirname(__FILE__), 'brazilian_id', filename)
|
3
|
+
end
|
4
|
+
|
5
|
+
['cpf.rb', 'cnpj.rb', 'titulo_eleitor.rb'].each do |filename|
|
6
|
+
require File.join(File.dirname(__FILE__), 'brazilian_id', 'ids', filename)
|
7
|
+
end
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
|
data/spec/cnpj_spec.rb
ADDED
data/spec/cpf_spec.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', 'lib', 'brazilian_id.rb')
|
2
|
+
|
3
|
+
describe CPF do
|
4
|
+
it { CPF.new('009.147.274-19').should be_valid }
|
5
|
+
it { CPF.new('009.147.074-19').should_not be_valid }
|
6
|
+
|
7
|
+
|
8
|
+
it { CPF.new('009.147.274-10').should_not be_valid }
|
9
|
+
it { CPF.new('000.000.000-00').should_not be_valid }
|
10
|
+
it { CPF.new('111.111.111-11').should_not be_valid }
|
11
|
+
end
|
data/spec/mod11_spec.rb
ADDED
data/spec/titulo_spec.rb
ADDED
metadata
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: brazilian_id
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Halan Pinheiro
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-07-17 00:00:00 Z
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: "Brazilian identifications like CPF, CNPJ, T\xC3\xADtulo de Eleitor"
|
22
|
+
email:
|
23
|
+
- halan.pinheiro@gmail.com
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files: []
|
29
|
+
|
30
|
+
files:
|
31
|
+
- .gitignore
|
32
|
+
- .rspec
|
33
|
+
- Gemfile
|
34
|
+
- LICENSE
|
35
|
+
- README.md
|
36
|
+
- Rakefile
|
37
|
+
- brazilian_id.gemspec
|
38
|
+
- lib/brazilian_id.rb
|
39
|
+
- lib/brazilian_id/brazilian_id.rb
|
40
|
+
- lib/brazilian_id/ids/cnpj.rb
|
41
|
+
- lib/brazilian_id/ids/cpf.rb
|
42
|
+
- lib/brazilian_id/ids/titulo_eleitor.rb
|
43
|
+
- lib/brazilian_id/mod11.rb
|
44
|
+
- lib/brazilian_id/version.rb
|
45
|
+
- spec/cnpj_spec.rb
|
46
|
+
- spec/cpf_spec.rb
|
47
|
+
- spec/mod11_spec.rb
|
48
|
+
- spec/titulo_spec.rb
|
49
|
+
homepage: ""
|
50
|
+
licenses: []
|
51
|
+
|
52
|
+
post_install_message:
|
53
|
+
rdoc_options: []
|
54
|
+
|
55
|
+
require_paths:
|
56
|
+
- lib
|
57
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
hash: 3
|
63
|
+
segments:
|
64
|
+
- 0
|
65
|
+
version: "0"
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
hash: 3
|
72
|
+
segments:
|
73
|
+
- 0
|
74
|
+
version: "0"
|
75
|
+
requirements: []
|
76
|
+
|
77
|
+
rubyforge_project:
|
78
|
+
rubygems_version: 1.8.24
|
79
|
+
signing_key:
|
80
|
+
specification_version: 3
|
81
|
+
summary: "Brazilian identifications like CPF, CNPJ, T\xC3\xADtulo de Eleitor"
|
82
|
+
test_files:
|
83
|
+
- spec/cnpj_spec.rb
|
84
|
+
- spec/cpf_spec.rb
|
85
|
+
- spec/mod11_spec.rb
|
86
|
+
- spec/titulo_spec.rb
|
87
|
+
has_rdoc:
|