simple_cpf_cnpj 0.2.1 → 1.0.1
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 +5 -5
- data/.github/workflows/run-tests.yml +39 -0
- data/Gemfile +0 -5
- data/README-pt.md +5 -5
- data/README.md +7 -7
- data/bin/benchmark +15 -0
- data/cpf_cnpj.gemspec +9 -6
- data/lib/simple_cpf_cnpj/version.rb +1 -1
- data/lib/simple_cpf_cnpj.rb +14 -18
- metadata +32 -18
- data/.travis.yml +0 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 99e239936871b845d6193c6a9fbacccab6e860bcbeaf44e5a74a56f439e3a70d
|
4
|
+
data.tar.gz: 8cb91799ccd828b1f89597fb4e843556b3773803326bf892bece7d6183b91fbf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4e2b9575ebca00cea1f7175016acc8ed33981855cae39dad94556a582f913823292ffd407ae353f8e3f6fd9847cfad7ef19c7311871419be31ef54efdd6f585f
|
7
|
+
data.tar.gz: 7bb3568b7dd86fbd373f3a298ce0c023cf3184afa7204c4fe9da210be8b74195475804b062e442cf8d237fbc78d13eb10b39e4f1f1ffa0c0f0d3f2d232888732
|
@@ -0,0 +1,39 @@
|
|
1
|
+
name: run-tests
|
2
|
+
on: [push]
|
3
|
+
jobs:
|
4
|
+
test:
|
5
|
+
runs-on: ubuntu-latest
|
6
|
+
strategy:
|
7
|
+
matrix:
|
8
|
+
ruby-version: ['3.2', '3.1', '3.0', '2.7', '2.3', '2.2', '2.1.9', '2.0.0', 'jruby', 'truffleruby']
|
9
|
+
env:
|
10
|
+
NO_COVERAGE: '1'
|
11
|
+
steps:
|
12
|
+
- uses: actions/checkout@v3
|
13
|
+
- name: Set up Ruby ${{ matrix.ruby-version }}
|
14
|
+
uses: ruby/setup-ruby@v1
|
15
|
+
with:
|
16
|
+
ruby-version: ${{ matrix.ruby-version }}
|
17
|
+
bundler-cache: true
|
18
|
+
- name: Install dependencies
|
19
|
+
run: bundle install
|
20
|
+
- name: Run tests
|
21
|
+
run: bundle exec rake test
|
22
|
+
coverage:
|
23
|
+
needs: [ test ]
|
24
|
+
runs-on: ubuntu-latest
|
25
|
+
steps:
|
26
|
+
- uses: actions/checkout@v3
|
27
|
+
- name: Set up Ruby ${{ matrix.ruby-version }}
|
28
|
+
uses: ruby/setup-ruby@v1
|
29
|
+
with:
|
30
|
+
ruby-version: '3.2'
|
31
|
+
bundler-cache: true
|
32
|
+
- name: Install dependencies
|
33
|
+
run: bundle install
|
34
|
+
- name: Test & publish code coverage
|
35
|
+
uses: paambaati/codeclimate-action@v3.2.0
|
36
|
+
env:
|
37
|
+
CC_TEST_REPORTER_ID: 78cf86ace5d24f5d0417cf92d63d5bf039a2a3023678acfec103fe36d4c4b0e4
|
38
|
+
with:
|
39
|
+
coverageCommand: bundle exec rake test
|
data/Gemfile
CHANGED
data/README-pt.md
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
# simple_cpf_cnpj
|
2
2
|
|
3
3
|
[](https://rubygems.org/gems/simple_cpf_cnpj)
|
4
|
-
|
5
|
-
[
|
5
|
+
[](https://codeclimate.com/github/DaviMedrade/simple_cpf_cnpj/maintainability)
|
6
|
+
[](https://codeclimate.com/github/DaviMedrade/simple_cpf_cnpj/test_coverage)
|
7
7
|
|
8
8
|
**English Version:** [README.md](rdoc-ref:README.md)
|
9
9
|
|
@@ -106,10 +106,10 @@ Após fazer checkout do repositório, execute `bin/setup` para instalar as depen
|
|
106
106
|
## Contribuindo
|
107
107
|
|
108
108
|
Relatórios de bugs e pull requests são bem-vindos. Envie pelo GitHub:
|
109
|
-
https://github.com/
|
109
|
+
https://github.com/DaviMedrade/simple_cpf_cnpj/issues
|
110
110
|
|
111
111
|
## Licença e copyright
|
112
112
|
|
113
|
-
Copyright
|
113
|
+
Copyright © 2015-2023 Davi Medrade
|
114
114
|
|
115
115
|
Este software é publicado sob a licença MIT, que pode ser encontrada no arquivo [LICENSE](rdoc-ref:LICENSE-pt).
|
data/README.md
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
# simple_cpf_cnpj
|
2
2
|
|
3
3
|
[](https://rubygems.org/gems/simple_cpf_cnpj)
|
4
|
-
|
5
|
-
[
|
5
|
+
[](https://codeclimate.com/github/DaviMedrade/simple_cpf_cnpj/maintainability)
|
6
|
+
[](https://codeclimate.com/github/DaviMedrade/simple_cpf_cnpj/test_coverage)
|
7
7
|
|
8
8
|
**Versão em Português:** [README-pt.md](rdoc-ref:README-pt.md)
|
9
9
|
|
@@ -15,7 +15,7 @@ The module provides methods to differentiate a CPF from a CNPJ, format, and vali
|
|
15
15
|
|
16
16
|
## Compatibility
|
17
17
|
|
18
|
-
The code is tested with Ruby
|
18
|
+
The code is tested with MRI Ruby 2.0-3.2, JRuby, and TruffleRuby.
|
19
19
|
|
20
20
|
## Installation
|
21
21
|
|
@@ -105,11 +105,11 @@ After checking out the repo, run `bin/setup` to install dependencies. Then, run
|
|
105
105
|
|
106
106
|
## Contributing
|
107
107
|
|
108
|
-
Bug reports and pull requests are welcome on GitHub
|
109
|
-
https://github.com/
|
108
|
+
Bug reports and pull requests are welcome on GitHub:
|
109
|
+
https://github.com/DaviMedrade/simple_cpf_cnpj/issues
|
110
110
|
|
111
111
|
## License and copyright
|
112
112
|
|
113
|
-
Copyright
|
113
|
+
Copyright © 2015-2023 Davi Medrade
|
114
114
|
|
115
115
|
This software is released under the MIT license, which can be found in the file [LICENSE](rdoc-ref:LICENSE).
|
data/bin/benchmark
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require "bundler/setup"
|
3
|
+
require "simple_cpf_cnpj"
|
4
|
+
require "benchmark"
|
5
|
+
|
6
|
+
SAMPLE_SIZE = 1_000_000
|
7
|
+
SAMPLE_LENGTH = 14 # CPF = 11; CNPJ = 14
|
8
|
+
|
9
|
+
subjects = Array.new(SAMPLE_SIZE){|_| "%0#{SAMPLE_LENGTH}u" % rand(10**SAMPLE_LENGTH) }
|
10
|
+
|
11
|
+
Benchmark.bmbm do |x|
|
12
|
+
x.report do
|
13
|
+
subjects.each{|c| CpfCnpj.valid_cpf_cnpj?(c) }
|
14
|
+
end
|
15
|
+
end
|
data/cpf_cnpj.gemspec
CHANGED
@@ -6,8 +6,8 @@ require 'simple_cpf_cnpj/version'
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
7
|
spec.name = "simple_cpf_cnpj"
|
8
8
|
spec.version = CpfCnpj::VERSION
|
9
|
-
spec.authors = ["
|
10
|
-
spec.email = ["
|
9
|
+
spec.authors = ["Davi Medrade"]
|
10
|
+
spec.email = ["davi@medrade.com.br"]
|
11
11
|
|
12
12
|
spec.summary = %q{Provides the module CpfCnpj, with methods that detect, format, and validate CPFs and CNPJs.}
|
13
13
|
spec.description = <<EOT
|
@@ -17,19 +17,22 @@ Provides the module CpfCnpj, with methods that detect, validate, and format CPFs
|
|
17
17
|
|
18
18
|
Fornece o módulo CpfCnpj, com funções que detectam, validam, e formatam CPFs e CNPJs.
|
19
19
|
EOT
|
20
|
-
spec.homepage = "https://github.com/
|
20
|
+
spec.homepage = "https://github.com/DaviMedrade/simple_cpf_cnpj"
|
21
21
|
spec.license = "MIT"
|
22
22
|
|
23
23
|
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
24
24
|
spec.bindir = "exe"
|
25
25
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
26
26
|
spec.require_paths = ["lib"]
|
27
|
-
spec.required_ruby_version = '>=
|
27
|
+
spec.required_ruby_version = '>= 2.0.0'
|
28
28
|
spec.extra_rdoc_files = ['README.md', 'README-pt.md', 'LICENSE', 'LICENSE-pt']
|
29
29
|
spec.rdoc_options << '--title' << 'simple_cpf_cnpj -- CPF/CNPJ utility methods' << '--main' << 'README.md'
|
30
30
|
|
31
|
-
spec.add_development_dependency "bundler", "
|
32
|
-
spec.add_development_dependency "rake"
|
31
|
+
spec.add_development_dependency "bundler", "> 1.10"
|
32
|
+
spec.add_development_dependency "rake"
|
33
33
|
spec.add_development_dependency "minitest"
|
34
34
|
spec.add_development_dependency "rdoc"
|
35
|
+
unless ENV['NO_COVERAGE']
|
36
|
+
spec.add_development_dependency "simplecov"
|
37
|
+
end
|
35
38
|
end
|
data/lib/simple_cpf_cnpj.rb
CHANGED
@@ -14,8 +14,11 @@ require "simple_cpf_cnpj/version"
|
|
14
14
|
# One way to do that is, for example:
|
15
15
|
# CpfCnpj.valid_cpf?(formatted_cpf.gsub(/\D/, ''))
|
16
16
|
module CpfCnpj
|
17
|
-
|
18
|
-
|
17
|
+
LENGTH_CPF = 11
|
18
|
+
LENGTH_CNPJ = 14
|
19
|
+
BLACKLIST_CPF = (0..9).collect{|n| n.to_s * LENGTH_CPF } # :nodoc:
|
20
|
+
BLACKLIST_CNPJ = (0..9).collect{|n| n.to_s * LENGTH_CNPJ } # :nodoc:
|
21
|
+
|
19
22
|
# Checks the length of +cpf_or_cnpj+ to determine if it's a CPF or a CNPJ.
|
20
23
|
#
|
21
24
|
# CpfCnpj.type_of("12345678987") # 11 characters
|
@@ -28,14 +31,14 @@ module CpfCnpj
|
|
28
31
|
# # => nil
|
29
32
|
# CpfCnpj.type_of(12345678987)
|
30
33
|
# # ArgumentError: argument must be a string or nil
|
31
|
-
|
34
|
+
def self.type_of(cpf_cnpj) # :arg: cpf_or_cnpj
|
32
35
|
return nil if cpf_cnpj.nil?
|
33
36
|
unless cpf_cnpj.is_a?(String)
|
34
37
|
raise(ArgumentError, "argument must be a string or nil")
|
35
38
|
end
|
36
39
|
case cpf_cnpj.length
|
37
|
-
when
|
38
|
-
when
|
40
|
+
when LENGTH_CPF ; :cpf
|
41
|
+
when LENGTH_CNPJ ; :cnpj
|
39
42
|
else ; nil
|
40
43
|
end
|
41
44
|
end
|
@@ -95,7 +98,7 @@ module CpfCnpj
|
|
95
98
|
return type_of(cnpj) == :cnpj && !BLACKLIST_CNPJ.include?(cnpj) && _mod11_check(cnpj, 9)
|
96
99
|
end
|
97
100
|
|
98
|
-
# Validates +
|
101
|
+
# Validates +cpf_or_cnpj+ as either a CPF or a CNPJ by verifying that the check digits match.
|
99
102
|
#
|
100
103
|
# CpfCnpj.valid_cpf_cnpj?("12345678900") # invalid CPF
|
101
104
|
# # => false
|
@@ -116,27 +119,20 @@ module CpfCnpj
|
|
116
119
|
def self._mod11_check_digit(digits, mult_max) # :nodoc:
|
117
120
|
sum = 0
|
118
121
|
mult = 2
|
119
|
-
digits[0..-2].reverse.
|
120
|
-
digit
|
121
|
-
return false
|
122
|
+
digits[0..-2].reverse.each_byte do |digit|
|
123
|
+
digit -= 48
|
124
|
+
return false if digit < 0 || digit > 9
|
122
125
|
sum += mult * digit
|
123
126
|
mult += 1
|
124
127
|
mult = 2 if mult > mult_max
|
125
128
|
end
|
126
129
|
sum = 11 - (sum % 11)
|
127
130
|
sum = 0 if sum > 9
|
128
|
-
|
131
|
+
(digits[-1].ord - 48) == sum
|
129
132
|
end
|
130
133
|
|
131
134
|
def self._mod11_check(digits, mult_max) # :nodoc:
|
135
|
+
return false unless digits.ascii_only?
|
132
136
|
_mod11_check_digit(digits[0..-2], mult_max) && _mod11_check_digit(digits, mult_max)
|
133
137
|
end
|
134
|
-
|
135
|
-
# ruby 1.8.7 doesn't have String#ord, and in versions after 1.8.7 String#[num] returns a
|
136
|
-
# one-character string instead of the byte value of the character.
|
137
|
-
# Since the return of this is always used to perform what is essentially an unclamped to_i
|
138
|
-
# on the first character, do it right here.
|
139
|
-
def self._int_value(str) # :nodoc:
|
140
|
-
(str.respond_to?(:ord) ? str.ord : str[0]) - 48
|
141
|
-
end
|
142
138
|
end
|
metadata
CHANGED
@@ -1,43 +1,43 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_cpf_cnpj
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
-
|
8
|
-
autorequire:
|
7
|
+
- Davi Medrade
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-01-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - ">"
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '1.10'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - ">"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.10'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - "
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
40
|
+
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: minitest
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -66,6 +66,20 @@ dependencies:
|
|
66
66
|
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: simplecov
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
69
83
|
description: |
|
70
84
|
Provides the module CpfCnpj, with methods that detect, validate, and format CPFs and CNPJs (Brazilian federal ID numbers).
|
71
85
|
|
@@ -73,7 +87,7 @@ description: |
|
|
73
87
|
|
74
88
|
Fornece o módulo CpfCnpj, com funções que detectam, validam, e formatam CPFs e CNPJs.
|
75
89
|
email:
|
76
|
-
-
|
90
|
+
- davi@medrade.com.br
|
77
91
|
executables: []
|
78
92
|
extensions: []
|
79
93
|
extra_rdoc_files:
|
@@ -82,24 +96,25 @@ extra_rdoc_files:
|
|
82
96
|
- LICENSE
|
83
97
|
- LICENSE-pt
|
84
98
|
files:
|
99
|
+
- ".github/workflows/run-tests.yml"
|
85
100
|
- ".gitignore"
|
86
|
-
- ".travis.yml"
|
87
101
|
- Gemfile
|
88
102
|
- LICENSE
|
89
103
|
- LICENSE-pt
|
90
104
|
- README-pt.md
|
91
105
|
- README.md
|
92
106
|
- Rakefile
|
107
|
+
- bin/benchmark
|
93
108
|
- bin/console
|
94
109
|
- bin/setup
|
95
110
|
- cpf_cnpj.gemspec
|
96
111
|
- lib/simple_cpf_cnpj.rb
|
97
112
|
- lib/simple_cpf_cnpj/version.rb
|
98
|
-
homepage: https://github.com/
|
113
|
+
homepage: https://github.com/DaviMedrade/simple_cpf_cnpj
|
99
114
|
licenses:
|
100
115
|
- MIT
|
101
116
|
metadata: {}
|
102
|
-
post_install_message:
|
117
|
+
post_install_message:
|
103
118
|
rdoc_options:
|
104
119
|
- "--title"
|
105
120
|
- simple_cpf_cnpj -- CPF/CNPJ utility methods
|
@@ -111,16 +126,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
111
126
|
requirements:
|
112
127
|
- - ">="
|
113
128
|
- !ruby/object:Gem::Version
|
114
|
-
version:
|
129
|
+
version: 2.0.0
|
115
130
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
116
131
|
requirements:
|
117
132
|
- - ">="
|
118
133
|
- !ruby/object:Gem::Version
|
119
134
|
version: '0'
|
120
135
|
requirements: []
|
121
|
-
|
122
|
-
|
123
|
-
signing_key:
|
136
|
+
rubygems_version: 3.3.23
|
137
|
+
signing_key:
|
124
138
|
specification_version: 4
|
125
139
|
summary: Provides the module CpfCnpj, with methods that detect, format, and validate
|
126
140
|
CPFs and CNPJs.
|