cpf_cnpj_tools 0.2.1 → 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 +4 -4
- data/.rubocop.yml +29 -22
- data/CHANGELOG.md +13 -13
- data/README.md +43 -40
- data/lib/cpf_cnpj_tools/version.rb +1 -1
- data/lib/cpf_cnpj_tools.rb +250 -174
- data/lib/invalid_cpf_cnpj_format_exception.rb +10 -10
- data/sig/cpf_cnpj_tools/generator.rbs +24 -0
- metadata +5 -50
- data/cpf_cnpj_tools.gemspec +0 -39
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 960e2816d23cd9e748a8b09c2c9938b1528b68659e9e278d2a9b521428701b68
|
|
4
|
+
data.tar.gz: 211d39ed878ae4417c0b242dc42d6db1e8e94c55c3b95859a9729e79d9db266b
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c2b1e1db0a91f2eac6a68f9d8c14364096c6e1d4b35c43b6d22b81ed295428e6483e702da84630553b0ec36cfc9c9b83960ff94f525e28470a0cd59a83a3eb19
|
|
7
|
+
data.tar.gz: f8f44be528bdd50da122e62d8a61ca4d763ae1a2bf614c06ff80fb25885aa1fbb2790ca6b290cfcb2318a3bdb1d5466a2d4701df534406fa42e45e4a080f17ee
|
data/.rubocop.yml
CHANGED
|
@@ -1,22 +1,29 @@
|
|
|
1
|
-
AllCops:
|
|
2
|
-
TargetRubyVersion: 2.6
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
1
|
+
AllCops:
|
|
2
|
+
TargetRubyVersion: 2.6
|
|
3
|
+
SuggestExtensions: false
|
|
4
|
+
|
|
5
|
+
Style/StringLiterals:
|
|
6
|
+
Enabled: true
|
|
7
|
+
EnforcedStyle: double_quotes
|
|
8
|
+
|
|
9
|
+
Style/StringLiteralsInInterpolation:
|
|
10
|
+
Enabled: true
|
|
11
|
+
EnforcedStyle: double_quotes
|
|
12
|
+
|
|
13
|
+
Layout/LineLength:
|
|
14
|
+
Max: 120
|
|
15
|
+
|
|
16
|
+
Style/EndOfLine:
|
|
17
|
+
Enabled: False
|
|
18
|
+
|
|
19
|
+
Lint/ScriptPermission:
|
|
20
|
+
Enabled: False
|
|
21
|
+
|
|
22
|
+
Metrics/MethodLength:
|
|
23
|
+
Max: 20
|
|
24
|
+
|
|
25
|
+
Metrics/ClassLength:
|
|
26
|
+
Max: 150
|
|
27
|
+
|
|
28
|
+
Metrics/AbcSize:
|
|
29
|
+
Enabled: false
|
data/CHANGELOG.md
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
## [Unreleased]
|
|
2
|
-
|
|
3
|
-
## [0.1.0] - 2022-10-11
|
|
4
|
-
|
|
5
|
-
- Initial release
|
|
6
|
-
|
|
7
|
-
## [0.2.0] - 2022-10-12
|
|
8
|
-
|
|
9
|
-
- Adding formatation methods
|
|
10
|
-
|
|
11
|
-
## [0.2.1] - 2022-10-12
|
|
12
|
-
|
|
13
|
-
- Adding gem dependencies to gemspec
|
|
1
|
+
## [Unreleased]
|
|
2
|
+
|
|
3
|
+
## [0.1.0] - 2022-10-11
|
|
4
|
+
|
|
5
|
+
- Initial release
|
|
6
|
+
|
|
7
|
+
## [0.2.0] - 2022-10-12
|
|
8
|
+
|
|
9
|
+
- Adding formatation methods
|
|
10
|
+
|
|
11
|
+
## [0.2.1] - 2022-10-12
|
|
12
|
+
|
|
13
|
+
- Adding gem dependencies to gemspec
|
data/README.md
CHANGED
|
@@ -1,40 +1,43 @@
|
|
|
1
|
-
# CpfCnpjTools
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
puts generate.
|
|
26
|
-
puts generate.formatted
|
|
27
|
-
puts generate.
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
1
|
+
# CpfCnpjTools
|
|
2
|
+
|
|
3
|
+
A simple and very efficient Ruby Gem to validate and generate CPF and CNPJ values.
|
|
4
|
+
** It supports the new alphanumeric CNPJ format!!! **
|
|
5
|
+
|
|
6
|
+
## Installation
|
|
7
|
+
|
|
8
|
+
Install the gem and add to the application's Gemfile by executing:
|
|
9
|
+
|
|
10
|
+
$ bundle add cpf_cnpj_tools
|
|
11
|
+
|
|
12
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
|
13
|
+
|
|
14
|
+
$ gem install cpf_cnpj_tools
|
|
15
|
+
|
|
16
|
+
## Usage
|
|
17
|
+
|
|
18
|
+
For usage, import the gem and create a new instance of it:
|
|
19
|
+
|
|
20
|
+
```ruby
|
|
21
|
+
require "cpf_cnpj_tools"
|
|
22
|
+
|
|
23
|
+
generate = CpfCnpjTools::Generator.new
|
|
24
|
+
|
|
25
|
+
puts generate.generate_cpf(formatted: false) # returns a valid CPF
|
|
26
|
+
puts generate.generate_cnpj(formatted: false) # returns a valid cnpj
|
|
27
|
+
puts generate.cpf_valid?("999999900") # validate returning true or false
|
|
28
|
+
puts generate.cnpj_valid?("99999999000100") # validate returning true or false
|
|
29
|
+
puts generate.formatted?("99999999900") # validate if cpf/cnpj is formatted returning true or false
|
|
30
|
+
puts generate.format("16255648800") # return the formatted cpf/cnpj. Ex: 162.556.488-00
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Contributing
|
|
34
|
+
|
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/ariasdiniz/cpf_cnpj_tools. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/ariasdiniz/cpf_cnpj_tools/blob/main/CODE_OF_CONDUCT.md).
|
|
36
|
+
|
|
37
|
+
## License
|
|
38
|
+
|
|
39
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
40
|
+
|
|
41
|
+
## Code of Conduct
|
|
42
|
+
|
|
43
|
+
Everyone interacting in the CpfCnpjTools project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/ariasdiniz/cpf_cnpj_tools/blob/main/CODE_OF_CONDUCT.md).
|
data/lib/cpf_cnpj_tools.rb
CHANGED
|
@@ -1,174 +1,250 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require_relative "cpf_cnpj_tools/version"
|
|
4
|
-
require_relative "invalid_cpf_cnpj_format_exception"
|
|
5
|
-
|
|
6
|
-
module CpfCnpjTools
|
|
7
|
-
##
|
|
8
|
-
# Class responsible for generating and
|
|
9
|
-
# validating CPF and CNPJ numbers
|
|
10
|
-
class Generator
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
return false if (
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
#
|
|
86
|
-
#
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
#
|
|
122
|
-
#
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "cpf_cnpj_tools/version"
|
|
4
|
+
require_relative "invalid_cpf_cnpj_format_exception"
|
|
5
|
+
|
|
6
|
+
module CpfCnpjTools
|
|
7
|
+
##
|
|
8
|
+
# Class responsible for generating and
|
|
9
|
+
# validating CPF and CNPJ numbers
|
|
10
|
+
class Generator
|
|
11
|
+
BLACKLIST_CPF = %w[
|
|
12
|
+
00000000000 11111111111 22222222222 33333333333 44444444444
|
|
13
|
+
55555555555 66666666666 77777777777 88888888888 99999999999
|
|
14
|
+
].freeze
|
|
15
|
+
|
|
16
|
+
REGEX_CPF_FORMATTED = /^\d{3}\.\d{3}\.\d{3}-\d{2}$/.freeze
|
|
17
|
+
REGEX_CNPJ_FORMATTED = %r{^[A-Za-z0-9]{2}\.[A-Za-z0-9]{3}\.[A-Za-z0-9]{3}/[A-Za-z0-9]{4}-\d{2}$}.freeze
|
|
18
|
+
REGEX_CNPJ_UNFORMATTED = /^[A-Z0-9]{12}\d{2}$/.freeze
|
|
19
|
+
|
|
20
|
+
CPF1DIGIT = [10, 9, 8, 7, 6, 5, 4, 3, 2].freeze
|
|
21
|
+
CPF2DIGIT = [11, 10, 9, 8, 7, 6, 5, 4, 3, 2].freeze
|
|
22
|
+
CNPJ1DIGIT = [5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2].freeze
|
|
23
|
+
CNPJ2DIGIT = [6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2].freeze
|
|
24
|
+
|
|
25
|
+
##
|
|
26
|
+
# Method for generating valid CPF numbers
|
|
27
|
+
# @param {Boolean} formatted
|
|
28
|
+
# @return String
|
|
29
|
+
def generate_cpf(formatted: true)
|
|
30
|
+
# Destructure the two arrays returned by the new generate_base
|
|
31
|
+
base_chars, base_values = generate_base(cnpj: false)
|
|
32
|
+
|
|
33
|
+
# Calculate the verification digits using base_values
|
|
34
|
+
first_digit = generate_identifier(base_values, true)
|
|
35
|
+
second_digit = generate_identifier(base_values + [first_digit], false)
|
|
36
|
+
|
|
37
|
+
# Combine the characters with the calculated digits
|
|
38
|
+
result = (base_chars + [first_digit, second_digit]).join
|
|
39
|
+
return result unless formatted
|
|
40
|
+
|
|
41
|
+
format(result)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
##
|
|
45
|
+
# Method for generating valid CNPJ numbers (Numeric)
|
|
46
|
+
# Standard numeric CNPJs remain 100% valid under the new rules.
|
|
47
|
+
# @param {Boolean} formatted
|
|
48
|
+
# @return String
|
|
49
|
+
def generate_cnpj(formatted: true, alphanumeric: false)
|
|
50
|
+
base_chars, base_values = generate_base(cnpj: true, alphanumeric: alphanumeric)
|
|
51
|
+
|
|
52
|
+
first_digit = generate_identifier(base_values, true, cpf: false)
|
|
53
|
+
second_digit = generate_identifier(base_values + [first_digit], false, cpf: false)
|
|
54
|
+
|
|
55
|
+
result = (base_chars + [first_digit, second_digit]).join
|
|
56
|
+
return result unless formatted
|
|
57
|
+
|
|
58
|
+
format(result)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
##
|
|
62
|
+
# Method for validating CPF numbers.
|
|
63
|
+
# @param {String, Integer} cpf
|
|
64
|
+
# @return bool
|
|
65
|
+
def cpf_valid?(cpf)
|
|
66
|
+
unformatted = remove_formatting(cpf.to_s)
|
|
67
|
+
return false if BLACKLIST_CPF.include?(unformatted)
|
|
68
|
+
|
|
69
|
+
cpf_array = remove_formatting(cpf.to_s).split("").map!(&:to_i)
|
|
70
|
+
return false if cpf_array.length != 11
|
|
71
|
+
|
|
72
|
+
first_digit = cpf_array[-2]
|
|
73
|
+
second_digit = cpf_array[-1]
|
|
74
|
+
base_cpf = cpf_array[0..8]
|
|
75
|
+
calculated_first_digit = generate_identifier(base_cpf, true)
|
|
76
|
+
calculated_second_digit = generate_identifier(base_cpf << calculated_first_digit, false)
|
|
77
|
+
|
|
78
|
+
return false if (first_digit != calculated_first_digit) || (second_digit != calculated_second_digit)
|
|
79
|
+
|
|
80
|
+
true
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
##
|
|
84
|
+
# Method for validating CNPJ numbers (Handles Alphanumeric).
|
|
85
|
+
# @param {Integer, String} cnpj
|
|
86
|
+
# @return bool
|
|
87
|
+
def cnpj_valid?(cnpj)
|
|
88
|
+
unformatted = remove_formatting(cnpj.to_s).upcase
|
|
89
|
+
|
|
90
|
+
return false unless unformatted.match?(REGEX_CNPJ_UNFORMATTED)
|
|
91
|
+
|
|
92
|
+
cnpj_array = unformatted.chars
|
|
93
|
+
first_digit = cnpj_array[-2].to_i
|
|
94
|
+
second_digit = cnpj_array[-1].to_i
|
|
95
|
+
|
|
96
|
+
base_cnpj_values = cnpj_array[0..11].map { |char| cnpj_char_to_value(char) }
|
|
97
|
+
|
|
98
|
+
calculated_first_digit = generate_identifier(base_cnpj_values, true, cpf: false)
|
|
99
|
+
calculated_second_digit = generate_identifier(base_cnpj_values + [calculated_first_digit], false, cpf: false)
|
|
100
|
+
|
|
101
|
+
return false if (first_digit != calculated_first_digit) || (second_digit != calculated_second_digit)
|
|
102
|
+
|
|
103
|
+
true
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
##
|
|
107
|
+
# Return true if the CPF/CNPJ is formatted.
|
|
108
|
+
# Return false if not.
|
|
109
|
+
# @param {String, Integer} cpf_or_cnpj
|
|
110
|
+
# @return bool
|
|
111
|
+
def formatted?(cpf_or_cnpj)
|
|
112
|
+
number = cpf_or_cnpj.to_s
|
|
113
|
+
return true if number =~ REGEX_CPF_FORMATTED
|
|
114
|
+
return true if number =~ REGEX_CNPJ_FORMATTED
|
|
115
|
+
|
|
116
|
+
false
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
##
|
|
120
|
+
# Returns an unformatted CPF or CNPJ.
|
|
121
|
+
# @param {String, Integer} cpf_or_cnpj
|
|
122
|
+
# @return String
|
|
123
|
+
def remove_formatting(cpf_or_cnpj)
|
|
124
|
+
unformatted = cpf_or_cnpj.to_s.delete("./-")
|
|
125
|
+
return unformatted unless unformatted.empty?
|
|
126
|
+
|
|
127
|
+
cpf_or_cnpj.to_s
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
##
|
|
131
|
+
# Returns a String containing a formatted CPF/CNPJ.
|
|
132
|
+
# @param {String, Integer} cpf_or_cnpj
|
|
133
|
+
# @return String
|
|
134
|
+
def format(cpf_or_cnpj)
|
|
135
|
+
if cpf_valid?(cpf_or_cnpj)
|
|
136
|
+
cpf = remove_formatting(cpf_or_cnpj).dup
|
|
137
|
+
cpf.insert(3, ".")
|
|
138
|
+
.insert(7, ".")
|
|
139
|
+
.insert(-3, "-")
|
|
140
|
+
elsif cnpj_valid?(cpf_or_cnpj)
|
|
141
|
+
cnpj = remove_formatting(cpf_or_cnpj).upcase.dup
|
|
142
|
+
cnpj.insert(2, ".")
|
|
143
|
+
.insert(6, ".")
|
|
144
|
+
.insert(10, "/")
|
|
145
|
+
.insert(-3, "-")
|
|
146
|
+
else
|
|
147
|
+
raise InvalidCpfCnpjFormatError
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
##
|
|
152
|
+
# Returns an array of valid random generated CPF numbers
|
|
153
|
+
# @param {Integer} number
|
|
154
|
+
# @param {bool} formatted
|
|
155
|
+
# @return {Array[String]}
|
|
156
|
+
def generate_array_of_cpf(number, formatted: true)
|
|
157
|
+
array = []
|
|
158
|
+
number.times do
|
|
159
|
+
array << generate_cpf(formatted: formatted)
|
|
160
|
+
end
|
|
161
|
+
array
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
##
|
|
165
|
+
# Returns an array of valid random generated CNPJ numbers
|
|
166
|
+
# @param {Integer} number
|
|
167
|
+
# @param {bool} formatted
|
|
168
|
+
# @return {Array[String]}
|
|
169
|
+
def generate_array_of_cnpj(number, formatted: true, alphanumeric: false)
|
|
170
|
+
array = []
|
|
171
|
+
number.times do
|
|
172
|
+
array << generate_cnpj(formatted: formatted, alphanumeric: alphanumeric)
|
|
173
|
+
end
|
|
174
|
+
array
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
private
|
|
178
|
+
|
|
179
|
+
##
|
|
180
|
+
# Converts a character to its mathematical value for CNPJ calculation.
|
|
181
|
+
# Numbers retain face value. Letters use ASCII value - 48.
|
|
182
|
+
# @param char (String)
|
|
183
|
+
# @return Integer
|
|
184
|
+
def cnpj_char_to_value(char)
|
|
185
|
+
char.match?(/[A-Z]/) ? char.ord - 48 : char.to_i
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
##
|
|
189
|
+
# Generate the first numbers of CPF/CNPJ
|
|
190
|
+
# randomly.
|
|
191
|
+
# @param cnpj (Boolean)
|
|
192
|
+
# @return (Array)
|
|
193
|
+
def generate_base(cnpj: false, alphanumeric: false)
|
|
194
|
+
pool = ("0".."9").to_a
|
|
195
|
+
pool += ("A".."Z").to_a if alphanumeric
|
|
196
|
+
|
|
197
|
+
base_chars = []
|
|
198
|
+
|
|
199
|
+
if cnpj
|
|
200
|
+
8.times { base_chars << pool.sample }
|
|
201
|
+
4.times { base_chars << pool.sample }
|
|
202
|
+
else
|
|
203
|
+
9.times { base_chars << rand(10).to_s }
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
base_values = base_chars.map do |char|
|
|
207
|
+
char.match?(/[A-Z]/) ? char.ord - 48 : char.to_i
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
[base_chars, base_values]
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
##
|
|
214
|
+
# Generate the first and second identifier numbers
|
|
215
|
+
# of the CPF/CNPJ.
|
|
216
|
+
# @param first_numbers (Array)
|
|
217
|
+
# @param first (Boolean)
|
|
218
|
+
# @param cpf (Boolean)
|
|
219
|
+
# @return Integer
|
|
220
|
+
def generate_identifier(first_numbers, first, cpf: true)
|
|
221
|
+
multipliers = if cpf
|
|
222
|
+
first ? CPF1DIGIT : CPF2DIGIT
|
|
223
|
+
else
|
|
224
|
+
first ? CNPJ1DIGIT : CNPJ2DIGIT
|
|
225
|
+
end
|
|
226
|
+
product = []
|
|
227
|
+
first_numbers.length.times do |index|
|
|
228
|
+
product << first_numbers[index] * multipliers[index]
|
|
229
|
+
end
|
|
230
|
+
generate_valid_digit(product)
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
##
|
|
234
|
+
# Calculates the value of a valid identifier digit.
|
|
235
|
+
# @param digits (Array)
|
|
236
|
+
# @return Integer
|
|
237
|
+
def generate_valid_digit(digits)
|
|
238
|
+
sum = 0
|
|
239
|
+
digits.each do |item|
|
|
240
|
+
sum += item
|
|
241
|
+
end
|
|
242
|
+
remainder = sum % 11
|
|
243
|
+
if remainder < 2
|
|
244
|
+
0
|
|
245
|
+
else
|
|
246
|
+
11 - remainder
|
|
247
|
+
end
|
|
248
|
+
end
|
|
249
|
+
end
|
|
250
|
+
end
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
##
|
|
4
|
-
# Error raised when trying to format
|
|
5
|
-
# an invalid CPF or CNPJ.
|
|
6
|
-
class InvalidCpfCnpjFormatError < StandardError
|
|
7
|
-
def initialize(msg = "Invalid CPF or CNPJ.")
|
|
8
|
-
super
|
|
9
|
-
end
|
|
10
|
-
end
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
##
|
|
4
|
+
# Error raised when trying to format
|
|
5
|
+
# an invalid CPF or CNPJ.
|
|
6
|
+
class InvalidCpfCnpjFormatError < StandardError
|
|
7
|
+
def initialize(msg = "Invalid CPF or CNPJ.")
|
|
8
|
+
super
|
|
9
|
+
end
|
|
10
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
module CpfCnpjTools
|
|
2
|
+
class Generator
|
|
3
|
+
CNPJ1DIGIT: Array[Integer]
|
|
4
|
+
CNPJ2DIGIT: Array[Integer]
|
|
5
|
+
CPF1DIGIT: Array[Integer]
|
|
6
|
+
CPF2DIGIT: Array[Integer]
|
|
7
|
+
|
|
8
|
+
def cnpj_valid?: -> bool
|
|
9
|
+
|
|
10
|
+
def cpf_valid?: -> bool
|
|
11
|
+
|
|
12
|
+
def formatted?: -> bool
|
|
13
|
+
|
|
14
|
+
def generate_array_of_cnpj: -> Array[String]
|
|
15
|
+
|
|
16
|
+
def generate_array_of_cpf: -> Array[String]
|
|
17
|
+
|
|
18
|
+
def generate_cnpj: -> String
|
|
19
|
+
|
|
20
|
+
def generate_cpf: -> String
|
|
21
|
+
|
|
22
|
+
def remove_formatting: -> String
|
|
23
|
+
end
|
|
24
|
+
end
|
metadata
CHANGED
|
@@ -1,57 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: cpf_cnpj_tools
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 1.0.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Aria Diniz
|
|
8
|
-
autorequire:
|
|
9
8
|
bindir: exe
|
|
10
9
|
cert_chain: []
|
|
11
|
-
date:
|
|
12
|
-
dependencies:
|
|
13
|
-
- !ruby/object:Gem::Dependency
|
|
14
|
-
name: rake
|
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
|
16
|
-
requirements:
|
|
17
|
-
- - "~>"
|
|
18
|
-
- !ruby/object:Gem::Version
|
|
19
|
-
version: '13.0'
|
|
20
|
-
type: :runtime
|
|
21
|
-
prerelease: false
|
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
-
requirements:
|
|
24
|
-
- - "~>"
|
|
25
|
-
- !ruby/object:Gem::Version
|
|
26
|
-
version: '13.0'
|
|
27
|
-
- !ruby/object:Gem::Dependency
|
|
28
|
-
name: minitest
|
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
|
30
|
-
requirements:
|
|
31
|
-
- - "~>"
|
|
32
|
-
- !ruby/object:Gem::Version
|
|
33
|
-
version: '5.0'
|
|
34
|
-
type: :runtime
|
|
35
|
-
prerelease: false
|
|
36
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
-
requirements:
|
|
38
|
-
- - "~>"
|
|
39
|
-
- !ruby/object:Gem::Version
|
|
40
|
-
version: '5.0'
|
|
41
|
-
- !ruby/object:Gem::Dependency
|
|
42
|
-
name: rubocop
|
|
43
|
-
requirement: !ruby/object:Gem::Requirement
|
|
44
|
-
requirements:
|
|
45
|
-
- - "~>"
|
|
46
|
-
- !ruby/object:Gem::Version
|
|
47
|
-
version: '1.21'
|
|
48
|
-
type: :runtime
|
|
49
|
-
prerelease: false
|
|
50
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
-
requirements:
|
|
52
|
-
- - "~>"
|
|
53
|
-
- !ruby/object:Gem::Version
|
|
54
|
-
version: '1.21'
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies: []
|
|
55
12
|
description: With this tool you will be able to generate and validate brazilian CPF
|
|
56
13
|
and CNPJ numbers.
|
|
57
14
|
email:
|
|
@@ -67,11 +24,11 @@ files:
|
|
|
67
24
|
- LICENSE.txt
|
|
68
25
|
- README.md
|
|
69
26
|
- Rakefile
|
|
70
|
-
- cpf_cnpj_tools.gemspec
|
|
71
27
|
- lib/cpf_cnpj_tools.rb
|
|
72
28
|
- lib/cpf_cnpj_tools/version.rb
|
|
73
29
|
- lib/invalid_cpf_cnpj_format_exception.rb
|
|
74
30
|
- sig/cpf_cnpj_tools.rbs
|
|
31
|
+
- sig/cpf_cnpj_tools/generator.rbs
|
|
75
32
|
homepage: https://github.com/ariasdiniz/cpf_cnpj_tools
|
|
76
33
|
licenses:
|
|
77
34
|
- MIT
|
|
@@ -79,7 +36,6 @@ metadata:
|
|
|
79
36
|
documentation_uri: https://rubydoc.info/gems/cpf_cnpj_tools
|
|
80
37
|
homepage_uri: https://github.com/ariasdiniz/cpf_cnpj_tools
|
|
81
38
|
source_code_uri: https://github.com/ariasdiniz/cpf_cnpj_tools
|
|
82
|
-
post_install_message:
|
|
83
39
|
rdoc_options: []
|
|
84
40
|
require_paths:
|
|
85
41
|
- lib
|
|
@@ -94,8 +50,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
94
50
|
- !ruby/object:Gem::Version
|
|
95
51
|
version: '0'
|
|
96
52
|
requirements: []
|
|
97
|
-
rubygems_version:
|
|
98
|
-
signing_key:
|
|
53
|
+
rubygems_version: 4.0.10
|
|
99
54
|
specification_version: 4
|
|
100
55
|
summary: A tool for generating and validating CPF/CNPJ numbers
|
|
101
56
|
test_files: []
|
data/cpf_cnpj_tools.gemspec
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require_relative "lib/cpf_cnpj_tools/version"
|
|
4
|
-
|
|
5
|
-
Gem::Specification.new do |spec|
|
|
6
|
-
spec.name = "cpf_cnpj_tools"
|
|
7
|
-
spec.version = CpfCnpjTools::VERSION
|
|
8
|
-
spec.authors = ["Aria Diniz"]
|
|
9
|
-
spec.email = ["aria.diniz.dev@gmail.com"]
|
|
10
|
-
|
|
11
|
-
spec.summary = "A tool for generating and validating CPF/CNPJ numbers"
|
|
12
|
-
spec.description = "With this tool you will be able to generate and validate brazilian CPF and CNPJ numbers."
|
|
13
|
-
spec.homepage = "https://github.com/ariasdiniz/cpf_cnpj_tools"
|
|
14
|
-
spec.license = "MIT"
|
|
15
|
-
spec.required_ruby_version = ">= 2.6"
|
|
16
|
-
|
|
17
|
-
spec.metadata["documentation_uri"] = "https://rubydoc.info/gems/cpf_cnpj_tools"
|
|
18
|
-
spec.metadata["homepage_uri"] = spec.homepage
|
|
19
|
-
spec.metadata["source_code_uri"] = "https://github.com/ariasdiniz/cpf_cnpj_tools"
|
|
20
|
-
|
|
21
|
-
# Specify which files should be added to the gem when it is released.
|
|
22
|
-
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
|
23
|
-
spec.files = Dir.chdir(__dir__) do
|
|
24
|
-
`git ls-files -z`.split("\x0").reject do |f|
|
|
25
|
-
(f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
|
|
26
|
-
end
|
|
27
|
-
end
|
|
28
|
-
spec.bindir = "exe"
|
|
29
|
-
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
|
30
|
-
spec.require_paths = ["lib"]
|
|
31
|
-
|
|
32
|
-
# Uncomment to register a new dependency of your gem
|
|
33
|
-
spec.add_dependency "rake", "~> 13.0"
|
|
34
|
-
spec.add_dependency "minitest", "~> 5.0"
|
|
35
|
-
spec.add_dependency "rubocop", "~> 1.21"
|
|
36
|
-
|
|
37
|
-
# For more information and examples about making a new gem, check out our
|
|
38
|
-
# guide at: https://bundler.io/guides/creating_gem.html
|
|
39
|
-
end
|