cpf_faker 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +1 -0
- data/Gemfile.lock +2 -0
- data/README.rdoc +27 -4
- data/Rakefile +2 -2
- data/VERSION +1 -1
- data/bin/cnpj +6 -0
- data/bin/cpf +6 -0
- data/cpf_faker.gemspec +78 -0
- data/lib/cpf_faker.rb +2 -50
- data/lib/faker/cnpj.rb +47 -0
- data/lib/faker/cpf.rb +49 -0
- data/spec/cnpj_faker_spec.rb +27 -0
- data/spec/cpf_faker_spec.rb +3 -2
- metadata +30 -8
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
data/README.rdoc
CHANGED
@@ -1,8 +1,12 @@
|
|
1
1
|
= CPF Faker
|
2
2
|
|
3
|
-
|
3
|
+
Pare de usar estes sites "garimpadores de grana do adwords" quando precisar de um CPF ou CPNJ!
|
4
4
|
|
5
|
-
|
5
|
+
Generates Brazilian CPF and CNPJ numbers for use in testing. Great to be used alongside {Faker}[https://github.com/stympy/faker] and won't clutter your namespace.
|
6
|
+
|
7
|
+
As a bonus, you get command line executables for quick generation or use in Bash scripts.
|
8
|
+
|
9
|
+
Good and simple code with working specs. It is pure ruby since the algorithms are lame and simple and nobody in their right mind should ever care about performance for this.
|
6
10
|
|
7
11
|
== Install
|
8
12
|
|
@@ -19,15 +23,34 @@ Or add it to your Gemfile if your project uses bundler (ex.: Rails apps)
|
|
19
23
|
|
20
24
|
== Usage
|
21
25
|
|
26
|
+
=== Ruby code
|
27
|
+
|
22
28
|
require 'cpf_faker'
|
23
29
|
|
30
|
+
# Generates CPFs
|
24
31
|
Faker::CPF.numeric #=> "32586104447"
|
25
|
-
|
26
32
|
Faker::CPF.pretty #=> "419.356.777-05"
|
27
33
|
|
34
|
+
# Generates CNPJ
|
35
|
+
Faker::CNPJ.numeric #=> "31669138588095"
|
36
|
+
Faker::CNPJ.pretty #=> "69.068.788/8492-00"
|
37
|
+
|
38
|
+
#some aliases so you won't guess wrong
|
39
|
+
Faker::CPF.cpf #=> "859.684.732-40"
|
40
|
+
|
41
|
+
=== Command line
|
42
|
+
|
43
|
+
The command line executables are ridiculously simple and do nothing besides output number-only cpfs and cnpjs.
|
44
|
+
|
45
|
+
$ cpf
|
46
|
+
85968473240
|
47
|
+
|
48
|
+
$ cnpj
|
49
|
+
69068788849200
|
50
|
+
|
28
51
|
== Motivation
|
29
52
|
|
30
|
-
I got tired of using lame "
|
53
|
+
I got tired of using lame "Adword bait" sites to generate CPFs. And needed to generate several to populate a test database.
|
31
54
|
|
32
55
|
So I invested 30 minutes to make this into a simple gem others could use.
|
33
56
|
|
data/Rakefile
CHANGED
@@ -15,8 +15,8 @@ Jeweler::Tasks.new do |gem|
|
|
15
15
|
gem.name = "cpf_faker"
|
16
16
|
gem.homepage = "http://github.com/bernardo/cpf_faker"
|
17
17
|
gem.license = "MIT"
|
18
|
-
gem.summary = %Q{Generate fake brasilian
|
19
|
-
gem.description = %Q{Generates
|
18
|
+
gem.summary = %Q{Generate fake brasilian CPF and CNPJ numbers for test purposes}
|
19
|
+
gem.description = %Q{Generates brazilian CPF and CNPJ numbers for use in testing. Great to be used alongside Faker and won't clutter your namespace. }
|
20
20
|
gem.email = "berpasan@gmail.com"
|
21
21
|
gem.authors = ["Bernardo de Pádua"]
|
22
22
|
# Include your dependencies below. Runtime dependencies are required when using your gem,
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.
|
1
|
+
1.1.0
|
data/bin/cnpj
ADDED
data/bin/cpf
ADDED
data/cpf_faker.gemspec
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{cpf_faker}
|
8
|
+
s.version = "1.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Bernardo de P\303\241dua"]
|
12
|
+
s.date = %q{2011-01-07}
|
13
|
+
s.description = %q{Generates brazilian CPF and CNPJ numbers for use in testing. Great to be used alongside Faker and won't clutter your namespace. }
|
14
|
+
s.email = %q{berpasan@gmail.com}
|
15
|
+
s.executables = ["cnpj", "cpf"]
|
16
|
+
s.extra_rdoc_files = [
|
17
|
+
"LICENSE.txt",
|
18
|
+
"README.rdoc"
|
19
|
+
]
|
20
|
+
s.files = [
|
21
|
+
".document",
|
22
|
+
".rspec",
|
23
|
+
"Gemfile",
|
24
|
+
"Gemfile.lock",
|
25
|
+
"LICENSE.txt",
|
26
|
+
"README.rdoc",
|
27
|
+
"Rakefile",
|
28
|
+
"VERSION",
|
29
|
+
"bin/cnpj",
|
30
|
+
"bin/cpf",
|
31
|
+
"cpf_faker.gemspec",
|
32
|
+
"lib/cpf_faker.rb",
|
33
|
+
"lib/faker/cnpj.rb",
|
34
|
+
"lib/faker/cpf.rb",
|
35
|
+
"spec/cnpj_faker_spec.rb",
|
36
|
+
"spec/cpf_faker_spec.rb",
|
37
|
+
"spec/spec_helper.rb"
|
38
|
+
]
|
39
|
+
s.homepage = %q{http://github.com/bernardo/cpf_faker}
|
40
|
+
s.licenses = ["MIT"]
|
41
|
+
s.require_paths = ["lib"]
|
42
|
+
s.rubygems_version = %q{1.3.7}
|
43
|
+
s.summary = %q{Generate fake brasilian CPF and CNPJ numbers for test purposes}
|
44
|
+
s.test_files = [
|
45
|
+
"spec/cnpj_faker_spec.rb",
|
46
|
+
"spec/cpf_faker_spec.rb",
|
47
|
+
"spec/spec_helper.rb"
|
48
|
+
]
|
49
|
+
|
50
|
+
if s.respond_to? :specification_version then
|
51
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
52
|
+
s.specification_version = 3
|
53
|
+
|
54
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
55
|
+
s.add_development_dependency(%q<rspec>, ["~> 2.1.0"])
|
56
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
57
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.5.1"])
|
58
|
+
s.add_development_dependency(%q<rcov>, [">= 0"])
|
59
|
+
s.add_development_dependency(%q<br-cpf>, [">= 0"])
|
60
|
+
s.add_development_dependency(%q<br-cnpj>, [">= 0"])
|
61
|
+
else
|
62
|
+
s.add_dependency(%q<rspec>, ["~> 2.1.0"])
|
63
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
64
|
+
s.add_dependency(%q<jeweler>, ["~> 1.5.1"])
|
65
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
66
|
+
s.add_dependency(%q<br-cpf>, [">= 0"])
|
67
|
+
s.add_dependency(%q<br-cnpj>, [">= 0"])
|
68
|
+
end
|
69
|
+
else
|
70
|
+
s.add_dependency(%q<rspec>, ["~> 2.1.0"])
|
71
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
72
|
+
s.add_dependency(%q<jeweler>, ["~> 1.5.1"])
|
73
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
74
|
+
s.add_dependency(%q<br-cpf>, [">= 0"])
|
75
|
+
s.add_dependency(%q<br-cnpj>, [">= 0"])
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
data/lib/cpf_faker.rb
CHANGED
@@ -1,50 +1,2 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
class << self
|
5
|
-
# Generates CPF a valid cpf number as a String with numbers only.
|
6
|
-
#
|
7
|
-
# Example:
|
8
|
-
# Faker::CPF.number # => "11438374798"
|
9
|
-
def numeric
|
10
|
-
cpf_root = Array.new(9) { rand(10) }
|
11
|
-
|
12
|
-
# calculate first digit
|
13
|
-
sum = (0..8).inject(0) do |sum, i|
|
14
|
-
sum + cpf_root[i] * (10 - i)
|
15
|
-
end
|
16
|
-
|
17
|
-
first_validator = sum % 11
|
18
|
-
first_validator = first_validator < 2 ? 0 : 11 - first_validator
|
19
|
-
|
20
|
-
# calculate second digit
|
21
|
-
sum = (0..8).inject(0) do |sum, i|
|
22
|
-
sum + cpf_root[i] * (11 - i)
|
23
|
-
end
|
24
|
-
|
25
|
-
sum += first_validator * 2
|
26
|
-
|
27
|
-
second_validator = sum % 11
|
28
|
-
second_validator = second_validator < 2 ? 0 : 11 - second_validator
|
29
|
-
|
30
|
-
(cpf_root + [first_validator, second_validator]).to_s
|
31
|
-
end
|
32
|
-
alias number numeric
|
33
|
-
alias numbers numeric
|
34
|
-
|
35
|
-
|
36
|
-
# Generates CPF a valid cpf number as a String with numbers and standard separator digits.
|
37
|
-
#
|
38
|
-
# Example:
|
39
|
-
# Faker::CPF.number # => "059.893.186-42"
|
40
|
-
def pretty
|
41
|
-
numbers = numeric
|
42
|
-
reg_match = numbers.match(/^(\d{3})(\d{3})(\d{3})(\d{2})$/)
|
43
|
-
"#{reg_match[1]}.#{reg_match[2]}.#{reg_match[3]}-#{reg_match[4]}"
|
44
|
-
end
|
45
|
-
alias cpf pretty
|
46
|
-
|
47
|
-
end
|
48
|
-
|
49
|
-
end
|
50
|
-
end
|
1
|
+
require 'faker/cpf'
|
2
|
+
require 'faker/cnpj'
|
data/lib/faker/cnpj.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
module Faker
|
2
|
+
class CNPJ
|
3
|
+
class << self
|
4
|
+
# Generates a valid CNPJ number with numbers only. Returns a string, since it may have leading zeros.
|
5
|
+
#
|
6
|
+
# Example:
|
7
|
+
# Faker::CNPJ.numeric #=> "57222068000132"
|
8
|
+
def numeric
|
9
|
+
cnpj_root = Array.new(12) { rand(10) }
|
10
|
+
|
11
|
+
# calculate first digit
|
12
|
+
factor = [5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2]
|
13
|
+
|
14
|
+
sum = (0..11).inject(0) do |sum, i|
|
15
|
+
sum + cnpj_root[i] * factor[i]
|
16
|
+
end
|
17
|
+
|
18
|
+
first_validator = sum % 11
|
19
|
+
cnpj_root << first_validator = first_validator < 2 ? 0 : 11 - first_validator
|
20
|
+
|
21
|
+
# calculate second digit
|
22
|
+
factor.unshift 6
|
23
|
+
|
24
|
+
sum = (0..12).inject(0) do |sum, i|
|
25
|
+
sum + cnpj_root[i] * factor[i]
|
26
|
+
end
|
27
|
+
|
28
|
+
second_validator = sum % 11
|
29
|
+
(cnpj_root << second_validator = second_validator < 2 ? 0 : 11 - second_validator).to_s
|
30
|
+
end
|
31
|
+
alias numbers numeric
|
32
|
+
alias number numeric
|
33
|
+
|
34
|
+
# Generates a valid CNPJ number, including separators. Returns a string.
|
35
|
+
#
|
36
|
+
# Example:
|
37
|
+
# Faker::CNPJ.numeric #=> "22.792.949/0001-04"
|
38
|
+
def pretty
|
39
|
+
cnpj = numeric.to_s.rjust(14, "0")
|
40
|
+
"%s.%s.%s/%s-%s" % [cnpj[0,2], cnpj[2,3], cnpj[5,3], cnpj[8,4], cnpj[12,2]]
|
41
|
+
end
|
42
|
+
alias cnpj pretty
|
43
|
+
alias formatted pretty
|
44
|
+
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
data/lib/faker/cpf.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
module Faker
|
2
|
+
class CPF
|
3
|
+
|
4
|
+
class << self
|
5
|
+
# Generates a valid CPF number with numbers only. Returns a String (since it might contain leading zeros).
|
6
|
+
#
|
7
|
+
# Example:
|
8
|
+
# Faker::CPF.number # => "11438374798"
|
9
|
+
def numeric
|
10
|
+
cpf_root = Array.new(9) { rand(10) }
|
11
|
+
|
12
|
+
# calculate first digit
|
13
|
+
sum = (0..8).inject(0) do |sum, i|
|
14
|
+
sum + cpf_root[i] * (10 - i)
|
15
|
+
end
|
16
|
+
|
17
|
+
first_validator = sum % 11
|
18
|
+
first_validator = first_validator < 2 ? 0 : 11 - first_validator
|
19
|
+
cpf_root << first_validator
|
20
|
+
|
21
|
+
# calculate second digit
|
22
|
+
sum = (0..8).inject(0) do |sum, i|
|
23
|
+
sum + cpf_root[i] * (11 - i)
|
24
|
+
end
|
25
|
+
|
26
|
+
sum += first_validator * 2
|
27
|
+
|
28
|
+
second_validator = sum % 11
|
29
|
+
second_validator = second_validator < 2 ? 0 : 11 - second_validator
|
30
|
+
(cpf_root << second_validator).to_s
|
31
|
+
end
|
32
|
+
alias number numeric
|
33
|
+
alias numbers numeric
|
34
|
+
|
35
|
+
# Generates a valid CPF number with numbers and separators. Returns a String.
|
36
|
+
#
|
37
|
+
# Example:
|
38
|
+
# Faker::CPF.number # => "059.893.186-42"
|
39
|
+
def pretty
|
40
|
+
cpf_numbers = numeric
|
41
|
+
"%s.%s.%s-%s" % [cpf_numbers[0..2], cpf_numbers[3..5], cpf_numbers[6..8], cpf_numbers[9..10]]
|
42
|
+
end
|
43
|
+
alias cpf pretty
|
44
|
+
alias formatted pretty
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
require 'br/cnpj'
|
4
|
+
|
5
|
+
describe Faker::CNPJ do
|
6
|
+
|
7
|
+
it "generates valid number only cnpjs" do
|
8
|
+
generated = Faker::CNPJ.numeric
|
9
|
+
generated.should =~ (/^\d{14}$/)
|
10
|
+
BR::CNPJ.valid?(generated).should == true
|
11
|
+
end
|
12
|
+
|
13
|
+
it "generates valid pretty printed cpfs" do
|
14
|
+
generated = Faker::CNPJ.pretty
|
15
|
+
generated.should =~ (/^\d{2}\.\d{3}\.\d{3}\/\d{4}\-\d{2}$/)
|
16
|
+
clean = generated.gsub(/\.|\-|\//, '')
|
17
|
+
BR::CNPJ.valid?(clean).should == true
|
18
|
+
end
|
19
|
+
|
20
|
+
it "has a bunch of aliases in case you forget the method names" do
|
21
|
+
lambda{Faker::CNPJ.number}.should_not raise_error
|
22
|
+
lambda{Faker::CNPJ.numbers}.should_not raise_error
|
23
|
+
lambda{Faker::CNPJ.cnpj}.should_not raise_error
|
24
|
+
lambda{Faker::CNPJ.formatted}.should_not raise_error
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
data/spec/cpf_faker_spec.rb
CHANGED
@@ -6,7 +6,7 @@ describe Faker::CPF do
|
|
6
6
|
|
7
7
|
it "generates valid number only cpfs" do
|
8
8
|
generated = Faker::CPF.numeric
|
9
|
-
generated.should =~
|
9
|
+
generated.should =~ /^\d{11}$/
|
10
10
|
BR::CPF.valid?(generated).should == true
|
11
11
|
end
|
12
12
|
|
@@ -20,7 +20,8 @@ describe Faker::CPF do
|
|
20
20
|
it "has a bunch of aliases in case you forget the method names" do
|
21
21
|
lambda{Faker::CPF.number}.should_not raise_error
|
22
22
|
lambda{Faker::CPF.numbers}.should_not raise_error
|
23
|
-
lambda{Faker::CPF.cpf}.should_not raise_error
|
23
|
+
lambda{Faker::CPF.cpf}.should_not raise_error
|
24
|
+
lambda{Faker::CPF.formatted}.should_not raise_error
|
24
25
|
end
|
25
26
|
|
26
27
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cpf_faker
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 19
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 1
|
8
|
+
- 1
|
8
9
|
- 0
|
9
|
-
|
10
|
-
version: 1.0.0
|
10
|
+
version: 1.1.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- "Bernardo de P\xC3\xA1dua"
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date:
|
18
|
+
date: 2011-01-07 00:00:00 -02:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -94,10 +94,25 @@ dependencies:
|
|
94
94
|
name: br-cpf
|
95
95
|
prerelease: false
|
96
96
|
version_requirements: *id005
|
97
|
-
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
99
|
+
none: false
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
hash: 3
|
104
|
+
segments:
|
105
|
+
- 0
|
106
|
+
version: "0"
|
107
|
+
type: :development
|
108
|
+
name: br-cnpj
|
109
|
+
prerelease: false
|
110
|
+
version_requirements: *id006
|
111
|
+
description: "Generates brazilian CPF and CNPJ numbers for use in testing. Great to be used alongside Faker and won't clutter your namespace. "
|
98
112
|
email: berpasan@gmail.com
|
99
|
-
executables:
|
100
|
-
|
113
|
+
executables:
|
114
|
+
- cnpj
|
115
|
+
- cpf
|
101
116
|
extensions: []
|
102
117
|
|
103
118
|
extra_rdoc_files:
|
@@ -112,7 +127,13 @@ files:
|
|
112
127
|
- README.rdoc
|
113
128
|
- Rakefile
|
114
129
|
- VERSION
|
130
|
+
- bin/cnpj
|
131
|
+
- bin/cpf
|
132
|
+
- cpf_faker.gemspec
|
115
133
|
- lib/cpf_faker.rb
|
134
|
+
- lib/faker/cnpj.rb
|
135
|
+
- lib/faker/cpf.rb
|
136
|
+
- spec/cnpj_faker_spec.rb
|
116
137
|
- spec/cpf_faker_spec.rb
|
117
138
|
- spec/spec_helper.rb
|
118
139
|
has_rdoc: true
|
@@ -148,7 +169,8 @@ rubyforge_project:
|
|
148
169
|
rubygems_version: 1.3.7
|
149
170
|
signing_key:
|
150
171
|
specification_version: 3
|
151
|
-
summary: Generate fake brasilian
|
172
|
+
summary: Generate fake brasilian CPF and CNPJ numbers for test purposes
|
152
173
|
test_files:
|
174
|
+
- spec/cnpj_faker_spec.rb
|
153
175
|
- spec/cpf_faker_spec.rb
|
154
176
|
- spec/spec_helper.rb
|