cpf_faker 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.rspec +1 -0
- data/Gemfile +16 -0
- data/Gemfile.lock +30 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +56 -0
- data/Rakefile +50 -0
- data/VERSION +1 -0
- data/lib/cpf_faker.rb +50 -0
- data/spec/cpf_faker_spec.rb +26 -0
- data/spec/spec_helper.rb +12 -0
- metadata +154 -0
data/.document
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
# Add dependencies to develop your gem here.
|
7
|
+
# Include everything needed to run rake, tests, features, etc.
|
8
|
+
|
9
|
+
group :development do
|
10
|
+
gem "rspec", "~> 2.1.0"
|
11
|
+
gem "bundler", "~> 1.0.0"
|
12
|
+
gem "jeweler", "~> 1.5.1"
|
13
|
+
gem "rcov", ">= 0"
|
14
|
+
gem "br-cpf", :require =>'br/cpf'
|
15
|
+
end
|
16
|
+
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
br-cpf (0.1.2)
|
5
|
+
diff-lcs (1.1.2)
|
6
|
+
git (1.2.5)
|
7
|
+
jeweler (1.5.2)
|
8
|
+
bundler (~> 1.0.0)
|
9
|
+
git (>= 1.2.5)
|
10
|
+
rake
|
11
|
+
rake (0.8.7)
|
12
|
+
rcov (0.9.9)
|
13
|
+
rspec (2.1.0)
|
14
|
+
rspec-core (~> 2.1.0)
|
15
|
+
rspec-expectations (~> 2.1.0)
|
16
|
+
rspec-mocks (~> 2.1.0)
|
17
|
+
rspec-core (2.1.0)
|
18
|
+
rspec-expectations (2.1.0)
|
19
|
+
diff-lcs (~> 1.1.2)
|
20
|
+
rspec-mocks (2.1.0)
|
21
|
+
|
22
|
+
PLATFORMS
|
23
|
+
ruby
|
24
|
+
|
25
|
+
DEPENDENCIES
|
26
|
+
br-cpf
|
27
|
+
bundler (~> 1.0.0)
|
28
|
+
jeweler (~> 1.5.1)
|
29
|
+
rcov
|
30
|
+
rspec (~> 2.1.0)
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Bernardo de Pádua
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
= CPF Faker
|
2
|
+
|
3
|
+
Generates Brazilian CPF numbers for use in testing. Great to be used alongside {Faker}[https://github.com/stympy/faker] and won't clutter your namespace.
|
4
|
+
|
5
|
+
Good and simple code with working specs.
|
6
|
+
|
7
|
+
== Install
|
8
|
+
|
9
|
+
Use rubygems:
|
10
|
+
|
11
|
+
gem install cpf_faker
|
12
|
+
|
13
|
+
Or add it to your Gemfile if your project uses bundler (ex.: Rails apps)
|
14
|
+
|
15
|
+
group :development do
|
16
|
+
# ...
|
17
|
+
gem 'cpf_faker'
|
18
|
+
end
|
19
|
+
|
20
|
+
== Usage
|
21
|
+
|
22
|
+
require 'cpf_faker'
|
23
|
+
|
24
|
+
Faker::CPF.numeric #=> "32586104447"
|
25
|
+
|
26
|
+
Faker::CPF.pretty #=> "419.356.777-05"
|
27
|
+
|
28
|
+
== Motivation
|
29
|
+
|
30
|
+
I got tired of using lame "Ad bait" sites to generate CPFs. And needed to generate several to populate a test database.
|
31
|
+
|
32
|
+
So I invested 30 minutes to make this into a simple gem others could use.
|
33
|
+
|
34
|
+
== Credits
|
35
|
+
|
36
|
+
Syntax based on the {Faker}[https://github.com/stympy/faker] gem.
|
37
|
+
|
38
|
+
Generation of the verification digits shamelessly stolen from the {validates_as_cpf}[https://github.com/sobrinho/validates_as_cpf] Rails Plugin (credit to Gabriel Sobrinho).
|
39
|
+
|
40
|
+
{br-cpf}[https://github.com/bbcoimbra/br-cpf] gem used for cross validation in the specs (credit to Bruno Coimbra).
|
41
|
+
|
42
|
+
== Contributing to cpf_faker
|
43
|
+
|
44
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
45
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
46
|
+
* Fork the project
|
47
|
+
* Start a feature/bugfix branch
|
48
|
+
* Commit and push until you are happy with your contribution
|
49
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
50
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
51
|
+
|
52
|
+
== Copyright
|
53
|
+
|
54
|
+
Copyright (c) 2010 Bernardo de Pádua. See LICENSE.txt for
|
55
|
+
further details.
|
56
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'rake'
|
11
|
+
|
12
|
+
require 'jeweler'
|
13
|
+
Jeweler::Tasks.new do |gem|
|
14
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
15
|
+
gem.name = "cpf_faker"
|
16
|
+
gem.homepage = "http://github.com/bernardo/cpf_faker"
|
17
|
+
gem.license = "MIT"
|
18
|
+
gem.summary = %Q{Generate fake brasilian CPFs for test purposes}
|
19
|
+
gem.description = %Q{Generates Brazilian CPF numbers for use in testing. Great to be used alongside Faker and won't clutter your namespace. }
|
20
|
+
gem.email = "berpasan@gmail.com"
|
21
|
+
gem.authors = ["Bernardo de Pádua"]
|
22
|
+
# Include your dependencies below. Runtime dependencies are required when using your gem,
|
23
|
+
# and development dependencies are only needed for development (ie running rake tasks, tests, etc)
|
24
|
+
# gem.add_runtime_dependency 'jabber4r', '> 0.1'
|
25
|
+
# gem.add_development_dependency 'rspec', '> 1.2.3'
|
26
|
+
end
|
27
|
+
Jeweler::RubygemsDotOrgTasks.new
|
28
|
+
|
29
|
+
require 'rspec/core'
|
30
|
+
require 'rspec/core/rake_task'
|
31
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
32
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
33
|
+
end
|
34
|
+
|
35
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
36
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
37
|
+
spec.rcov = true
|
38
|
+
end
|
39
|
+
|
40
|
+
task :default => :spec
|
41
|
+
|
42
|
+
require 'rake/rdoctask'
|
43
|
+
Rake::RDocTask.new do |rdoc|
|
44
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
45
|
+
|
46
|
+
rdoc.rdoc_dir = 'rdoc'
|
47
|
+
rdoc.title = "cpf_faker #{version}"
|
48
|
+
rdoc.rdoc_files.include('README*')
|
49
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
50
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.0
|
data/lib/cpf_faker.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
module Faker
|
2
|
+
class CPF
|
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
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
require 'br/cpf'
|
4
|
+
|
5
|
+
describe Faker::CPF do
|
6
|
+
|
7
|
+
it "generates valid number only cpfs" do
|
8
|
+
generated = Faker::CPF.numeric
|
9
|
+
generated.should =~ (/^\d{11}$/)
|
10
|
+
BR::CPF.valid?(generated).should == true
|
11
|
+
end
|
12
|
+
|
13
|
+
it "generates valid pretty printed cpfs" do
|
14
|
+
generated = Faker::CPF.pretty
|
15
|
+
generated.should =~ (/^\d{3}\.\d{3}\.\d{3}\-\d{2}$/)
|
16
|
+
clean = generated.gsub(/\.|\-/, '')
|
17
|
+
BR::CPF.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::CPF.number}.should_not raise_error
|
22
|
+
lambda{Faker::CPF.numbers}.should_not raise_error
|
23
|
+
lambda{Faker::CPF.cpf}.should_not raise_error
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
require 'rspec'
|
4
|
+
require 'cpf_faker'
|
5
|
+
|
6
|
+
# Requires supporting files with custom matchers and macros, etc,
|
7
|
+
# in ./support/ and its subdirectories.
|
8
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
|
12
|
+
end
|
metadata
ADDED
@@ -0,0 +1,154 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cpf_faker
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 23
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
- 0
|
10
|
+
version: 1.0.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- "Bernardo de P\xC3\xA1dua"
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-12-23 00:00:00 -02:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
23
|
+
none: false
|
24
|
+
requirements:
|
25
|
+
- - ~>
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
hash: 11
|
28
|
+
segments:
|
29
|
+
- 2
|
30
|
+
- 1
|
31
|
+
- 0
|
32
|
+
version: 2.1.0
|
33
|
+
type: :development
|
34
|
+
name: rspec
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
hash: 23
|
44
|
+
segments:
|
45
|
+
- 1
|
46
|
+
- 0
|
47
|
+
- 0
|
48
|
+
version: 1.0.0
|
49
|
+
type: :development
|
50
|
+
name: bundler
|
51
|
+
prerelease: false
|
52
|
+
version_requirements: *id002
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ~>
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
hash: 1
|
60
|
+
segments:
|
61
|
+
- 1
|
62
|
+
- 5
|
63
|
+
- 1
|
64
|
+
version: 1.5.1
|
65
|
+
type: :development
|
66
|
+
name: jeweler
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *id003
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
hash: 3
|
76
|
+
segments:
|
77
|
+
- 0
|
78
|
+
version: "0"
|
79
|
+
type: :development
|
80
|
+
name: rcov
|
81
|
+
prerelease: false
|
82
|
+
version_requirements: *id004
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
85
|
+
none: false
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
hash: 3
|
90
|
+
segments:
|
91
|
+
- 0
|
92
|
+
version: "0"
|
93
|
+
type: :development
|
94
|
+
name: br-cpf
|
95
|
+
prerelease: false
|
96
|
+
version_requirements: *id005
|
97
|
+
description: "Generates Brazilian CPF numbers for use in testing. Great to be used alongside Faker and won't clutter your namespace. "
|
98
|
+
email: berpasan@gmail.com
|
99
|
+
executables: []
|
100
|
+
|
101
|
+
extensions: []
|
102
|
+
|
103
|
+
extra_rdoc_files:
|
104
|
+
- LICENSE.txt
|
105
|
+
- README.rdoc
|
106
|
+
files:
|
107
|
+
- .document
|
108
|
+
- .rspec
|
109
|
+
- Gemfile
|
110
|
+
- Gemfile.lock
|
111
|
+
- LICENSE.txt
|
112
|
+
- README.rdoc
|
113
|
+
- Rakefile
|
114
|
+
- VERSION
|
115
|
+
- lib/cpf_faker.rb
|
116
|
+
- spec/cpf_faker_spec.rb
|
117
|
+
- spec/spec_helper.rb
|
118
|
+
has_rdoc: true
|
119
|
+
homepage: http://github.com/bernardo/cpf_faker
|
120
|
+
licenses:
|
121
|
+
- MIT
|
122
|
+
post_install_message:
|
123
|
+
rdoc_options: []
|
124
|
+
|
125
|
+
require_paths:
|
126
|
+
- lib
|
127
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
128
|
+
none: false
|
129
|
+
requirements:
|
130
|
+
- - ">="
|
131
|
+
- !ruby/object:Gem::Version
|
132
|
+
hash: 3
|
133
|
+
segments:
|
134
|
+
- 0
|
135
|
+
version: "0"
|
136
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
137
|
+
none: false
|
138
|
+
requirements:
|
139
|
+
- - ">="
|
140
|
+
- !ruby/object:Gem::Version
|
141
|
+
hash: 3
|
142
|
+
segments:
|
143
|
+
- 0
|
144
|
+
version: "0"
|
145
|
+
requirements: []
|
146
|
+
|
147
|
+
rubyforge_project:
|
148
|
+
rubygems_version: 1.3.7
|
149
|
+
signing_key:
|
150
|
+
specification_version: 3
|
151
|
+
summary: Generate fake brasilian CPFs for test purposes
|
152
|
+
test_files:
|
153
|
+
- spec/cpf_faker_spec.rb
|
154
|
+
- spec/spec_helper.rb
|