cpf_cnpj 0.3.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +59 -0
- data/.travis.yml +4 -3
- data/Gemfile +2 -0
- data/Rakefile +5 -3
- data/bin/cnpj +2 -0
- data/bin/cpf +2 -0
- data/cpf_cnpj.gemspec +6 -5
- data/lib/cnpj.rb +6 -3
- data/lib/cnpj/formatter.rb +3 -1
- data/lib/cnpj/verifier_digit.rb +4 -2
- data/lib/cpf.rb +6 -2
- data/lib/cpf/formatter.rb +2 -0
- data/lib/cpf/verifier_digit.rb +2 -0
- data/lib/cpf_cnpj.rb +3 -0
- data/lib/cpf_cnpj/cli.rb +37 -16
- data/lib/cpf_cnpj/generator.rb +3 -1
- data/lib/cpf_cnpj/version.rb +3 -1
- data/test/support/capture_syscall.rb +4 -0
- data/test/test_helper.rb +3 -2
- data/test/unit/cnpj/cli_test.rb +3 -1
- data/test/unit/cnpj/formatter_test.rb +2 -0
- data/test/unit/cnpj/generator_test.rb +2 -0
- data/test/unit/cnpj_test.rb +12 -1
- data/test/unit/cpf/cli_test.rb +4 -4
- data/test/unit/cpf/formatter_test.rb +2 -0
- data/test/unit/cpf/generator_test.rb +2 -0
- data/test/unit/cpf_test.rb +13 -2
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 060f6cfefb56f74570849d75281ab0f5d00fe500
|
4
|
+
data.tar.gz: 52d7c441db65b19ce5175aa82c168a670e708f06
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 21a524afa4993d82d12b1abe9ee15f957a786acf5ba89704dc5916f35946e475d50842ce40f78e56bd979c4ec66afdd90376da8d011b28e41e5203a31e8a5530
|
7
|
+
data.tar.gz: 9c25eb4c0c480eb29b040b8595d1b1a9b9e768f21dd689f4c24adc0b4f22be4051624013398a4259c400985c1f843e7a35859681409b0b3a9205f0f8f53866e5
|
data/.rubocop.yml
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.3
|
3
|
+
|
4
|
+
Include:
|
5
|
+
- '**/*.gemspec'
|
6
|
+
- '**/*.podspec'
|
7
|
+
- '**/*.jbuilder'
|
8
|
+
- '**/*.rake'
|
9
|
+
- '**/*.opal'
|
10
|
+
- '**/config.ru'
|
11
|
+
- '**/Gemfile'
|
12
|
+
- '**/Rakefile'
|
13
|
+
- '**/Capfile'
|
14
|
+
- '**/Guardfile'
|
15
|
+
- '**/Podfile'
|
16
|
+
- '**/Thorfile'
|
17
|
+
- '**/Vagrantfile'
|
18
|
+
- '**/Berksfile'
|
19
|
+
- '**/Cheffile'
|
20
|
+
- '**/Vagabondfile'
|
21
|
+
|
22
|
+
Style/Documentation:
|
23
|
+
Enabled: false
|
24
|
+
|
25
|
+
Style/StringLiterals:
|
26
|
+
EnforcedStyle: double_quotes
|
27
|
+
|
28
|
+
Layout/SpaceInsideBlockBraces:
|
29
|
+
EnforcedStyle: space
|
30
|
+
EnforcedStyleForEmptyBraces: space
|
31
|
+
SpaceBeforeBlockParameters: false
|
32
|
+
|
33
|
+
Layout/SpaceInsideHashLiteralBraces:
|
34
|
+
EnforcedStyle: no_space
|
35
|
+
EnforcedStyleForEmptyBraces: no_space
|
36
|
+
|
37
|
+
Layout/FirstArrayElementLineBreak:
|
38
|
+
Enabled: true
|
39
|
+
|
40
|
+
Layout/FirstHashElementLineBreak:
|
41
|
+
Enabled: true
|
42
|
+
|
43
|
+
Style/SymbolArray:
|
44
|
+
Enabled: true
|
45
|
+
|
46
|
+
Style/PercentLiteralDelimiters:
|
47
|
+
PreferredDelimiters:
|
48
|
+
'%': '[]'
|
49
|
+
'%i': '[]'
|
50
|
+
'%q': '[]'
|
51
|
+
'%Q': '[]'
|
52
|
+
'%r': '[]'
|
53
|
+
'%s': '[]'
|
54
|
+
'%w': '[]'
|
55
|
+
'%W': '[]'
|
56
|
+
'%x': '[]'
|
57
|
+
|
58
|
+
Metrics/LineLength:
|
59
|
+
Max: 100
|
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
data/Rakefile
CHANGED
@@ -1,11 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "bundler/gem_tasks"
|
2
4
|
require "rake/testtask"
|
3
5
|
|
4
6
|
Rake::TestTask.new(:test) do |t|
|
5
|
-
t.libs <<
|
6
|
-
t.test_files = FileList[
|
7
|
+
t.libs << "test"
|
8
|
+
t.test_files = FileList["test/**/*_test.rb"]
|
7
9
|
t.verbose = true
|
8
10
|
t.warning = false
|
9
11
|
end
|
10
12
|
|
11
|
-
task :
|
13
|
+
task default: :test
|
data/bin/cnpj
CHANGED
data/bin/cpf
CHANGED
data/cpf_cnpj.gemspec
CHANGED
@@ -1,5 +1,6 @@
|
|
1
|
-
#
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "./lib/cpf_cnpj/version"
|
3
4
|
|
4
5
|
Gem::Specification.new do |gem|
|
5
6
|
gem.authors = ["Nando Vieira"]
|
@@ -7,9 +8,9 @@ Gem::Specification.new do |gem|
|
|
7
8
|
gem.description = "Validate, generate and format CPF/CNPJ numbers. Include command-line tools."
|
8
9
|
gem.summary = gem.description
|
9
10
|
|
10
|
-
gem.files = `git ls-files`.split(
|
11
|
-
gem.executables = gem.files.grep(%r
|
12
|
-
gem.test_files = gem.files.grep(%r
|
11
|
+
gem.files = `git ls-files`.split($OUTPUT_RECORD_SEPARATOR)
|
12
|
+
gem.executables = gem.files.grep(%r[^bin/]).map {|f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r[^(test|spec|features)/])
|
13
14
|
gem.name = "cpf_cnpj"
|
14
15
|
gem.require_paths = ["lib"]
|
15
16
|
gem.version = CPF::VERSION
|
data/lib/cnpj.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class CNPJ
|
2
4
|
require "cpf_cnpj"
|
3
5
|
require "cnpj/formatter"
|
@@ -6,7 +8,8 @@ class CNPJ
|
|
6
8
|
attr_reader :number
|
7
9
|
attr_reader :strict
|
8
10
|
|
9
|
-
REGEX =
|
11
|
+
REGEX = %r[\A\d{2}\.\d{3}.\d{3}/\d{4}-\d{2}\Z]
|
12
|
+
VALIDATION_SIZE_REGEX = /^\d{14}$/
|
10
13
|
NUMBER_SIZE = 12
|
11
14
|
|
12
15
|
BLACKLIST = %w[
|
@@ -19,7 +22,7 @@ class CNPJ
|
|
19
22
|
66666666666666
|
20
23
|
77777777777777
|
21
24
|
88888888888888
|
22
|
-
|
25
|
+
99999999999999
|
23
26
|
].freeze
|
24
27
|
|
25
28
|
def self.valid?(number, strict: false)
|
@@ -53,7 +56,7 @@ class CNPJ
|
|
53
56
|
end
|
54
57
|
|
55
58
|
def valid?
|
56
|
-
return unless stripped
|
59
|
+
return unless stripped =~ VALIDATION_SIZE_REGEX
|
57
60
|
return if BLACKLIST.include?(stripped)
|
58
61
|
|
59
62
|
digits = numbers[0...12]
|
data/lib/cnpj/formatter.rb
CHANGED
data/lib/cnpj/verifier_digit.rb
CHANGED
@@ -1,10 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class CNPJ
|
2
4
|
class VerifierDigit
|
3
5
|
def self.generate(numbers)
|
4
6
|
index = 2
|
5
7
|
|
6
|
-
sum = numbers.reverse.reduce(0) do |
|
7
|
-
(
|
8
|
+
sum = numbers.reverse.reduce(0) do |buffer, number|
|
9
|
+
(buffer + number * index).tap do
|
8
10
|
index = index == 9 ? 2 : index + 1
|
9
11
|
end
|
10
12
|
end
|
data/lib/cpf.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class CPF
|
2
4
|
require "cpf_cnpj"
|
3
5
|
require "cpf/formatter"
|
@@ -7,6 +9,7 @@ class CPF
|
|
7
9
|
attr_reader :strict
|
8
10
|
|
9
11
|
REGEX = /\A\d{3}\.\d{3}\.\d{3}-\d{2}\Z/
|
12
|
+
VALIDATION_SIZE_REGEX = /^[0-9]{11}$/
|
10
13
|
NUMBER_SIZE = 9
|
11
14
|
|
12
15
|
BLACKLIST = %w[
|
@@ -21,6 +24,7 @@ class CPF
|
|
21
24
|
88888888888
|
22
25
|
99999999999
|
23
26
|
12345678909
|
27
|
+
01234567890
|
24
28
|
].freeze
|
25
29
|
|
26
30
|
def self.valid?(number, strict: false)
|
@@ -53,8 +57,8 @@ class CPF
|
|
53
57
|
@formatted ||= Formatter.format(number)
|
54
58
|
end
|
55
59
|
|
56
|
-
def valid?
|
57
|
-
return unless stripped
|
60
|
+
def valid?
|
61
|
+
return unless stripped =~ VALIDATION_SIZE_REGEX
|
58
62
|
return if BLACKLIST.include?(stripped)
|
59
63
|
|
60
64
|
digits = numbers[0...9]
|
data/lib/cpf/formatter.rb
CHANGED
data/lib/cpf/verifier_digit.rb
CHANGED
data/lib/cpf_cnpj.rb
CHANGED
data/lib/cpf_cnpj/cli.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module CpfCnpj
|
2
4
|
class CLI
|
3
5
|
attr_reader :document_class, :arguments, :stdin, :stdout, :stderr
|
@@ -18,9 +20,12 @@ module CpfCnpj
|
|
18
20
|
document_class.name
|
19
21
|
end
|
20
22
|
|
21
|
-
def
|
22
|
-
options
|
23
|
+
def options
|
24
|
+
@options ||= {}
|
25
|
+
end
|
23
26
|
|
27
|
+
# rubocop:disable Metrics/MethodLength, Metrics/AbcSize
|
28
|
+
def process_command
|
24
29
|
opts.banner = "Usage: #{bin_name} [options] [#{document_name} number]"
|
25
30
|
opts.separator ""
|
26
31
|
opts.separator "Specific options:"
|
@@ -47,21 +52,27 @@ module CpfCnpj
|
|
47
52
|
end
|
48
53
|
|
49
54
|
opts.on_tail("-h", "--help", "Show help") do
|
50
|
-
help
|
51
|
-
exit
|
55
|
+
help(true)
|
52
56
|
end
|
53
57
|
|
54
58
|
opts.parse!(arguments)
|
55
59
|
opts.permute!(arguments)
|
60
|
+
end
|
61
|
+
# rubocop:enable Metrics/MethodLength, Metrics/AbcSize
|
62
|
+
|
63
|
+
def input
|
64
|
+
stdin.tty? ? arguments.first : stdin.read
|
65
|
+
end
|
56
66
|
|
57
|
-
|
58
|
-
|
59
|
-
|
67
|
+
def start
|
68
|
+
process_command
|
69
|
+
help
|
70
|
+
generate(options)
|
60
71
|
document = document_class.new(input)
|
61
72
|
validate(document)
|
62
|
-
format(document)
|
63
|
-
strip(document)
|
64
|
-
check(document)
|
73
|
+
format(document)
|
74
|
+
strip(document)
|
75
|
+
check(document)
|
65
76
|
end
|
66
77
|
|
67
78
|
def validate(document)
|
@@ -76,28 +87,38 @@ module CpfCnpj
|
|
76
87
|
end
|
77
88
|
|
78
89
|
def generate(options)
|
90
|
+
return unless options[:generate]
|
91
|
+
|
79
92
|
document = document_class.new(document_class.generate)
|
80
93
|
|
81
|
-
if options[:strip]
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
94
|
+
output = if options[:strip]
|
95
|
+
document.stripped
|
96
|
+
else
|
97
|
+
document.formatted
|
98
|
+
end
|
99
|
+
|
100
|
+
stdout << output
|
86
101
|
|
87
102
|
exit
|
88
103
|
end
|
89
104
|
|
90
105
|
def format(document)
|
106
|
+
return unless options[:format]
|
107
|
+
|
91
108
|
stdout << document.formatted
|
92
109
|
exit
|
93
110
|
end
|
94
111
|
|
95
112
|
def strip(document)
|
113
|
+
return unless options[:strip]
|
114
|
+
|
96
115
|
stdout << document.stripped
|
97
116
|
exit
|
98
117
|
end
|
99
118
|
|
100
|
-
def help
|
119
|
+
def help(run = options.empty?)
|
120
|
+
return unless run
|
121
|
+
|
101
122
|
stderr << opts.to_s
|
102
123
|
exit 1
|
103
124
|
end
|
data/lib/cpf_cnpj/generator.rb
CHANGED
@@ -1,9 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module CpfCnpj
|
2
4
|
class Generator
|
3
5
|
NUMBERS = [*0..9].freeze
|
4
6
|
|
5
7
|
def self.generate(size, verifier_digit_generator)
|
6
|
-
numbers = size
|
8
|
+
numbers = Array.new(size) { NUMBERS.sample }
|
7
9
|
numbers << verifier_digit_generator.generate(numbers)
|
8
10
|
numbers << verifier_digit_generator.generate(numbers)
|
9
11
|
numbers.join("")
|
data/lib/cpf_cnpj/version.rb
CHANGED
data/test/test_helper.rb
CHANGED
data/test/unit/cnpj/cli_test.rb
CHANGED
data/test/unit/cnpj_test.rb
CHANGED
@@ -1,7 +1,10 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "test_helper"
|
2
4
|
|
3
5
|
class CnpjTest < Minitest::Test
|
4
6
|
test "blacklists common numbers" do
|
7
|
+
refute CNPJ.valid?("00000000000000")
|
5
8
|
refute CNPJ.valid?("11111111111111")
|
6
9
|
refute CNPJ.valid?("22222222222222")
|
7
10
|
refute CNPJ.valid?("33333333333333")
|
@@ -41,6 +44,14 @@ class CnpjTest < Minitest::Test
|
|
41
44
|
end
|
42
45
|
|
43
46
|
test "generates stripped number" do
|
44
|
-
assert_match
|
47
|
+
assert_match(/\A\d{14}\z/, CNPJ.generate)
|
48
|
+
end
|
49
|
+
|
50
|
+
test "rejects strings" do
|
51
|
+
refute CNPJ.valid?("aa.bb.ccc/dddd-ee")
|
52
|
+
end
|
53
|
+
|
54
|
+
test "rejects strings (strict)" do
|
55
|
+
refute CNPJ.valid?("aa.bb.ccc/dddd-ee", strict: true)
|
45
56
|
end
|
46
57
|
end
|
data/test/unit/cpf/cli_test.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "test_helper"
|
2
4
|
|
3
5
|
module CPFCli
|
@@ -54,7 +56,7 @@ module CPFCli
|
|
54
56
|
|
55
57
|
%w[-v --version].each do |switch|
|
56
58
|
test "outputs version [using #{switch}]" do
|
57
|
-
exit_status, stdout,
|
59
|
+
exit_status, stdout, _stderr = capture_syscall do
|
58
60
|
system "./bin/cpf #{switch}"
|
59
61
|
end
|
60
62
|
|
@@ -84,7 +86,7 @@ module CPFCli
|
|
84
86
|
end
|
85
87
|
|
86
88
|
assert_equal 0, exit_status
|
87
|
-
assert_match
|
89
|
+
assert_match(/\A\d{11}\Z/, stdout)
|
88
90
|
end
|
89
91
|
end
|
90
92
|
|
@@ -103,8 +105,6 @@ module CPFCli
|
|
103
105
|
end
|
104
106
|
|
105
107
|
test "formats argument using stdin" do
|
106
|
-
stdin = StringIO.new("76616598837")
|
107
|
-
|
108
108
|
exit_status, stdout = capture_syscall do
|
109
109
|
system "echo 76616598837 | ./bin/cpf --format"
|
110
110
|
end
|
data/test/unit/cpf_test.rb
CHANGED
@@ -1,7 +1,10 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "test_helper"
|
2
4
|
|
3
5
|
class CpfTest < Minitest::Test
|
4
6
|
test "blacklists common numbers" do
|
7
|
+
refute CPF.valid?("01234567890")
|
5
8
|
refute CPF.valid?("11111111111")
|
6
9
|
refute CPF.valid?("22222222222")
|
7
10
|
refute CPF.valid?("33333333333")
|
@@ -18,6 +21,10 @@ class CpfTest < Minitest::Test
|
|
18
21
|
refute CPF.valid?("")
|
19
22
|
end
|
20
23
|
|
24
|
+
test "rejects strings" do
|
25
|
+
refute CPF.valid?("aaa.bbb.ccc-dd")
|
26
|
+
end
|
27
|
+
|
21
28
|
test "rejects nil values" do
|
22
29
|
refute CPF.valid?(nil)
|
23
30
|
end
|
@@ -43,6 +50,10 @@ class CpfTest < Minitest::Test
|
|
43
50
|
assert CPF.valid?("29537995593", strict: true)
|
44
51
|
end
|
45
52
|
|
53
|
+
test "rejects strings (strict)" do
|
54
|
+
refute CPF.valid?("aaa.bbb.ccc-dd", strict: true)
|
55
|
+
end
|
56
|
+
|
46
57
|
test "returns stripped number" do
|
47
58
|
cpf = CPF.new("295.379.955-93")
|
48
59
|
assert_equal "29537995593", cpf.stripped
|
@@ -54,11 +65,11 @@ class CpfTest < Minitest::Test
|
|
54
65
|
end
|
55
66
|
|
56
67
|
test "generates formatted number" do
|
57
|
-
assert_match
|
68
|
+
assert_match(/\A\d{3}\.\d{3}\.\d{3}-\d{2}\z/, CPF.generate(true))
|
58
69
|
end
|
59
70
|
|
60
71
|
test "generates stripped number" do
|
61
|
-
assert_match
|
72
|
+
assert_match(/\A\d{11}\z/, CPF.generate)
|
62
73
|
end
|
63
74
|
|
64
75
|
test "invalidates memoization cache" do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cpf_cnpj
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nando Vieira
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-10-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: pry-meta
|
@@ -63,6 +63,7 @@ extensions: []
|
|
63
63
|
extra_rdoc_files: []
|
64
64
|
files:
|
65
65
|
- ".gitignore"
|
66
|
+
- ".rubocop.yml"
|
66
67
|
- ".travis.yml"
|
67
68
|
- Gemfile
|
68
69
|
- LICENSE
|
@@ -110,7 +111,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
110
111
|
version: '0'
|
111
112
|
requirements: []
|
112
113
|
rubyforge_project:
|
113
|
-
rubygems_version: 2.
|
114
|
+
rubygems_version: 2.6.13
|
114
115
|
signing_key:
|
115
116
|
specification_version: 4
|
116
117
|
summary: Validate, generate and format CPF/CNPJ numbers. Include command-line tools.
|