bra_documents 1.0.2 → 1.1.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 +4 -4
- data/CHANGELOG.md +28 -4
- data/README.md +52 -6
- data/lib/bra_documents/cnpj_generator.rb +15 -5
- data/lib/bra_documents/cpf_generator.rb +6 -2
- data/lib/bra_documents/formatter.rb +5 -5
- data/lib/bra_documents/matcher.rb +49 -6
- data/lib/bra_documents/national_register_base.rb +12 -16
- data/lib/bra_documents/testing/rspec/matchers/cnpj_matcher.rb +6 -14
- data/lib/bra_documents/testing/rspec/matchers/cpf_matcher.rb +5 -13
- data/lib/bra_documents/version.rb +1 -1
- metadata +8 -77
- data/.github/dependabot.yml +0 -8
- data/.gitignore +0 -11
- data/.rspec +0 -3
- data/.travis.yml +0 -12
- data/Gemfile +0 -6
- data/Gemfile.lock +0 -43
- data/LICENSE +0 -674
- data/Rakefile +0 -8
- data/bin/console +0 -14
- data/bin/setup +0 -8
- data/bra_documents.gemspec +0 -34
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 9abb59173a8af173e52415a22f9f0931c2d02fbe1bf710b7a4039fb7e7a896c6
|
|
4
|
+
data.tar.gz: d1cb2ec1282cb184b55f05c99bdb311a118ffbe7f53c649b676bdc0d44e00fe6
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 6d374d7c23a9d5e509dc8fe2510e14c5014a9078b9417c4928a74888c44f62d2ffabf9827f9458c5c2af2b83e292e86dd956b838d4aec11f7edc82e2f8ddd402
|
|
7
|
+
data.tar.gz: bc2a922081d085404263c3fd91a1072ce1fc8098a5efaddcab08d2b1e092ce369160928a561d9552f37e6da946e24422b01361d5ba781e9728b86f014aa44d15
|
data/CHANGELOG.md
CHANGED
|
@@ -1,12 +1,36 @@
|
|
|
1
|
-
|
|
2
1
|
# Changelog
|
|
3
2
|
|
|
4
3
|
All notable changes to this project will be documented in this file.
|
|
5
4
|
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
This project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
|
|
6
|
+
|
|
7
|
+
## [1.1.1](https://github.com/bvicenzo/bra_documents/compare/v1.1.0...v1.1.1) (2026-12-01)
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
### Bug Fixes
|
|
11
|
+
|
|
12
|
+
* reject formatted CPFs with all digits equal ([#38](https://github.com/bvicenzo/bra_documents/pull/38)) ([400c1c7](https://github.com/bvicenzo/bra_documents/commit/400c1c77d0a40d300e6e8d8eb0bbd08fe9127036)), closes [#36](https://github.com/bvicenzo/bra_documents/issues/36)
|
|
13
|
+
|
|
14
|
+
## [1.1.0] - 2026-01-14
|
|
15
|
+
|
|
16
|
+
### Added
|
|
17
|
+
|
|
18
|
+
- Alphanumeric CNPJ support: company number and branch/subsidiary number now accept letters (A-Z) and numbers (0-9);
|
|
19
|
+
- Enhanced CNPJ generation with alphanumeric character support;
|
|
20
|
+
- Updated CNPJ formatter to handle alphanumeric characters (outputs uppercase);
|
|
21
|
+
- Enhanced CNPJ matcher with alphanumeric pattern validation;
|
|
22
|
+
- RSpec matchers (`be_a_formatted_cnpj`, `be_a_raw_cnpj`, `a_formatted_cnpj`, `a_raw_cnpj`) now support alphanumeric CNPJ validation;
|
|
23
|
+
|
|
24
|
+
### Changed
|
|
25
|
+
|
|
26
|
+
- CNPJ verification digit calculation now supports alphanumeric characters in company and branch numbers;
|
|
27
|
+
- `Formatter.raw` method now accepts `kind:` parameter to properly handle CNPJ alphanumeric characters;
|
|
28
|
+
- Matcher patterns now reject documents where all characters are identical (e.g., '11111111111' for CPF, 'AAAAAAAAAAAAAA' for CNPJ);
|
|
29
|
+
|
|
30
|
+
### Fixed
|
|
8
31
|
|
|
9
|
-
|
|
32
|
+
- Ruby 4.0 compatibility: removed `__FILE__` argument from `IRB.start` in bin/console;
|
|
33
|
+
- Added `irb` gem as development dependency for Ruby 4.0+;
|
|
10
34
|
|
|
11
35
|
## [1.0.2] - 2020-22-10
|
|
12
36
|
|
data/README.md
CHANGED
|
@@ -1,17 +1,19 @@
|
|
|
1
1
|
# BraDocuments
|
|
2
2
|
|
|
3
|
-
[](https://github.com/bvicenzo/bra_documents/actions/workflows/main.yml)
|
|
4
4
|
|
|
5
5
|
This gem make us able to generate Brazilian documents, such as CPF and CNPJ.
|
|
6
6
|
We can generate a tottaly random number, or pass the number and the gem completes with the verification digits.
|
|
7
7
|
If you already have a CPF or CNPJ only their numbers, you can also put the mask using the formatter.
|
|
8
8
|
|
|
9
|
+
**Note:** Starting with recent regulations, CNPJ numbers now support alphanumeric characters (letters A-Z and numbers 0-9) in the company number and branch/subsidiary number portions, while the verification digits remain numeric only.
|
|
10
|
+
|
|
9
11
|
## Installation
|
|
10
12
|
|
|
11
13
|
Add this line to your application's Gemfile:
|
|
12
14
|
|
|
13
15
|
```ruby
|
|
14
|
-
gem 'bra_documents'
|
|
16
|
+
gem 'bra_documents', '~> 1.1'
|
|
15
17
|
```
|
|
16
18
|
|
|
17
19
|
And then execute:
|
|
@@ -55,6 +57,8 @@ BraDocuments::CPFGenerator.valid_verification_digit?(document: '123.123.123-88')
|
|
|
55
57
|
|
|
56
58
|
### CNPJ Generation
|
|
57
59
|
|
|
60
|
+
**Note:** CNPJ now supports alphanumeric characters (A-Z, 0-9) in company and branch numbers.
|
|
61
|
+
|
|
58
62
|
```rb
|
|
59
63
|
BraDocuments::CNPJGenerator.generate
|
|
60
64
|
#=> "62885807804809"
|
|
@@ -73,10 +77,19 @@ BraDocuments::CNPJGenerator.generate(company_number: '53855973', matrix_subsidia
|
|
|
73
77
|
|
|
74
78
|
BraDocuments::CNPJGenerator.generate(company_number: '53855973', matrix_subsidiary_number: '0001', formatted: true)
|
|
75
79
|
#=> "53.855.973/0001-79"
|
|
80
|
+
|
|
81
|
+
# Alphanumeric CNPJ examples
|
|
82
|
+
BraDocuments::CNPJGenerator.generate(company_number: 'AB12CD34')
|
|
83
|
+
#=> "AB12CD34EUJW83"
|
|
84
|
+
|
|
85
|
+
BraDocuments::CNPJGenerator.generate(company_number: 'AB12CD34', matrix_subsidiary_number: '0A01', formatted: true)
|
|
86
|
+
#=> "AB.12C.D34/0A01-60"
|
|
76
87
|
```
|
|
77
88
|
|
|
78
89
|
### CNPJ digit verification
|
|
79
90
|
|
|
91
|
+
**Note:** Validation now supports alphanumeric CNPJ numbers.
|
|
92
|
+
|
|
80
93
|
```rb
|
|
81
94
|
BraDocuments::CNPJGenerator.valid_verification_digit?(document: '62885807804809')
|
|
82
95
|
#=> true
|
|
@@ -86,6 +99,13 @@ BraDocuments::CNPJGenerator.valid_verification_digit?(document: '53.855.973/0001
|
|
|
86
99
|
|
|
87
100
|
BraDocuments::CNPJGenerator.valid_verification_digit?(document: '53855973000177')
|
|
88
101
|
#=> false
|
|
102
|
+
|
|
103
|
+
# Alphanumeric CNPJ validation
|
|
104
|
+
BraDocuments::CNPJGenerator.valid_verification_digit?(document: 'AB12CD340A0160')
|
|
105
|
+
#=> true (if valid digits)
|
|
106
|
+
|
|
107
|
+
BraDocuments::CNPJGenerator.valid_verification_digit?(document: 'AB.12C.D34/0A01-60')
|
|
108
|
+
#=> true (if valid digits)
|
|
89
109
|
```
|
|
90
110
|
|
|
91
111
|
### Formatting
|
|
@@ -99,22 +119,42 @@ BraDocuments::Formatter.format('53855973879456', as: :cnpj)
|
|
|
99
119
|
|
|
100
120
|
BraDocuments::Formatter.raw('53.855.973/8794-56')
|
|
101
121
|
#=> "53855973879456"
|
|
122
|
+
|
|
123
|
+
# Alphanumeric CNPJ formatting
|
|
124
|
+
BraDocuments::Formatter.format('AB12CD3400A179', as: :cnpj)
|
|
125
|
+
#=> "AB.12C.D34/00A1-79"
|
|
126
|
+
|
|
127
|
+
BraDocuments::Formatter.raw('AB.12C.D34/00A1-79', kind: :cnpj)
|
|
128
|
+
#=> "AB12CD3400A179"
|
|
102
129
|
```
|
|
103
130
|
|
|
104
131
|
### Matching
|
|
105
132
|
|
|
106
133
|
```rb
|
|
107
134
|
BraDocuments::Matcher.match?('11111111111', kind: :cpf, mode: :raw)
|
|
135
|
+
#=> false (rejected - all same digits)
|
|
136
|
+
|
|
137
|
+
BraDocuments::Matcher.match?('12345678901', kind: :cpf, mode: :raw)
|
|
108
138
|
#=> true
|
|
109
139
|
|
|
110
140
|
BraDocuments::Matcher.match?('11111111111', kind: :cpf, mode: :formatted)
|
|
111
141
|
#=> false
|
|
112
142
|
|
|
113
|
-
BraDocuments::Matcher.match?('
|
|
143
|
+
BraDocuments::Matcher.match?('11111111111111', kind: :cnpj, mode: :raw)
|
|
114
144
|
#=> false
|
|
115
145
|
|
|
116
146
|
BraDocuments::Matcher.match?('90.978.812/0001-07', kind: :cnpj, mode: :formatted)
|
|
117
147
|
#=> true
|
|
148
|
+
|
|
149
|
+
# Alphanumeric CNPJ matching
|
|
150
|
+
BraDocuments::Matcher.match?('AB12CD3400A179', kind: :cnpj, mode: :raw)
|
|
151
|
+
#=> true
|
|
152
|
+
|
|
153
|
+
BraDocuments::Matcher.match?('AB.12C.D34/00A1-79', kind: :cnpj, mode: :formatted)
|
|
154
|
+
#=> true
|
|
155
|
+
|
|
156
|
+
BraDocuments::Matcher.match?('AAAAAAAAAAAAAA', kind: :cnpj, mode: :raw)
|
|
157
|
+
#=> false (rejected - all same characters)
|
|
118
158
|
```
|
|
119
159
|
|
|
120
160
|
### Tests Matching
|
|
@@ -186,13 +226,19 @@ end
|
|
|
186
226
|
|
|
187
227
|
## Development
|
|
188
228
|
|
|
189
|
-
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake
|
|
229
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bundle exec rake` to run the tests and RuboCop. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
230
|
+
|
|
231
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
|
190
232
|
|
|
191
|
-
|
|
233
|
+
Releases are automated: the version, the `CHANGELOG.md`, the git tag and the push
|
|
234
|
+
to [rubygems.org](https://rubygems.org) are all derived from
|
|
235
|
+
[Conventional Commits](https://www.conventionalcommits.org/) by release-please.
|
|
236
|
+
There is no version to edit and no release command to run by hand — see
|
|
237
|
+
[CONTRIBUTING.md](CONTRIBUTING.md).
|
|
192
238
|
|
|
193
239
|
## Contributing
|
|
194
240
|
|
|
195
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
|
241
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/bvicenzo/bra_documents. Read [CONTRIBUTING.md](CONTRIBUTING.md) first — it covers the commit conventions the release automation depends on.
|
|
196
242
|
|
|
197
243
|
|
|
198
244
|
## License
|
|
@@ -7,6 +7,7 @@ module BraDocuments
|
|
|
7
7
|
class CNPJGenerator < NationalRegisterBase
|
|
8
8
|
COMPANY_NUMBER_SIZE = 8
|
|
9
9
|
MATRIX_SUBSIDIARY_SIZE = 4
|
|
10
|
+
ALPHANUM_UPPER = [*'A'..'Z', *'0'..'9'].freeze
|
|
10
11
|
|
|
11
12
|
class << self
|
|
12
13
|
# Generates a random CNPJ document number or add verifying digits to one if it's given.
|
|
@@ -34,13 +35,18 @@ module BraDocuments
|
|
|
34
35
|
# )
|
|
35
36
|
# # => "53.855.973/0001-79"
|
|
36
37
|
def generate(company_number: nil, matrix_subsidiary_number: nil, formatted: false)
|
|
37
|
-
company_number = number_for('Company', COMPANY_NUMBER_SIZE, company_number)
|
|
38
|
-
matrix_subsidiary_number = number_for(
|
|
38
|
+
company_number = number_for('Company', COMPANY_NUMBER_SIZE, company_number, kind: :cnpj)
|
|
39
|
+
matrix_subsidiary_number = number_for(
|
|
40
|
+
'Matrix or subsidiary',
|
|
41
|
+
MATRIX_SUBSIDIARY_SIZE,
|
|
42
|
+
matrix_subsidiary_number,
|
|
43
|
+
kind: :cnpj
|
|
44
|
+
)
|
|
39
45
|
numbers = company_number + matrix_subsidiary_number
|
|
40
46
|
|
|
41
47
|
full_number = complete!(numbers)
|
|
42
48
|
|
|
43
|
-
formatted ?
|
|
49
|
+
formatted ? Formatter.format(full_number, as: :cnpj) : full_number
|
|
44
50
|
end
|
|
45
51
|
|
|
46
52
|
# Returns if a CPF has a valid verification digit.
|
|
@@ -54,8 +60,8 @@ module BraDocuments
|
|
|
54
60
|
# BraDocuments::CPFGenerator.valid_verification_digit?(document: '29432530000190')
|
|
55
61
|
# # => true
|
|
56
62
|
def valid_verification_digit?(document:)
|
|
57
|
-
raw_document = Formatter.raw(document)
|
|
58
|
-
return false
|
|
63
|
+
raw_document = Formatter.raw(document, kind: :cnpj)
|
|
64
|
+
return false unless Matcher.match?(raw_document, kind: :cnpj, mode: :raw)
|
|
59
65
|
|
|
60
66
|
company_number = raw_document.slice(0..(COMPANY_NUMBER_SIZE - 1))
|
|
61
67
|
matrix_subsidiary_number = raw_document
|
|
@@ -68,6 +74,10 @@ module BraDocuments
|
|
|
68
74
|
|
|
69
75
|
private
|
|
70
76
|
|
|
77
|
+
def number_with(size)
|
|
78
|
+
Array.new(size) { ALPHANUM_UPPER.sample }
|
|
79
|
+
end
|
|
80
|
+
|
|
71
81
|
def verification_digit_multiplicators_for(numbers)
|
|
72
82
|
(2..(numbers.size - 7)).to_a.reverse + (2..9).to_a.reverse
|
|
73
83
|
end
|
|
@@ -23,7 +23,7 @@ module BraDocuments
|
|
|
23
23
|
# BraDocuments::CPFGenerator.generate(person_number: '123123123', formatted: true)
|
|
24
24
|
# # => "123.123.123-87"
|
|
25
25
|
def generate(person_number: nil, formatted: false)
|
|
26
|
-
numbers = number_for('Person', PERSON_NUMBER_SIZE, person_number)
|
|
26
|
+
numbers = number_for('Person', PERSON_NUMBER_SIZE, person_number, kind: :cpf)
|
|
27
27
|
full_number = complete!(numbers)
|
|
28
28
|
|
|
29
29
|
formatted ? Formatter.format(full_number, as: :cpf) : full_number
|
|
@@ -41,7 +41,7 @@ module BraDocuments
|
|
|
41
41
|
# # => true
|
|
42
42
|
def valid_verification_digit?(document:)
|
|
43
43
|
raw_document = Formatter.raw(document)
|
|
44
|
-
return false
|
|
44
|
+
return false unless Matcher.match?(raw_document, kind: :cpf, mode: :raw)
|
|
45
45
|
|
|
46
46
|
person_number = raw_document.slice(0..(PERSON_NUMBER_SIZE - 1))
|
|
47
47
|
verified_digit = raw_document.slice(PERSON_NUMBER_SIZE..(raw_document.size - 1))
|
|
@@ -51,6 +51,10 @@ module BraDocuments
|
|
|
51
51
|
|
|
52
52
|
private
|
|
53
53
|
|
|
54
|
+
def number_with(size)
|
|
55
|
+
Array.new(size) { rand(10) }
|
|
56
|
+
end
|
|
57
|
+
|
|
54
58
|
def verification_digit_multiplicators_for(numbers)
|
|
55
59
|
(2..numbers.size.next).to_a.reverse
|
|
56
60
|
end
|
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
module BraDocuments
|
|
4
4
|
class Formatter
|
|
5
|
-
|
|
5
|
+
RAW_TRANSFORM_PATTERNS = { cpf: /[^[:digit:]]/, cnpj: /[^[:alnum:]]/ }.freeze
|
|
6
6
|
FORMATS = {
|
|
7
7
|
cpf: { pattern: /\A(\d{3})(\d{3})(\d{3})(\d{2})\z/, mask: '%s.%s.%s-%s' },
|
|
8
|
-
cnpj: { pattern: /\A(
|
|
8
|
+
cnpj: { pattern: /\A([A-Z0-9]{2})([A-Z0-9]{3})([A-Z0-9]{3})([A-Z0-9]{4})(\d{2})\z/i, mask: '%s.%s.%s/%s-%s' }
|
|
9
9
|
}.freeze
|
|
10
10
|
|
|
11
11
|
class << self
|
|
@@ -21,17 +21,17 @@ module BraDocuments
|
|
|
21
21
|
|
|
22
22
|
format_data = FORMATS[as]
|
|
23
23
|
|
|
24
|
-
Kernel.format(format_data[:mask], *format_data[:pattern].match(number).captures)
|
|
24
|
+
Kernel.format(format_data[:mask], *format_data[:pattern].match(number).captures).upcase
|
|
25
25
|
end
|
|
26
26
|
|
|
27
27
|
# Formats removing all not number caracters from string.
|
|
28
28
|
#
|
|
29
29
|
# BraDocuments::Formatter.raw('860.272.658-9') # => "286027265892"
|
|
30
30
|
# BraDocuments::Formatter.format('53.855.973/8794-56') # => "53855973879456"
|
|
31
|
-
def raw(number)
|
|
31
|
+
def raw(number, kind: :cpf)
|
|
32
32
|
raise ArgumentError, "\"#{number.inspect}\" must be a String." unless number.is_a?(String)
|
|
33
33
|
|
|
34
|
-
number.gsub(
|
|
34
|
+
number.gsub(RAW_TRANSFORM_PATTERNS[kind], '')
|
|
35
35
|
end
|
|
36
36
|
|
|
37
37
|
private
|
|
@@ -3,33 +3,76 @@
|
|
|
3
3
|
module BraDocuments
|
|
4
4
|
class Matcher
|
|
5
5
|
FORMATS = {
|
|
6
|
-
cpf: {
|
|
7
|
-
|
|
6
|
+
cpf: {
|
|
7
|
+
raw: /\A(?!([0-9])\1{10})\d{11}\z/,
|
|
8
|
+
formatted: /\A(?!([0-9])(?:[.-]?\1){10}\z)(\d{3}\.){2}\d{3}-\d{2}\z/
|
|
9
|
+
},
|
|
10
|
+
cnpj: {
|
|
11
|
+
raw: /\A(?!([A-Z0-9])\1{11}\d{2})[A-Z0-9]{12}\d{2}\z/i,
|
|
12
|
+
formatted: %r{
|
|
13
|
+
\A
|
|
14
|
+
(?! # disallow all characters being the same
|
|
15
|
+
([A-Z0-9])
|
|
16
|
+
(?: [.\-/]? \1 ){11}
|
|
17
|
+
[.\-/]? \d{2}
|
|
18
|
+
)
|
|
19
|
+
[A-Z0-9]{2} \.
|
|
20
|
+
[A-Z0-9]{3} \.
|
|
21
|
+
[A-Z0-9]{3} /
|
|
22
|
+
[A-Z0-9]{4} -
|
|
23
|
+
\d{2}
|
|
24
|
+
\z
|
|
25
|
+
}ix
|
|
26
|
+
}
|
|
8
27
|
}.freeze
|
|
9
28
|
|
|
10
29
|
class << self
|
|
11
30
|
# Macthes with Brazilian CPF and CNPJ documents.
|
|
12
31
|
#
|
|
13
32
|
# BraDocuments::Matcher.match?('11111111111', kind: :cpf, mode: :raw)
|
|
33
|
+
# # => false
|
|
34
|
+
#
|
|
35
|
+
# BraDocuments::Matcher.match?('12345678888', kind: :cpf, mode: :raw)
|
|
14
36
|
# # => true
|
|
15
37
|
#
|
|
16
|
-
# BraDocuments::Matcher.match?('
|
|
38
|
+
# BraDocuments::Matcher.match?('111.111.111-11', kind: :cpf, mode: :formatted)
|
|
17
39
|
# # => false
|
|
18
40
|
#
|
|
19
|
-
# BraDocuments::Matcher.match?('
|
|
41
|
+
# BraDocuments::Matcher.match?('11111111111111', kind: :cnpj, mode: :raw)
|
|
20
42
|
# # => false
|
|
21
43
|
#
|
|
44
|
+
# BraDocuments::Matcher.match?('11111B11111111', kind: :cnpj, mode: :raw)
|
|
45
|
+
# # => true
|
|
46
|
+
#
|
|
47
|
+
# BraDocuments::Matcher.match?('11111a11111111', kind: :cnpj, mode: :raw)
|
|
48
|
+
# # => true
|
|
49
|
+
#
|
|
22
50
|
# BraDocuments::Matcher.match?('90.978.812/0001-07', kind: :cnpj, mode: :formatted)
|
|
23
51
|
# # => true
|
|
52
|
+
#
|
|
53
|
+
# BraDocuments::Matcher.match?('90.97A.812/0001-07', kind: :cnpj, mode: :formatted)
|
|
54
|
+
# # => true
|
|
55
|
+
#
|
|
56
|
+
# BraDocuments::Matcher.match?('90.97b.812/0001-07', kind: :cnpj, mode: :formatted)
|
|
57
|
+
# # => true
|
|
58
|
+
#
|
|
59
|
+
# BraDocuments::Matcher.match?('dd.ccc.bbb/AAAA-00', kind: :cnpj, mode: :formatted)
|
|
60
|
+
# # => true
|
|
24
61
|
def match?(number, kind:, mode:)
|
|
25
62
|
raise ArgumentError, "\"#{number.inspect}\" must be a String." unless number.is_a?(String)
|
|
26
63
|
|
|
27
64
|
unless known_format?(kind)
|
|
28
|
-
raise
|
|
65
|
+
raise(
|
|
66
|
+
ArgumentError,
|
|
67
|
+
"Unknown document kind \"#{kind.inspect}\". Known documents: #{known_formats.join(', ')}."
|
|
68
|
+
)
|
|
29
69
|
end
|
|
30
70
|
|
|
31
71
|
unless known_mode?(mode)
|
|
32
|
-
raise
|
|
72
|
+
raise(
|
|
73
|
+
ArgumentError,
|
|
74
|
+
"Unknown document format mode \"#{mode.inspect}\". Known modes: #{known_modes.join(', ')}."
|
|
75
|
+
)
|
|
33
76
|
end
|
|
34
77
|
|
|
35
78
|
formats_to_match(kind, mode).any? { |format| format.match?(number) }
|
|
@@ -4,6 +4,7 @@ module BraDocuments
|
|
|
4
4
|
class NationalRegisterBase
|
|
5
5
|
class << self
|
|
6
6
|
BASE = 11
|
|
7
|
+
ZERO_ON_ASCII_CODE = 48
|
|
7
8
|
|
|
8
9
|
private
|
|
9
10
|
|
|
@@ -12,29 +13,28 @@ module BraDocuments
|
|
|
12
13
|
numbers.join
|
|
13
14
|
end
|
|
14
15
|
|
|
15
|
-
def number_for(number_description, number_size, given_value)
|
|
16
|
-
given_value = Formatter.raw(given_value.to_s)
|
|
17
|
-
if
|
|
16
|
+
def number_for(number_description, number_size, given_value, kind:)
|
|
17
|
+
given_value = Formatter.raw(given_value.to_s, kind:)
|
|
18
|
+
if given_value.to_s.empty?
|
|
19
|
+
number_with(number_size)
|
|
20
|
+
else
|
|
18
21
|
unless given_value.size == number_size
|
|
19
22
|
raise ArgumentError, "#{number_description} number must be a number with #{number_size} digits."
|
|
20
23
|
end
|
|
21
24
|
|
|
22
|
-
given_value.
|
|
23
|
-
else
|
|
24
|
-
number_with(number_size)
|
|
25
|
+
given_value.chars
|
|
25
26
|
end
|
|
26
27
|
end
|
|
27
28
|
|
|
28
|
-
def number_with(size)
|
|
29
|
-
size.times.map { rand(10) }
|
|
30
|
-
end
|
|
31
|
-
|
|
32
29
|
def verification_digit_for(numbers)
|
|
33
|
-
|
|
34
|
-
|
|
30
|
+
numbers_to_calculate = calculable_numbers_for(numbers:)
|
|
31
|
+
verification_digit_multiplicators = verification_digit_multiplicators_for(numbers_to_calculate)
|
|
32
|
+
sum_and_multiplication = sum_and_multiply(numbers_to_calculate, verification_digit_multiplicators)
|
|
35
33
|
verified_digit(sum_and_multiplication)
|
|
36
34
|
end
|
|
37
35
|
|
|
36
|
+
def calculable_numbers_for(numbers:) = numbers.map { |number| number.to_s.upcase.ord - ZERO_ON_ASCII_CODE }
|
|
37
|
+
|
|
38
38
|
def verified_digit(sum_and_multiplication)
|
|
39
39
|
rest = sum_and_multiplication % BASE
|
|
40
40
|
rest < 2 ? 0 : BASE - rest
|
|
@@ -46,10 +46,6 @@ module BraDocuments
|
|
|
46
46
|
.with_index { |multiplicator, position| numbers[position] * multiplicator }
|
|
47
47
|
.sum
|
|
48
48
|
end
|
|
49
|
-
|
|
50
|
-
def black_listed?(numbers)
|
|
51
|
-
numbers.chars.uniq.size == 1
|
|
52
|
-
end
|
|
53
49
|
end
|
|
54
50
|
end
|
|
55
51
|
end
|
|
@@ -3,28 +3,20 @@
|
|
|
3
3
|
require 'rspec/expectations'
|
|
4
4
|
|
|
5
5
|
RSpec::Matchers.define :a_formatted_cnpj do
|
|
6
|
-
match
|
|
7
|
-
formatted_cnpj_pattern = /\A(\d{2}\.\d{3}\.\d{3}\/\d{4})-(\d{2})\z/
|
|
8
|
-
|
|
9
|
-
formatted_cnpj_pattern.match?(cnpj.to_s)
|
|
10
|
-
end
|
|
6
|
+
match { |cnpj| BraDocuments::Matcher.match?(cnpj.to_s, kind: :cnpj, mode: :formatted) }
|
|
11
7
|
|
|
12
8
|
failure_message do |cnpj|
|
|
13
|
-
"Was expected `#{cnpj.inspect}` to be a Brazilian CNPJ document number but it isn't.\n"\
|
|
14
|
-
"A CNPJ has the following format
|
|
9
|
+
"Was expected `#{cnpj.inspect}` to be a Brazilian CNPJ document number but it isn't.\n" \
|
|
10
|
+
"A CNPJ has the following format AA.AAA.AAA/AAAA-99 where X are numbers from 0 to 9.\n"
|
|
15
11
|
end
|
|
16
12
|
end
|
|
17
13
|
|
|
18
14
|
RSpec::Matchers.define :a_raw_cnpj do
|
|
19
|
-
match
|
|
20
|
-
raw_cnpj_pattern = /\A\d{14}\z/
|
|
21
|
-
|
|
22
|
-
raw_cnpj_pattern.match?(cnpj.to_s)
|
|
23
|
-
end
|
|
15
|
+
match { |cnpj| BraDocuments::Matcher.match?(cnpj.to_s, kind: :cnpj, mode: :raw) }
|
|
24
16
|
|
|
25
17
|
failure_message do |cnpj|
|
|
26
|
-
"Was expected `#{cnpj.inspect}` to be a raw Brazilian cnpj document number but it isn't.\n"\
|
|
27
|
-
"A raw CNPJ has the following format
|
|
18
|
+
"Was expected `#{cnpj.inspect}` to be a raw Brazilian cnpj document number but it isn't.\n" \
|
|
19
|
+
"A raw CNPJ has the following format AAAAAAAAAAAAAA where X are numbers from 0 to 9.\n"
|
|
28
20
|
end
|
|
29
21
|
end
|
|
30
22
|
|
|
@@ -3,27 +3,19 @@
|
|
|
3
3
|
require 'rspec/expectations'
|
|
4
4
|
|
|
5
5
|
RSpec::Matchers.define :a_formatted_cpf do
|
|
6
|
-
match
|
|
7
|
-
formatted_cpf_pattern = /\A(\d{3}\.\d{3}\.\d{3})-(\d{2})\z/
|
|
8
|
-
|
|
9
|
-
formatted_cpf_pattern.match?(cpf.to_s)
|
|
10
|
-
end
|
|
6
|
+
match { |cpf| BraDocuments::Matcher.match?(cpf.to_s, kind: :cpf, mode: :formatted) }
|
|
11
7
|
|
|
12
8
|
failure_message do |cpf|
|
|
13
|
-
"Was expected `#{cpf.inspect}` to be a Brazilian CPF document number but it isn't.\n"\
|
|
14
|
-
"A CPF has the following format
|
|
9
|
+
"Was expected `#{cpf.inspect}` to be a Brazilian CPF document number but it isn't.\n" \
|
|
10
|
+
"A CPF has the following format 999.999.999-99 where X are numbers from 0 to 9.\n"
|
|
15
11
|
end
|
|
16
12
|
end
|
|
17
13
|
|
|
18
14
|
RSpec::Matchers.define :a_raw_cpf do
|
|
19
|
-
match
|
|
20
|
-
raw_cpf_pattern = /\A\d{11}\z/
|
|
21
|
-
|
|
22
|
-
raw_cpf_pattern.match?(cpf.to_s)
|
|
23
|
-
end
|
|
15
|
+
match { |cpf| BraDocuments::Matcher.match?(cpf.to_s, kind: :cpf, mode: :raw) }
|
|
24
16
|
|
|
25
17
|
failure_message do |cpf|
|
|
26
|
-
"Was expected `#{cpf.inspect}` to be a raw Brazilian CPF document number but it isn't.\n"\
|
|
18
|
+
"Was expected `#{cpf.inspect}` to be a raw Brazilian CPF document number but it isn't.\n" \
|
|
27
19
|
"A raw CPF has the following format XXXXXXXXXXX where X are numbers from 0 to 9.\n"
|
|
28
20
|
end
|
|
29
21
|
end
|
metadata
CHANGED
|
@@ -1,71 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: bra_documents
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 1.1.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Bruno Vicenzo
|
|
8
|
-
|
|
9
|
-
bindir: exe
|
|
8
|
+
bindir: bin
|
|
10
9
|
cert_chain: []
|
|
11
|
-
date:
|
|
12
|
-
dependencies:
|
|
13
|
-
- !ruby/object:Gem::Dependency
|
|
14
|
-
name: pry
|
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
|
16
|
-
requirements:
|
|
17
|
-
- - ">="
|
|
18
|
-
- !ruby/object:Gem::Version
|
|
19
|
-
version: '0'
|
|
20
|
-
type: :development
|
|
21
|
-
prerelease: false
|
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
-
requirements:
|
|
24
|
-
- - ">="
|
|
25
|
-
- !ruby/object:Gem::Version
|
|
26
|
-
version: '0'
|
|
27
|
-
- !ruby/object:Gem::Dependency
|
|
28
|
-
name: pry-nav
|
|
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: rake
|
|
43
|
-
requirement: !ruby/object:Gem::Requirement
|
|
44
|
-
requirements:
|
|
45
|
-
- - ">="
|
|
46
|
-
- !ruby/object:Gem::Version
|
|
47
|
-
version: '0'
|
|
48
|
-
type: :development
|
|
49
|
-
prerelease: false
|
|
50
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
-
requirements:
|
|
52
|
-
- - ">="
|
|
53
|
-
- !ruby/object:Gem::Version
|
|
54
|
-
version: '0'
|
|
55
|
-
- !ruby/object:Gem::Dependency
|
|
56
|
-
name: rspec
|
|
57
|
-
requirement: !ruby/object:Gem::Requirement
|
|
58
|
-
requirements:
|
|
59
|
-
- - ">="
|
|
60
|
-
- !ruby/object:Gem::Version
|
|
61
|
-
version: '0'
|
|
62
|
-
type: :development
|
|
63
|
-
prerelease: false
|
|
64
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
-
requirements:
|
|
66
|
-
- - ">="
|
|
67
|
-
- !ruby/object:Gem::Version
|
|
68
|
-
version: '0'
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies: []
|
|
69
12
|
description: Here we are able to generate CPNJ and CPF documents, only numbers or
|
|
70
13
|
formatted.
|
|
71
14
|
email:
|
|
@@ -74,20 +17,9 @@ executables: []
|
|
|
74
17
|
extensions: []
|
|
75
18
|
extra_rdoc_files: []
|
|
76
19
|
files:
|
|
77
|
-
- ".github/dependabot.yml"
|
|
78
|
-
- ".gitignore"
|
|
79
|
-
- ".rspec"
|
|
80
|
-
- ".travis.yml"
|
|
81
20
|
- CHANGELOG.md
|
|
82
|
-
- Gemfile
|
|
83
|
-
- Gemfile.lock
|
|
84
|
-
- LICENSE
|
|
85
21
|
- LICENSE.txt
|
|
86
22
|
- README.md
|
|
87
|
-
- Rakefile
|
|
88
|
-
- bin/console
|
|
89
|
-
- bin/setup
|
|
90
|
-
- bra_documents.gemspec
|
|
91
23
|
- lib/bra_documents.rb
|
|
92
24
|
- lib/bra_documents/cnpj_generator.rb
|
|
93
25
|
- lib/bra_documents/cpf_generator.rb
|
|
@@ -102,11 +34,11 @@ homepage: https://github.com/bvicenzo/bra_documents
|
|
|
102
34
|
licenses:
|
|
103
35
|
- MIT
|
|
104
36
|
metadata:
|
|
105
|
-
allowed_push_host: https://rubygems.org
|
|
37
|
+
allowed_push_host: https://rubygems.org
|
|
106
38
|
homepage_uri: https://github.com/bvicenzo/bra_documents
|
|
107
39
|
source_code_uri: https://github.com/bvicenzo/bra_documents
|
|
108
40
|
changelog_uri: https://github.com/bvicenzo/bra_documents/blob/master/CHANGELOG.md
|
|
109
|
-
|
|
41
|
+
rubygems_mfa_required: 'true'
|
|
110
42
|
rdoc_options: []
|
|
111
43
|
require_paths:
|
|
112
44
|
- lib
|
|
@@ -114,15 +46,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
114
46
|
requirements:
|
|
115
47
|
- - ">="
|
|
116
48
|
- !ruby/object:Gem::Version
|
|
117
|
-
version:
|
|
49
|
+
version: 3.3.0
|
|
118
50
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
119
51
|
requirements:
|
|
120
52
|
- - ">="
|
|
121
53
|
- !ruby/object:Gem::Version
|
|
122
54
|
version: '0'
|
|
123
55
|
requirements: []
|
|
124
|
-
rubygems_version:
|
|
125
|
-
signing_key:
|
|
56
|
+
rubygems_version: 4.0.20
|
|
126
57
|
specification_version: 4
|
|
127
58
|
summary: A implementation for generating Brazilian CPF and CNPJ document numbers.
|
|
128
59
|
test_files: []
|
data/.github/dependabot.yml
DELETED