cpf_utils 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c0d2d6965f5f52f0e68ff55f132eb61320ff2ba0
4
+ data.tar.gz: 86de60cc7567d557c4d2b046d6263d38cf5ae683
5
+ SHA512:
6
+ metadata.gz: e29b9ae14c7b1bd6de47566e40b05f381be364cc2711f17c59967a7b8d3bd1c8f8fbf4a9b05536609d4304ac20b01c5eef072857b6c3bdb17b89f2290aff08fb
7
+ data.tar.gz: 05f06aaea9eb8c9b80c090407523b1a73b6d996c969687d8127d87ced37d5adb95670f008f646de65db77446a9b770bb3104a6057cf1e4d72377064e30c1626b
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format nested
2
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in cpf_utils.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Jackson Pires
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,58 @@
1
+ # CpfUtils
2
+
3
+ CpfUtils é uma suite de funcionalidades para CPF.
4
+ O CpfUtils é capaz de gerar CPF para testes no formado tradicional ou apenas numérico, testa se determinado número de CPF é válido, gera dígitos verificadores para determinado número candidato a CPF, dentre outras coisas.
5
+
6
+ ## Instalação
7
+
8
+ Adicione essa linha na Gemfile da sua aplicação:
9
+
10
+ gem 'cpf_utils'
11
+
12
+ E então execute:
13
+
14
+ $ bundle
15
+
16
+ Ou instale você mesmo, conforme abaixo:
17
+
18
+ $ gem install cpf_utils
19
+
20
+ ## Uso
21
+
22
+ O CpfUtils é muito fácil de usar, por exempo:
23
+
24
+ # Para gerar um número de CPF:
25
+ CpfUtils.cpf => # "45698394823"
26
+
27
+ # Para gerar um CPF formatado:
28
+ CpfUtils.cpf_formatted => # "456.983.948-23"
29
+
30
+ # Para verificar se um CPF é válido:
31
+ CpfUtils.valid_cpf?("47238051923") => # true
32
+ CpfUtils.valid_cpf?(47238051923) => # true
33
+ CpfUtils.valid_cpf?("472.380.519-23") => # true
34
+
35
+ # Outra forma de verificar se um CPF é válido:
36
+ "45698394823".valid_cpf? => # true
37
+ "456.983.948-23".valid_cpf? => # true
38
+
39
+ # Para verificar se uma máscara de CPF é válida:
40
+ "456.983.948-23".valid_cpf_mask? => # true
41
+ "456.983..948-23".valid_cpf_mask? => # false
42
+
43
+ # Para formatar um número válido de CPF:
44
+ "45698394823".to_cpf_format => # "456.983.948-23"
45
+
46
+ # Para gerar um número de CPF a partir de um número candidato:
47
+ "456983948".generate_cpf => # "45698394823"
48
+
49
+ # Para gerar um número de CPF formatado a partir de um número candidato:
50
+ "456983948".generate_cpf_formatted => # "456.983.948-23"
51
+
52
+ ## Contribuindo
53
+
54
+ 1. Faça um Fork
55
+ 2. Crie um branch para a nova funcionalidade (`git checkout -b my-new-feature`)
56
+ 3. Faça o commit de suas alterações (`git commit -am 'Add some feature'`)
57
+ 4. Faça um push da sua nova funconalidade (`git push origin my-new-feature`)
58
+ 5. Submeta uma nova Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/cpf_utils.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'cpf_utils/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "cpf_utils"
8
+ spec.version = CpfUtils::VERSION
9
+ spec.authors = ["Jackson Pires"]
10
+ spec.email = ["jackson.pires@gmail.com"]
11
+ spec.description = %q{Uma suite de funcionalidades para o CPF.}
12
+ spec.summary = %q{Gera CPF para testes no formado tradicional ou apenas
13
+ numérico, testa se determinado número de CPF
14
+ é válido, além muitas outras funcionalidades descritas
15
+ na documentação.}
16
+ spec.homepage = "https://github.com/jacksonpires/cpf_utils"
17
+ spec.license = "MIT"
18
+
19
+ spec.files = `git ls-files`.split($/)
20
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
21
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.3"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "rspec", "~> 2.14.1"
27
+ end
@@ -0,0 +1,59 @@
1
+ class String
2
+
3
+ # Verifica se uma máscara de CPF é válida:
4
+ #
5
+ # "456.983.948-23".valid_cpf_mask? => # true
6
+ # "456.983..948-23".valid_cpf_mask? => # false
7
+ def valid_cpf_mask?
8
+ without_mask = !!(self =~ /^[+-]?[0-9]{11}+$/)
9
+ with_mask = !!(self =~ /^[+-]?[0-9]{3}\.[0-9]{3}\.[0-9]{3}-[0-9]{2}+$/)
10
+ with_mask || without_mask
11
+ end
12
+
13
+ # Verifica se um CPF é válido:
14
+ #
15
+ # CpfUtils.valid_cpf?("47238051923") => # true
16
+ # CpfUtils.valid_cpf?(47238051923) => # tru
17
+ # CpfUtils.valid_cpf?("472.380.519-23") => # true
18
+ def valid_cpf?
19
+ if valid_cpf_mask?
20
+ original_cpf = self.gsub(/\.?-?/,"",)
21
+ tested_cpf = original_cpf[0..8]
22
+
23
+ tested_cpf << CpfUtils::Cpf.new(original_cpf[0..8]).first_digit.to_s
24
+ tested_cpf << CpfUtils::Cpf.new(tested_cpf[0..9]).second_digit.to_s
25
+
26
+ tested_cpf == original_cpf ? true : false
27
+ end
28
+ end
29
+
30
+ # Para formatar um número válido de CPF:
31
+ #
32
+ # "45698394823".to_cpf_format => # "456.983.948-23"
33
+ def to_cpf_format
34
+ if self.valid_cpf?
35
+ "#{self[0..2]}.#{self[3..5]}.#{self[6..8]}-#{self[9..11]}"
36
+ end
37
+ end
38
+
39
+ # Para gerar um número de CPF a partir de um número candidato:
40
+ #
41
+ # "456983948".generate_cpf => # "45698394823"
42
+ def generate_cpf
43
+ if !!(self =~ /^[+-]?[0-9]{9}+$/)
44
+ final_cpf = self
45
+
46
+ final_cpf << CpfUtils::Cpf.new(final_cpf[0..8]).first_digit.to_s
47
+ final_cpf << CpfUtils::Cpf.new(final_cpf[0..9]).second_digit.to_s
48
+
49
+ final_cpf
50
+ end
51
+ end
52
+
53
+ # Para gerar um número de CPF formatado a partir de um número candidato:
54
+ #
55
+ # "456983948".generate_cpf_formatted => # "456.983.948-23"
56
+ def generate_cpf_formatted
57
+ generate_cpf.to_cpf_format
58
+ end
59
+ end
@@ -0,0 +1,3 @@
1
+ module CpfUtils
2
+ VERSION = "1.0.0"
3
+ end
data/lib/cpf_utils.rb ADDED
@@ -0,0 +1,99 @@
1
+ require "cpf_utils/version"
2
+ require "cpf_utils/string"
3
+
4
+ module CpfUtils
5
+
6
+
7
+ # Gera um número de CPF.
8
+ #
9
+ # Exemplo:
10
+ # CpfUtils.cpf => # "45698394823"
11
+ def self.cpf
12
+ Cpf.new(sample_numbers).generate_cpf
13
+ end
14
+
15
+ # Gera um número de CPF formatado.
16
+ #
17
+ # Exemplo:
18
+ # CpfUtils.cpf_formatted => # "456.983.948-23"
19
+ def self.cpf_formatted
20
+ Cpf.new(sample_numbers).generate_cpf.to_cpf_format
21
+ end
22
+
23
+ # Verifica se um CPF é válido.
24
+ #
25
+ # Exemplo:
26
+ # CpfUtils.valid_cpf?(45698394823) => # true
27
+ # CpfUtils.valid_cpf?("45698394823") => # true
28
+ # CpfUtils.valid_cpf?("456.983.948-23") => # true
29
+ def self.valid_cpf?(cpf_number)
30
+ cpf_number.to_s.valid_cpf?
31
+ end
32
+
33
+ # Sorteia nove números para compor o um novo CPF
34
+ def self.sample_numbers
35
+ [*0..9].sample(9)
36
+ end
37
+
38
+
39
+ class Cpf
40
+
41
+ # Acessor de leitura para os números
42
+ attr_reader :numbers
43
+
44
+ # Inicializador da classe
45
+ def initialize(numbers)
46
+ if numbers.is_a? String
47
+ numbers = numbers.split('')
48
+ elsif numbers.is_a? Fixnum
49
+ numbers = numbers.to_s.split('')
50
+ end
51
+
52
+ @numbers = numbers
53
+ end
54
+
55
+ # Gera o CPF propriamente dito
56
+ def generate_cpf
57
+ @numbers << first_digit
58
+ @numbers << second_digit
59
+ @numbers.join("")
60
+ end
61
+
62
+ # Gera o primeiro dígito verificador
63
+ def first_digit
64
+ if @numbers.size == 9
65
+ value = 0
66
+ multipliers = [10,9,8,7,6,5,4,3,2]
67
+
68
+ multipliers.each_with_index do |mult, index|
69
+ value += @numbers[index].to_i * mult
70
+ end
71
+
72
+ check_remainder(value % 11)
73
+ end
74
+ end
75
+
76
+ # Gera o segundo dígito verificador
77
+ def second_digit
78
+ if @numbers.size == 10
79
+ value = 0
80
+ multipliers = [11,10,9,8,7,6,5,4,3,2]
81
+
82
+ multipliers.each_with_index do |mult, index|
83
+ value += @numbers[index].to_i * mult
84
+ end
85
+
86
+ check_remainder(value % 11)
87
+ end
88
+ end
89
+
90
+ # Checa o resto da divisão
91
+ def check_remainder(remainder)
92
+ if remainder < 2
93
+ 0
94
+ else
95
+ (11 - remainder)
96
+ end
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,24 @@
1
+ require "cpf_utils"
2
+
3
+ describe String do
4
+ it "#valid_cpf_mask?" do
5
+ expect("123.456.789-12".valid_cpf_mask?).to be_true
6
+ expect("12345678912".valid_cpf_mask?).to be_true
7
+ end
8
+
9
+ it "#valid_cpf_mask? - be false" do
10
+ expect("123..456.789-12".valid_cpf_mask?).to be_false
11
+ expect("123.456.789--12".valid_cpf_mask?).to be_false
12
+ expect("123.456.789-1E".valid_cpf_mask?).to be_false
13
+ end
14
+
15
+ it "#valid_cpf?" do
16
+ cpf = CpfUtils.cpf
17
+ expect(cpf.valid_cpf?).to be_true
18
+ end
19
+
20
+ it "#to_cpf_format?" do
21
+ cpf = CpfUtils.cpf
22
+ expect(cpf.to_cpf_format).to match(/^[+-]?[0-9]{3}\.[0-9]{3}\.[0-9]{3}-[0-9]{2}+$/)
23
+ end
24
+ end
@@ -0,0 +1,89 @@
1
+ require "cpf_utils"
2
+
3
+ describe CpfUtils do
4
+ context 'CpfUtils' do
5
+ it ".cpf" do
6
+ valid_cpf = CpfUtils.cpf
7
+ expect(valid_cpf).to be_a_kind_of(String)
8
+ expect(valid_cpf.length).to eql(11)
9
+ end
10
+
11
+ it ".cpf_formatted" do
12
+ valid_cpf = CpfUtils.cpf_formatted
13
+ expect(valid_cpf).to match(/^[+-]?[0-9]{3}\.[0-9]{3}\.[0-9]{3}-[0-9]{2}+$/)
14
+ end
15
+
16
+ it ".valid_cpf?" do
17
+ valid_cpf = CpfUtils.cpf
18
+ expect(CpfUtils.valid_cpf? valid_cpf).to be_true
19
+ end
20
+
21
+ it ".valid_cpf? - be false" do
22
+ invalid_cpf = "123.456.789-12"
23
+ expect(CpfUtils.valid_cpf? invalid_cpf).to be_false
24
+ end
25
+
26
+ it ".sample_numbers" do
27
+ numbers = CpfUtils.sample_numbers
28
+ expect(numbers.size).to eql(9)
29
+ expect(numbers).to be_a_kind_of(Array)
30
+ end
31
+ end
32
+
33
+ context 'CpfUtils::Cpf' do
34
+ it "#initialize - Text" do
35
+ cpf = CpfUtils::Cpf.new("123456789")
36
+ expect(cpf.numbers).to be_a_kind_of(Array)
37
+ expect(cpf.numbers.size).to eql(9)
38
+ end
39
+
40
+ it "#initialize - Fixnum" do
41
+ cpf = CpfUtils::Cpf.new(123456789)
42
+ expect(cpf.numbers).to be_a_kind_of(Array)
43
+ expect(cpf.numbers.size).to eql(9)
44
+ end
45
+
46
+ it "#initialize - Array" do
47
+ cpf = CpfUtils::Cpf.new([1, 2, 3, 4, 5, 6, 7, 8, 9])
48
+ expect(cpf.numbers).to be_a_kind_of(Array)
49
+ expect(cpf.numbers.size).to eql(9)
50
+ end
51
+
52
+ it "#generate_cpf" do
53
+ cpf = CpfUtils::Cpf.new("123456789")
54
+ complete_cpf = cpf.generate_cpf
55
+ expect(complete_cpf.size).to eql(11)
56
+ end
57
+
58
+ it "#first_digit" do
59
+ cpf = CpfUtils::Cpf.new("123412345")
60
+ first_digit = cpf.first_digit
61
+ expect(first_digit).to eql(2)
62
+ end
63
+
64
+ it "#first_digit - false" do
65
+ cpf = CpfUtils::Cpf.new("123")
66
+ first_digit = cpf.first_digit
67
+ expect(first_digit).to be_false
68
+ end
69
+
70
+ it "#second_digit" do
71
+ cpf = CpfUtils::Cpf.new("1234567891")
72
+ second_digit = cpf.second_digit
73
+ expect(second_digit).to eql(7)
74
+ end
75
+
76
+ it "#second_digit - false" do
77
+ cpf = CpfUtils::Cpf.new("123")
78
+ second_digit = cpf.second_digit
79
+ expect(second_digit).to be_false
80
+ end
81
+
82
+ it "#check_remainder" do
83
+ cpf = CpfUtils::Cpf.new("1234567891")
84
+ expect(cpf.check_remainder(0)).to eql(0)
85
+ expect(cpf.check_remainder(1)).to eql(0)
86
+ expect(cpf.check_remainder(2)).to eql(9)
87
+ end
88
+ end
89
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cpf_utils
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Jackson Pires
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 2.14.1
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 2.14.1
55
+ description: Uma suite de funcionalidades para o CPF.
56
+ email:
57
+ - jackson.pires@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - .rspec
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - cpf_utils.gemspec
69
+ - lib/cpf_utils.rb
70
+ - lib/cpf_utils/string.rb
71
+ - lib/cpf_utils/version.rb
72
+ - spec/cpf_util/string_spec.rb
73
+ - spec/cpf_util_spec.rb
74
+ homepage: https://github.com/jacksonpires/cpf_utils
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.0.3
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Gera CPF para testes no formado tradicional ou apenas numérico, testa se
98
+ determinado número de CPF é válido, além muitas outras funcionalidades descritas
99
+ na documentação.
100
+ test_files:
101
+ - spec/cpf_util/string_spec.rb
102
+ - spec/cpf_util_spec.rb