cpf_utils 1.0.0 → 1.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c0d2d6965f5f52f0e68ff55f132eb61320ff2ba0
4
- data.tar.gz: 86de60cc7567d557c4d2b046d6263d38cf5ae683
3
+ metadata.gz: 3f2114e18fd7ac1dfe9813aaa6644341560b925e
4
+ data.tar.gz: dd4b5fcaf9228aef7c7ec39d5d236324e9c8b91d
5
5
  SHA512:
6
- metadata.gz: e29b9ae14c7b1bd6de47566e40b05f381be364cc2711f17c59967a7b8d3bd1c8f8fbf4a9b05536609d4304ac20b01c5eef072857b6c3bdb17b89f2290aff08fb
7
- data.tar.gz: 05f06aaea9eb8c9b80c090407523b1a73b6d996c969687d8127d87ced37d5adb95670f008f646de65db77446a9b770bb3104a6057cf1e4d72377064e30c1626b
6
+ metadata.gz: 7b1974ca3f4d387c40f8bb948b993aeaf695f550359ba91ec4529deecb2e4e8c1940b7ef7668b2bba960ee461ee8bb0fe617a7ece279eb66f52df9ddbd9458fc
7
+ data.tar.gz: b91138132f481c28794c60375ee1499f81340a1ec28b513b3e43d1b2d8953a0a23003256efe7ea76bbc16e70c33c2a4107259a21813dcd2a887f51dda8a013cd
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ rvm:
2
+ - 1.9.2
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - 2.1.0
6
+ script: "bundle exec rspec spec"
data/README.md CHANGED
@@ -1,4 +1,7 @@
1
1
  # CpfUtils
2
+ [![Gem Version](https://badge.fury.io/rb/cpf_utils.png)](http://badge.fury.io/rb/cpf_utils)
3
+ [![Build status](https://secure.travis-ci.org/jacksonpires/cpf_utils.png)](https://secure.travis-ci.org/jacksonpires/cpf_utils)
4
+ [![Code Climate](https://codeclimate.com/github/jacksonpires/cpf_utils.png)](https://codeclimate.com/github/jacksonpires/cpf_utils)
2
5
 
3
6
  CpfUtils é uma suite de funcionalidades para CPF.
4
7
  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.
@@ -0,0 +1,62 @@
1
+ module CpfUtils
2
+ class Cpf
3
+
4
+ # Acessor de leitura para os números
5
+ attr_reader :numbers
6
+
7
+ # Inicializador da classe
8
+ def initialize(numbers)
9
+ if numbers.is_a? String
10
+ numbers = numbers.split('')
11
+ elsif numbers.is_a? Fixnum
12
+ numbers = numbers.to_s.split('')
13
+ end
14
+
15
+ @numbers = numbers
16
+ end
17
+
18
+ # Gera o CPF propriamente dito
19
+ def generate_cpf
20
+ @numbers << first_digit
21
+ @numbers << second_digit
22
+ @numbers.join("")
23
+ end
24
+
25
+ # Gera o primeiro dígito verificador
26
+ def first_digit
27
+ if @numbers.size == 9
28
+ value = 0
29
+ multipliers = [10,9,8,7,6,5,4,3,2]
30
+
31
+ multipliers.each_with_index do |mult, index|
32
+ value += @numbers[index].to_i * mult
33
+ end
34
+
35
+ check_remainder(value % 11)
36
+ end
37
+ end
38
+
39
+ # Gera o segundo dígito verificador
40
+ def second_digit
41
+ if @numbers.size == 10
42
+ value = 0
43
+ multipliers = [11,10,9,8,7,6,5,4,3,2]
44
+
45
+ multipliers.each_with_index do |mult, index|
46
+ value += @numbers[index].to_i * mult
47
+ end
48
+
49
+ check_remainder(value % 11)
50
+ end
51
+ end
52
+
53
+ # Checa o resto da divisão
54
+ def check_remainder(remainder)
55
+ if remainder < 2
56
+ 0
57
+ else
58
+ (11 - remainder)
59
+ end
60
+ end
61
+ end
62
+ end
@@ -56,4 +56,19 @@ class String
56
56
  def generate_cpf_formatted
57
57
  generate_cpf.to_cpf_format
58
58
  end
59
+
60
+ # Apelido 'mascara_de_cpf_valida' para o método valid_cpf_mask
61
+ alias_method :mascara_de_cpf_valida?, :valid_cpf_mask?
62
+
63
+ # Apelido 'cpf_valido?' para o método valid_cpf?
64
+ alias_method :cpf_valido?, :valid_cpf?
65
+
66
+ # Apelido 'para_formato_cpf' para o método to_cpf_format
67
+ alias_method :para_formato_cpf, :to_cpf_format
68
+
69
+ # Apelido 'gerar_cpf' para o método generate_cpf
70
+ alias_method :gerar_cpf, :generate_cpf
71
+
72
+ # Apelido 'gerar_cpf_formatado' para o método generate_cpf_formatted
73
+ alias_method :gerar_cpf_formatado, :generate_cpf_formatted
59
74
  end
@@ -1,3 +1,3 @@
1
1
  module CpfUtils
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end
data/lib/cpf_utils.rb CHANGED
@@ -1,9 +1,9 @@
1
1
  require "cpf_utils/version"
2
2
  require "cpf_utils/string"
3
+ require "cpf_utils/cpf"
3
4
 
4
5
  module CpfUtils
5
6
 
6
-
7
7
  # Gera um número de CPF.
8
8
  #
9
9
  # Exemplo:
@@ -20,6 +20,14 @@ module CpfUtils
20
20
  Cpf.new(sample_numbers).generate_cpf.to_cpf_format
21
21
  end
22
22
 
23
+ # Gera um número de CPF formatado.
24
+ #
25
+ # Exemplo:
26
+ # CpfUtils.cpf_formatado => # "456.983.948-23"
27
+ def self.cpf_formatado
28
+ self.cpf_formatted
29
+ end
30
+
23
31
  # Verifica se um CPF é válido.
24
32
  #
25
33
  # Exemplo:
@@ -30,70 +38,20 @@ module CpfUtils
30
38
  cpf_number.to_s.valid_cpf?
31
39
  end
32
40
 
33
- # Sorteia nove números para compor o um novo CPF
34
- def self.sample_numbers
35
- [*0..9].sample(9)
41
+ # Verifica se um CPF é válido.
42
+ #
43
+ # Exemplo:
44
+ # CpfUtils.cpf_valido?(45698394823) => # true
45
+ # CpfUtils.cpf_valido?("45698394823") => # true
46
+ # CpfUtils.cpf_valido?("456.983.948-23") => # true
47
+ def self.cpf_valido?(cpf_number)
48
+ self.valid_cpf?(cpf_number)
36
49
  end
37
50
 
51
+ private
38
52
 
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
53
+ # Sorteia nove números para compor o um novo CPF
54
+ def self.sample_numbers
55
+ [*0..9].sample(9)
98
56
  end
99
57
  end
@@ -1,36 +1,9 @@
1
- require "cpf_utils"
1
+ require "spec_helper"
2
2
 
3
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
4
 
33
5
  context 'CpfUtils::Cpf' do
6
+
34
7
  it "#initialize - Text" do
35
8
  cpf = CpfUtils::Cpf.new("123456789")
36
9
  expect(cpf.numbers).to be_a_kind_of(Array)
@@ -0,0 +1,75 @@
1
+ require "spec_helper"
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 "#mascara_de_cpf_valida?" do
10
+ expect("123.456.789-12".mascara_de_cpf_valida?).to be_true
11
+ expect("12345678912".mascara_de_cpf_valida?).to be_true
12
+ end
13
+
14
+ it "#valid_cpf_mask? - be false" do
15
+ expect("123..456.789-12".valid_cpf_mask?).to be_false
16
+ expect("123.456.789--12".valid_cpf_mask?).to be_false
17
+ expect("123.456.789-1E".valid_cpf_mask?).to be_false
18
+ end
19
+
20
+ it "#mascara_de_cpf_valida? - be false" do
21
+ expect("123..456.789-12".mascara_de_cpf_valida?).to be_false
22
+ expect("123.456.789--12".mascara_de_cpf_valida?).to be_false
23
+ expect("123.456.789-1E".mascara_de_cpf_valida?).to be_false
24
+ end
25
+
26
+ it "#valid_cpf?" do
27
+ cpf = CpfUtils.cpf
28
+ expect(cpf.valid_cpf?).to be_true
29
+ end
30
+
31
+ it "#cpf_valido?" do
32
+ cpf = CpfUtils.cpf
33
+ expect(cpf.cpf_valido?).to be_true
34
+ end
35
+
36
+ it "#to_cpf_format?" do
37
+ cpf = CpfUtils.cpf
38
+ expect(cpf.to_cpf_format).to match(/^[+-]?[0-9]{3}\.[0-9]{3}\.[0-9]{3}-[0-9]{2}+$/)
39
+ end
40
+
41
+ it "#para_formato_cpf?" do
42
+ cpf = CpfUtils.cpf
43
+ expect(cpf.para_formato_cpf).to match(/^[+-]?[0-9]{3}\.[0-9]{3}\.[0-9]{3}-[0-9]{2}+$/)
44
+ end
45
+
46
+ it "#generate_cpf" do
47
+ valid_cpf = CpfUtils.cpf
48
+ final_cpf = valid_cpf[0..8].generate_cpf
49
+
50
+ expect(final_cpf).to be_a_kind_of(String)
51
+ expect(final_cpf.length).to eql(11)
52
+ end
53
+
54
+ it "#gerar_cpf" do
55
+ valid_cpf = CpfUtils.cpf
56
+ final_cpf = valid_cpf[0..8].gerar_cpf
57
+
58
+ expect(final_cpf).to be_a_kind_of(String)
59
+ expect(final_cpf.length).to eql(11)
60
+ end
61
+
62
+ it "#gerar_cpf_formatado" do
63
+ valid_cpf = CpfUtils.cpf
64
+ final_cpf = valid_cpf[0..8].gerar_cpf_formatado
65
+
66
+ expect(final_cpf).to match(/^[+-]?[0-9]{3}\.[0-9]{3}\.[0-9]{3}-[0-9]{2}+$/)
67
+ end
68
+
69
+ it "#generate_cpf_formatted" do
70
+ valid_cpf = CpfUtils.cpf
71
+ final_cpf = valid_cpf[0..8].generate_cpf_formatted
72
+
73
+ expect(final_cpf).to match(/^[+-]?[0-9]{3}\.[0-9]{3}\.[0-9]{3}-[0-9]{2}+$/)
74
+ end
75
+ end
@@ -0,0 +1,41 @@
1
+ require "spec_helper"
2
+
3
+ describe CpfUtils do
4
+
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 ".cpf_formatado" do
17
+ valid_cpf = CpfUtils.cpf_formatado
18
+ expect(valid_cpf).to match(/^[+-]?[0-9]{3}\.[0-9]{3}\.[0-9]{3}-[0-9]{2}+$/)
19
+ end
20
+
21
+ it ".valid_cpf?" do
22
+ valid_cpf = CpfUtils.cpf
23
+ expect(CpfUtils.valid_cpf? valid_cpf).to be_true
24
+ end
25
+
26
+ it ".cpf_valido?" do
27
+ cpf_valido = CpfUtils.cpf
28
+ expect(CpfUtils.cpf_valido? cpf_valido).to be_true
29
+ end
30
+
31
+ it ".valid_cpf? - be false" do
32
+ invalid_cpf = "123.456.789-12"
33
+ expect(CpfUtils.valid_cpf? invalid_cpf).to be_false
34
+ end
35
+
36
+ it ".sample_numbers" do
37
+ numbers = CpfUtils.sample_numbers
38
+ expect(numbers.size).to eql(9)
39
+ expect(numbers).to be_a_kind_of(Array)
40
+ end
41
+ end
@@ -0,0 +1,2 @@
1
+ require "cpf_utils"
2
+ require_relative "../lib/cpf_utils/cpf"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cpf_utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jackson Pires
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-22 00:00:00.000000000 Z
11
+ date: 2014-03-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -61,16 +61,20 @@ extra_rdoc_files: []
61
61
  files:
62
62
  - .gitignore
63
63
  - .rspec
64
+ - .travis.yml
64
65
  - Gemfile
65
66
  - LICENSE.txt
66
67
  - README.md
67
68
  - Rakefile
68
69
  - cpf_utils.gemspec
69
70
  - lib/cpf_utils.rb
71
+ - lib/cpf_utils/cpf.rb
70
72
  - lib/cpf_utils/string.rb
71
73
  - lib/cpf_utils/version.rb
72
- - spec/cpf_util/string_spec.rb
73
- - spec/cpf_util_spec.rb
74
+ - spec/cpf_utils/cpf.rb
75
+ - spec/cpf_utils/string_spec.rb
76
+ - spec/cpf_utils_spec.rb
77
+ - spec/spec_helper.rb
74
78
  homepage: https://github.com/jacksonpires/cpf_utils
75
79
  licenses:
76
80
  - MIT
@@ -98,5 +102,7 @@ summary: Gera CPF para testes no formado tradicional ou apenas numérico, testa
98
102
  determinado número de CPF é válido, além muitas outras funcionalidades descritas
99
103
  na documentação.
100
104
  test_files:
101
- - spec/cpf_util/string_spec.rb
102
- - spec/cpf_util_spec.rb
105
+ - spec/cpf_utils/cpf.rb
106
+ - spec/cpf_utils/string_spec.rb
107
+ - spec/cpf_utils_spec.rb
108
+ - spec/spec_helper.rb
@@ -1,24 +0,0 @@
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