/cpf_faker

Generates Brazilian CPF numbers for use in testing.

Primary LanguageRubyMIT LicenseMIT

CPF Faker

Pare de usar estes sites “garimpadores de grana do adwords” quando precisar de um CPF ou CPNJ!

Generates Brazilian CPF and CNPJ numbers for use in testing. Great to be used alongside Faker and won’t clutter your namespace.

As a bonus, you get command line executables for quick generation or use in Bash scripts.

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.

Install

Use rubygems:

gem install cpf_faker

Or add it to your Gemfile if your project uses bundler (ex.: Rails apps)

group :development do
  # ...
  gem 'cpf_faker'
end

Usage

Ruby code

require 'cpf_faker'

# Generates CPFs
Faker::CPF.numeric    #=> "32586104447"
Faker::CPF.pretty     #=> "419.356.777-05"

# Generates CNPJ
Faker::CNPJ.numeric    #=> "31669138588095"
Faker::CNPJ.pretty     #=> "69.068.788/8492-00"

#some aliases so you won't guess wrong
Faker::CPF.cpf     #=> "859.684.732-40"

Command line

The command line executables are ridiculously simple and do nothing besides printing and coping number-only cpfs and cnpjs.

$ cpf
85968473240
CPF Copied to your clipboard!

$ cnpj
69068788849200
CNPJ Copied to your clipboard!

Motivation

I got tired of using lame “Adword bait” sites to generate CPFs. And needed to generate several to populate a test database.

So I invested 30 minutes to make this into a simple gem others could use.

Credits

Syntax based on the Faker gem.

Generation of the verification digits shamelessly stolen from the validates_as_cpf Rails Plugin (credit to Gabriel Sobrinho).

br-cpf gem used for cross validation in the specs (credit to Bruno Coimbra).

Contributing to cpf_faker

  • Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet

  • Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it

  • Fork the project

  • Start a feature/bugfix branch

  • Commit and push until you are happy with your contribution

  • Make sure to add tests for it. This is important so I don’t break it in a future version unintentionally.

  • 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.

Copyright © 2010 Bernardo de Pádua. See LICENSE.txt for further details.