cpf_cnpj_plus 0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b0205bbfedea020597d02606a1dd67e642278d11cca9f66b1aa798a136326bda
4
+ data.tar.gz: 0712a0959fe535b47d1d03c7f9a0493248354f242a6dd0b29d0a4716d8bd9dcb
5
+ SHA512:
6
+ metadata.gz: 4dd053dc269c7868957eea4fef508713e41022b31f9bc60ea40cfa1d0d928df60f7624c6e35fa5dd057912663640be2b36a2b7a23030475793507af0425f27cc
7
+ data.tar.gz: c84da6e355ed10be01f01745de51b5dac6f781d5a64bcd6cd60bc9e3772b3a15ebb0674065d9c666db5abc1e28cc39927c29b6927501f6c0fce3656d8d11fe3a
data/.byebug_history ADDED
@@ -0,0 +1,4 @@
1
+ c
2
+ described_class.valid_cpf?("10321255780")
3
+ described_class.valid_cpf?("103.212.557-80")
4
+ described_class.valid_cpf?("10321255780")
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,8 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.1
3
+
4
+ Style/StringLiterals:
5
+ EnforcedStyle: double_quotes
6
+
7
+ Style/StringLiteralsInInterpolation:
8
+ EnforcedStyle: double_quotes
data/CHANGELOG.md ADDED
@@ -0,0 +1,9 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2025-07-06
4
+
5
+ - Initial release
6
+ - Basic functionality for CPF and CNPJ validation.
7
+
8
+
9
+
@@ -0,0 +1,132 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, caste, color, religion, or sexual
10
+ identity and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
19
+
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the overall
26
+ community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or advances of
31
+ any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
33
+ * Public or private harassment
34
+ * Publishing others' private information, such as a physical or email address,
35
+ without their explicit permission
36
+ * Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official email address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at
63
+ [INSERT CONTACT METHOD].
64
+ All complaints will be reviewed and investigated promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
84
+
85
+ **Community Impact**: A violation through a single incident or series of
86
+ actions.
87
+
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or permanent
93
+ ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within the
113
+ community.
114
+
115
+ ## Attribution
116
+
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.1, available at
119
+ [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120
+
121
+ Community Impact Guidelines were inspired by
122
+ [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
123
+
124
+ For answers to common questions about this code of conduct, see the FAQ at
125
+ [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
126
+ [https://www.contributor-covenant.org/translations][translations].
127
+
128
+ [homepage]: https://www.contributor-covenant.org
129
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
130
+ [Mozilla CoC]: https://github.com/mozilla/diversity
131
+ [FAQ]: https://www.contributor-covenant.org/faq
132
+ [translations]: https://www.contributor-covenant.org/translations
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 DougNeo
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,93 @@
1
+ # CPF CNPJ Plus
2
+
3
+ **cpf_cnpj_plus** é uma biblioteca Ruby para **validar, gerar e formatar** números de CPF e CNPJ brasileiros, tanto via código Ruby quanto pela linha de comando.
4
+
5
+ ---
6
+
7
+ ## 🚀 Novidade: Suporte a CNPJ Alfanumérico
8
+
9
+ > **Atenção:** Esta gem já está atualizada para validar e formatar os novos CNPJs alfanuméricos, conforme a alteração anunciada pela Receita Federal para julho de 2026.
10
+ > Assim, seu sistema estará pronto para lidar tanto com o formato tradicional quanto com o novo padrão de CNPJ.
11
+
12
+ ---
13
+
14
+ ## Instalação
15
+
16
+ Adicione esta linha ao seu Gemfile:
17
+
18
+ ```ruby
19
+ gem "cpf_cnpj_plus"
20
+ ```
21
+
22
+
23
+ E execute:
24
+
25
+ ```bash
26
+ bundle install
27
+ ```
28
+ Ou instale diretamente com:
29
+
30
+ ```bash
31
+ gem install cpf_cnpj_plus
32
+ ```
33
+
34
+ ## Uso Básico
35
+
36
+ ### API Ruby
37
+
38
+ A gem oferece uma API unificada para CPF e CNPJ. Exemplos:
39
+
40
+ ```ruby
41
+ require "cpf_cnpj_plus"
42
+
43
+ CPF
44
+ CPFPlus.format("12345678909") # => "123.456.789-09"
45
+ CPFPlus.valid?("123.456.789-09") # => true ou false
46
+ CPFPlus.generate # => Gera um CPF válido
47
+
48
+ CNPJ
49
+ CNPJPlus.format("12345678000195") # => "12.345.678/0001-95"
50
+ CNPJPlus.valid?("12.345.678/0001-95") # => true ou false
51
+ CNPJPlus.generate # => Gera um CNPJ válido
52
+
53
+ CNPJ Alfanumérico (novo formato)
54
+ CNPJPlus.valid?("A1234567B00195") # => true ou false
55
+ CNPJPlus.format("A1234567B00195") # => "A12.345.67B/0019-5"
56
+ CNPJPlus.generate # => Gera um CNPJ alfanumérico válido
57
+ ```
58
+
59
+
60
+ ### Linha de comando
61
+
62
+ A gem fornece comandos CLI:
63
+
64
+ ```bash
65
+ $ cpf_plus --check 123.456.789-09
66
+ $ cpf_plus --format 12345678909
67
+ $ cpf_plus --generate
68
+ $ cnpj_plus --check 12.345.678/0001-95
69
+ $ cnpj_plus --check A12.345.67B/0019-5
70
+ $ cnpj_plus --generate
71
+ ```
72
+
73
+ ## Compatibilidade
74
+
75
+ - Ruby >= 3.1.0
76
+
77
+ ## Contribuição
78
+
79
+ 1. Faça um fork do projeto
80
+ 2. Crie sua branch (`git checkout -b minha-feature`)
81
+ 3. Commit suas alterações (`git commit -am 'Adiciona nova feature'`)
82
+ 4. Faça push para a branch (`git push origin minha-feature`)
83
+ 5. Abra um Pull Request
84
+
85
+ ## Licença
86
+
87
+ MIT
88
+
89
+ ---
90
+
91
+ *Para dúvidas ou sugestões, abra uma issue ou envie um pull request!*
92
+
93
+
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,59 @@
1
+ module CpfCnpjPlus
2
+ module Validator
3
+ class Cnpj
4
+ def self.valid?(cnpj)
5
+ cnpj = cnpj.to_s.upcase.gsub(/[^A-Z0-9]/, "")
6
+ return false unless cnpj.length == 14
7
+ return false if cpj_not_valid?(cnpj)
8
+ return false unless cnpj[0..11] =~ /^[A-Z0-9]{12}$/ && cnpj[12..13] =~ /^[0-9]{2}$/
9
+
10
+ base = cnpj[0..11].chars.map { |c| char_to_value(c) }
11
+ dv = cnpj[12..13]
12
+
13
+ dv_calc = calculate_cnpj_digits(base)
14
+ dv == dv_calc
15
+ end
16
+
17
+ def self.char_to_value(char)
18
+ if ("0".."9").include?(char)
19
+ char.ord - 48
20
+ elsif ("A".."Z").include?(char)
21
+ char.ord - 48 # Ajuste para A=10, B=11, ..., Z=35
22
+ else
23
+ raise ArgumentError, "Caractere inválido"
24
+ end
25
+ end
26
+
27
+ def self.calculate_cnpj_digits(base)
28
+ first_digit = calculate_digit(base, fator_index_1d)
29
+ second_digit = calculate_digit((base + [first_digit]), fator_index_2d)
30
+ "#{first_digit}#{second_digit}"
31
+ end
32
+
33
+
34
+ def self.fator_index_1d
35
+ [5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2]
36
+ end
37
+
38
+ def self.fator_index_2d
39
+ [6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2]
40
+ end
41
+
42
+ def self.calculate_digit(base, fator_index)
43
+ soma = 0
44
+ fator_index.each_with_index do |fator, index|
45
+ soma += base[index] * fator
46
+ end
47
+ digit = (soma * 10) % 11
48
+ digit = 0 if digit == 10
49
+ digit
50
+ end
51
+ def self.cpj_not_valid?(cnpj)
52
+ cnpj = cnpj.to_s.gsub(/[^0-9]/, "")
53
+ ["00000000000000", "11111111111111", "22222222222222", "33333333333333",
54
+ "44444444444444", "55555555555555", "66666666666666", "77777777777777",
55
+ "88888888888888", "99999999999999"].include?(cnpj)
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,45 @@
1
+ module CpfCnpjPlus
2
+ module Validator
3
+ class Cpf
4
+ def self.valid?(cpf)
5
+ cpf = cpf.to_s.gsub(/[^0-9]/, "")
6
+ return false unless cpf.length == 11 && cpf =~ /^\d{11}$/
7
+ return false if cpj_not_valid?(cpf)
8
+ first_digit(cpf) && second_digit(cpf)
9
+ end
10
+
11
+ private
12
+
13
+ def self.first_digit(cpf)
14
+ first_digit = cpf[0..8]
15
+ soma = 0
16
+ ponteiro = 10
17
+ first_digit.chars.each do |char|
18
+ res = (char.to_i * ponteiro)
19
+ soma += res
20
+ ponteiro -= 1
21
+ end
22
+ soma * 10 % 11 == cpf[9].to_i
23
+ end
24
+
25
+ def self.second_digit(cpf)
26
+ second_digit = cpf[0..9]
27
+ soma = 0
28
+ ponteiro = 11
29
+ second_digit.chars.each do |char|
30
+ res = (char.to_i * ponteiro)
31
+ soma += res
32
+ ponteiro -= 1
33
+ end
34
+ soma * 10 % 11 == cpf[10].to_i || (soma * 10 % 11 == 10 && cpf[10].to_i == 0)
35
+ end
36
+
37
+ def self.cpj_not_valid?(cpf)
38
+ cpf = cpf.to_s.gsub(/[^0-9]/, "")
39
+ ["00000000000", "11111111111", "22222222222", "33333333333",
40
+ "44444444444", "55555555555", "66666666666",
41
+ "77777777777", "88888888888", "99999999999"].include?(cpf)
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,6 @@
1
+ module CpfCnpjPlus
2
+ module Validator
3
+ require_relative 'validator/cnpj'
4
+ require_relative 'validator/cpf'
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CpfCnpjPlus
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "cpf_cnpj_plus/version"
4
+ require_relative "cpf_cnpj_plus/validator"
5
+
6
+ module CpfCnpjPlus
7
+ class Error < StandardError; end
8
+ def self.valid_cnpj?(cnpj)
9
+ CpfCnpjPlus::Validator::Cnpj.valid?(cnpj)
10
+ end
11
+
12
+ def self.valid_cpf?(cpf)
13
+ CpfCnpjPlus::Validator::Cpf.valid?(cpf)
14
+ end
15
+ end
@@ -0,0 +1,4 @@
1
+ module CpfCnpjPlus
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cpf_cnpj_plus
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - DougNeo
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 2025-07-07 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: simplecov
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '0'
19
+ type: :development
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: '0'
26
+ description: Fornece métodos práticos para validar, gerar e formatar números de CPF
27
+ e CNPJ, incluindo ferramentas de linha de comando para facilitar integrações em
28
+ sistemas bancários, governamentais e aplicações Ruby em geral.
29
+ email:
30
+ - doug.neo@gmail.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - ".byebug_history"
36
+ - ".rspec"
37
+ - ".rubocop.yml"
38
+ - CHANGELOG.md
39
+ - CODE_OF_CONDUCT.md
40
+ - LICENSE.txt
41
+ - README.md
42
+ - Rakefile
43
+ - lib/cpf_cnpj_plus.rb
44
+ - lib/cpf_cnpj_plus/validator.rb
45
+ - lib/cpf_cnpj_plus/validator/cnpj.rb
46
+ - lib/cpf_cnpj_plus/validator/cpf.rb
47
+ - lib/cpf_cnpj_plus/version.rb
48
+ - sig/cpf_cnpj_plus.rbs
49
+ homepage: https://github.com/DougNeo/cpf_cnpj_plus.git
50
+ licenses:
51
+ - MIT
52
+ metadata:
53
+ allowed_push_host: https://rubygems.org
54
+ homepage_uri: https://github.com/DougNeo/cpf_cnpj_plus.git
55
+ source_code_uri: https://github.com/DougNeo/cpf_cnpj_plus.git
56
+ changelog_uri: https://github.com/DougNeo/cpf_cnpj_plus.git/blob/main/CHANGELOG.md
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: 3.1.0
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ requirements: []
71
+ rubygems_version: 3.6.5
72
+ specification_version: 4
73
+ summary: Validação, geração e formatação de números de CPF e CNPJ para o Brasil.
74
+ test_files: []