brid 0.1.0
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/brid.gemspec +17 -0
- data/lib/brid/generic_id.rb +55 -0
- data/lib/brid/ids/cnpj.rb +22 -0
- data/lib/brid/ids/cpf.rb +37 -0
- data/lib/brid/ids/pis.rb +33 -0
- data/lib/brid/ids/titulo_eleitor.rb +41 -0
- data/lib/brid/mod11.rb +55 -0
- data/lib/brid/version.rb +3 -0
- data/lib/brid.rb +11 -0
- data/spec/cnpj_spec.rb +7 -0
- data/spec/cpf_spec.rb +13 -0
- data/spec/generic_id_spec.rb +5 -0
- data/spec/mod11_spec.rb +8 -0
- data/spec/pis_spec.rb +8 -0
- data/spec/titulo_spec.rb +6 -0
- metadata +92 -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
|
+
# Brid
|
2
|
+
|
3
|
+
|
4
|
+
## Installation
|
5
|
+
|
6
|
+
Add this line to your application's Gemfile:
|
7
|
+
|
8
|
+
gem 'brid'
|
9
|
+
|
10
|
+
And then execute:
|
11
|
+
|
12
|
+
$ bundle
|
13
|
+
|
14
|
+
Or install it yourself as:
|
15
|
+
|
16
|
+
$ gem install brid
|
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
data/brid.gemspec
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/brid/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 = "brid"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Brid::VERSION
|
17
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
class GenericID
|
2
|
+
def number_length; 0; end;
|
3
|
+
def check_digits_length; 1; end;
|
4
|
+
|
5
|
+
def initialize number
|
6
|
+
@number = clear_number(number)
|
7
|
+
|
8
|
+
if number_length > 0
|
9
|
+
raise ArgumentError.new "invalid number" if @number.length != number_length
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def check_digits
|
14
|
+
@check_digits ||= begin
|
15
|
+
@number[-check_digits_length, check_digits_length]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def sequential
|
20
|
+
@sequential ||= begin
|
21
|
+
@number[0, @number.length - check_digits_length]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def valid?
|
26
|
+
@valid ||= begin
|
27
|
+
valid_number == @number and not invalid_sequential?
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def invalid_sequential?
|
32
|
+
@invalid_sequential ||=begin
|
33
|
+
sequential.chars.to_a.uniq.count.eql? 1
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def to_s
|
38
|
+
"#{sequential}-#{check_digits}"
|
39
|
+
end
|
40
|
+
|
41
|
+
protected
|
42
|
+
def mod11 number
|
43
|
+
Mod11.new(number)
|
44
|
+
end
|
45
|
+
|
46
|
+
def valid_number
|
47
|
+
mod11(sequential) * check_digits_length
|
48
|
+
end
|
49
|
+
|
50
|
+
def clear_number number
|
51
|
+
number.to_s.scan(/\d/).join
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Brid
|
2
|
+
class CNPJ < GenericID
|
3
|
+
def number_length; 14; end
|
4
|
+
def check_digits_length; 2; end;
|
5
|
+
|
6
|
+
def to_s
|
7
|
+
er = /^(.{2})(.{3})(.{3})(.{4})(.{2})/
|
8
|
+
"#{@number.gsub(er, '\1.\2.\3/\4-\5')}"
|
9
|
+
end
|
10
|
+
|
11
|
+
def inspect
|
12
|
+
"#<CNPJ: #{to_s} #{valid? ? 'valid': 'not valid'} >"
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
def mod11 number
|
17
|
+
Mod11.new(number, :base => 2..9)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
CNPJ = Brid::CNPJ if not defined? CNPJ and not defined? Cnpj
|
data/lib/brid/ids/cpf.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
module Brid
|
2
|
+
class CPF < GenericID
|
3
|
+
def number_length; 11; end
|
4
|
+
def check_digits_length; 2; end;
|
5
|
+
|
6
|
+
def to_s
|
7
|
+
"#{sequential.scan(/\d{3}/).join('.')}-#{check_digits}"
|
8
|
+
end
|
9
|
+
|
10
|
+
def inspect
|
11
|
+
"#<CPF: #{to_s} #{valid? ? 'valid': 'not valid'} >"
|
12
|
+
end
|
13
|
+
|
14
|
+
def origin
|
15
|
+
@origin ||= begin
|
16
|
+
[
|
17
|
+
[:rs],
|
18
|
+
[:df, :go, :ms, :mt, :to],
|
19
|
+
[:ac, :am, :ap, :pa, :ro, :rr],
|
20
|
+
[:ce, :ma, :pi],
|
21
|
+
[:al, :pb, :pe, :rn],
|
22
|
+
[:ba, :se],
|
23
|
+
[:mg],
|
24
|
+
[:es, :rj],
|
25
|
+
[:sp],
|
26
|
+
[:pr, :sc]
|
27
|
+
][sequential[-1,1].to_i]
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def from? uf
|
32
|
+
origin.include? uf
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
CPF = Brid::CPF if not defined? CPF and not defined? Cpf
|
data/lib/brid/ids/pis.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
module Brid
|
2
|
+
class PIS < GenericID
|
3
|
+
def number_length; 11; end
|
4
|
+
|
5
|
+
def check_digits
|
6
|
+
@check_digits ||= begin
|
7
|
+
@number[/.$/]
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def sequential
|
12
|
+
@sequential ||= begin
|
13
|
+
@number[/^(.*).$/, 1]
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def to_s
|
18
|
+
er = /^(.{3})(.{5})(.{2})(.{1})/
|
19
|
+
"#{@number.gsub(er, '\1.\2.\3-\4')}"
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
def mod11 number
|
24
|
+
Mod11.new(number, :base => 2..9)
|
25
|
+
end
|
26
|
+
|
27
|
+
def valid_number
|
28
|
+
mod11(sequential) * 1
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
PIS = Brid::PIS if not defined? PIS and not defined? Pis
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module Brid
|
2
|
+
class TituloEleitor < GenericID
|
3
|
+
def number_length; 10; end;
|
4
|
+
|
5
|
+
def initialize number
|
6
|
+
@number = clear_number(number)
|
7
|
+
end
|
8
|
+
|
9
|
+
def sequential
|
10
|
+
@sequential ||= begin
|
11
|
+
@number[/^(.*)....$/, 1]
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def uf_digit
|
16
|
+
@uf_digit ||= begin
|
17
|
+
@number[/(..)..$/, 1]
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def origin
|
22
|
+
@origin ||= begin
|
23
|
+
[ nil, :sp, :mg, :rj, :rs, :ba, :pr, :ce,
|
24
|
+
:pe, :sc, :go, :ma, :pb, :pa, :es,
|
25
|
+
:pi, :rn, :al, :mt, :ms, :df, :se,
|
26
|
+
:am, :ro, :ac, :ap, :rr, :to, :exterior
|
27
|
+
][uf_digit.to_i]
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
def valid_number
|
33
|
+
first_check_digit = mod11(sequential).cdigi.to_s
|
34
|
+
second_check_digit = mod11(uf_digit+first_check_digit).cdigi.to_s
|
35
|
+
|
36
|
+
sequential + uf_digit + first_check_digit + second_check_digit
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
TituloEleitoral = TituloEleitor = Brid::TituloEleitor unless defined? TituloEleitor and not defined? TituloEleitoral
|
data/lib/brid/mod11.rb
ADDED
@@ -0,0 +1,55 @@
|
|
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
|
+
alias :cdigi :check_digit
|
19
|
+
|
20
|
+
def * digits
|
21
|
+
digits.times.reduce(self) do |number, digit|
|
22
|
+
number += Mod11.new(number, :base => @base, :mode => @mod).cdigi
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
alias :>> :*
|
27
|
+
|
28
|
+
def + digit
|
29
|
+
Mod11.new(@number += digit.to_s.scan(/\d/).join, :base => @base, :mode => @mod)
|
30
|
+
end
|
31
|
+
|
32
|
+
alias :<< :+
|
33
|
+
|
34
|
+
def to_s
|
35
|
+
@number.to_s
|
36
|
+
end
|
37
|
+
|
38
|
+
def == other
|
39
|
+
self.to_s == other.to_s
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
def bases
|
44
|
+
multipliers = ([*@base] * (@number.length/@base.count+1))[0..(@number.length-1)].reverse
|
45
|
+
end
|
46
|
+
|
47
|
+
def numbers_list
|
48
|
+
@number.chars.map &:to_i
|
49
|
+
end
|
50
|
+
|
51
|
+
def sum
|
52
|
+
bases.zip(numbers_list).reduce(0) {|total, n| total += n[0] * n[1]}
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
data/lib/brid/version.rb
ADDED
data/lib/brid.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
['version.rb', 'mod11.rb', 'generic_id.rb'].each do |filename|
|
2
|
+
require File.join(File.dirname(__FILE__), 'brid', filename)
|
3
|
+
end
|
4
|
+
|
5
|
+
['cpf.rb', 'cnpj.rb', 'titulo_eleitor.rb', 'pis.rb'].each do |filename|
|
6
|
+
require File.join(File.dirname(__FILE__), 'brid', 'ids', filename)
|
7
|
+
end
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
|
data/spec/cnpj_spec.rb
ADDED
data/spec/cpf_spec.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', 'lib', 'brid.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
|
+
|
12
|
+
it { lambda { CPF.new('foobar') }.should raise_error(ArgumentError)}
|
13
|
+
end
|
data/spec/mod11_spec.rb
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), '..', 'lib', 'brid.rb')
|
2
|
+
|
3
|
+
describe Mod11 do
|
4
|
+
it { Mod11.new('009147274').check_digit.should == 1 }
|
5
|
+
it { Mod11.new('0091472741').check_digit.should == 9 }
|
6
|
+
|
7
|
+
it { (Mod11.new('009147274') * 2).to_s.should == '00914727419' }
|
8
|
+
end
|
data/spec/pis_spec.rb
ADDED
data/spec/titulo_spec.rb
ADDED
metadata
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: brid
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Halan Pinheiro
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-07-18 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
|
+
- brid.gemspec
|
38
|
+
- lib/brid.rb
|
39
|
+
- lib/brid/generic_id.rb
|
40
|
+
- lib/brid/ids/cnpj.rb
|
41
|
+
- lib/brid/ids/cpf.rb
|
42
|
+
- lib/brid/ids/pis.rb
|
43
|
+
- lib/brid/ids/titulo_eleitor.rb
|
44
|
+
- lib/brid/mod11.rb
|
45
|
+
- lib/brid/version.rb
|
46
|
+
- spec/cnpj_spec.rb
|
47
|
+
- spec/cpf_spec.rb
|
48
|
+
- spec/generic_id_spec.rb
|
49
|
+
- spec/mod11_spec.rb
|
50
|
+
- spec/pis_spec.rb
|
51
|
+
- spec/titulo_spec.rb
|
52
|
+
homepage: ""
|
53
|
+
licenses: []
|
54
|
+
|
55
|
+
post_install_message:
|
56
|
+
rdoc_options: []
|
57
|
+
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
hash: 3
|
66
|
+
segments:
|
67
|
+
- 0
|
68
|
+
version: "0"
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
none: false
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
hash: 3
|
75
|
+
segments:
|
76
|
+
- 0
|
77
|
+
version: "0"
|
78
|
+
requirements: []
|
79
|
+
|
80
|
+
rubyforge_project:
|
81
|
+
rubygems_version: 1.8.24
|
82
|
+
signing_key:
|
83
|
+
specification_version: 3
|
84
|
+
summary: "Brazilian identifications like CPF, CNPJ, T\xC3\xADtulo de Eleitor"
|
85
|
+
test_files:
|
86
|
+
- spec/cnpj_spec.rb
|
87
|
+
- spec/cpf_spec.rb
|
88
|
+
- spec/generic_id_spec.rb
|
89
|
+
- spec/mod11_spec.rb
|
90
|
+
- spec/pis_spec.rb
|
91
|
+
- spec/titulo_spec.rb
|
92
|
+
has_rdoc:
|