Overview
Implement the br-utilities package as a class-based Ruby library to handle two key Brazilian taxpayer identification documents:
- CPF (Cadastro de Pessoas Físicas) — the individual taxpayer registry number issued by the Brazilian Federal Revenue Service (Receita Federal).
- CNPJ (Cadastro Nacional da Pessoa Jurídica) — the national registry number for legal entities, also managed by the Receita Federal.
Features
The package must provide the following capabilities for both CPF and CNPJ:
| Feature |
Description |
| Format |
Apply the canonical mask to a raw number string (e.g., 12345678000195 → 12.345.678/0001-95). |
| Generate |
Produce a random, valid CPF or CNPJ number (optionally formatted). |
| Validate |
Check whether a given number is mathematically valid using the módulo 11 algorithm. |
Document Specifications
CPF
- Length: 11 digits
- Formatted mask:
xxx.xxx.xxx-xx (last 2 digits are check digits)
- Validation: Módulo 11 algorithm applied twice to compute the two check digits.
CNPJ
- Length: 14 characters
- Formatted mask (numeric):
xx.xxx.xxx/xxxx-xx
- Formatted mask (alphanumeric):
xx.XXX.xxx/XXXX-xx — e.g., 12.ABC.345/01DE-35
- Validation: Módulo 11 algorithm; for alphanumeric CNPJs, the numeric value of uppercase letters is derived from the ASCII table.
Alphanumeric CNPJ Support
Starting July 2026, Brazil's Receita Federal will begin issuing alphanumeric CNPJs under Normative Instruction RFB No. 2,229/2024 and Joint Technical Note No. 2025.001. The new format retains the 14-character length, with the first 12 positions being alphanumeric (letters A–Z and digits 0–9) and the last 2 remaining purely numeric check digits. Existing numeric CNPJs remain valid and both formats will coexist indefinitely.
This package must handle both numeric and alphanumeric CNPJ formats seamlessly.
Class-Based API Design
The library should expose dedicated classes (e.g., BrUtilities::CPF and BrUtilities::CNPJ) rather than plain modules or functions, providing an object-oriented interface. Example of expected usage:
cpf = BrUtilities::CPF.new('53282085796')
cpf.valid? # => true
cpf.formatted # => '532.820.857-96'
cpf.stripped # => '53282085796'
BrUtilities::CPF.generate # => random valid CPF (unformatted)
BrUtilities::CPF.generate(true) # => random valid CPF (formatted)
cnpj = BrUtilities::CNPJ.new('12ABC34501DE35')
cnpj.valid? # => true
cnpj.formatted # => '12.ABC.345/01DE-35'
cnpj.stripped # => '12ABC34501DE35'
BrUtilities::CNPJ.generate # => random valid CNPJ (unformatted)
BrUtilities::CNPJ.generate(true) # => random valid CNPJ (formatted)
Acceptance Criteria
Overview
Implement the
br-utilitiespackage as a class-based Ruby library to handle two key Brazilian taxpayer identification documents:Features
The package must provide the following capabilities for both CPF and CNPJ:
12345678000195→12.345.678/0001-95).Document Specifications
CPF
xxx.xxx.xxx-xx(last 2 digits are check digits)CNPJ
xx.xxx.xxx/xxxx-xxxx.XXX.xxx/XXXX-xx— e.g.,12.ABC.345/01DE-35Alphanumeric CNPJ Support
Starting July 2026, Brazil's Receita Federal will begin issuing alphanumeric CNPJs under Normative Instruction RFB No. 2,229/2024 and Joint Technical Note No. 2025.001. The new format retains the 14-character length, with the first 12 positions being alphanumeric (letters A–Z and digits 0–9) and the last 2 remaining purely numeric check digits. Existing numeric CNPJs remain valid and both formats will coexist indefinitely.
This package must handle both numeric and alphanumeric CNPJ formats seamlessly.
Class-Based API Design
The library should expose dedicated classes (e.g.,
BrUtilities::CPFandBrUtilities::CNPJ) rather than plain modules or functions, providing an object-oriented interface. Example of expected usage:Acceptance Criteria
BrUtilities::CPFclass with#valid?,#formatted,#strippedinstance methods and.generateclass method.BrUtilities::CNPJclass with#valid?,#formatted,#strippedinstance methods and.generateclass method.