Module: BrazilianUtils::CNPJUtils
- Defined in:
- lib/brazilian-utils/cnpj-utils.rb
Constant Summary collapse
- V2_CHARSET =
Characters usable in the alphanumeric CNPJ's base (positions 0-11).
'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'.freeze
- V2_WEIGHTS_DV1 =
[5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2].freeze
- V2_WEIGHTS_DV2 =
[6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2].freeze
Class Method Summary collapse
-
.display(cnpj) ⇒ String?
Formats a CNPJ string for visual display (legacy method).
-
.format_cnpj(cnpj) ⇒ String?
Formats a CNPJ (Brazilian Company Registration Number) string for visual display.
-
.generate(branch: nil, version: 1) ⇒ String
Generates a random valid CNPJ digit string.
-
.parse(value, options = {}) ⇒ String
Removes CNPJ formatting and returns the normalized value, capped to 14 characters.
-
.remove_symbols(dirty) ⇒ String
Removes specific symbols from a CNPJ string.
-
.sieve(dirty) ⇒ String
Removes specific symbols from a CNPJ (Brazilian Company Registration Number) string.
-
.valid?(cnpj, version: 1) ⇒ Boolean
Returns whether or not the verifying checksum digits of the given CNPJ match its base number.
-
.validate(cnpj) ⇒ Boolean
Validates a CNPJ by comparing its verifying checksum digits to its base number.
Class Method Details
.display(cnpj) ⇒ String?
This method should not be used in new code and is only provided for backward compatibility. Use format_cnpj instead.
Formats a CNPJ string for visual display (legacy method).
Will format an adequately formatted numbers-only CNPJ string, adding in standard formatting visual aid symbols for display.
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/brazilian-utils/cnpj-utils.rb', line 54 def self.display(cnpj) return nil unless cnpj.to_s.match?(/^\d{14}$/) return nil if cnpj.chars.uniq.length == 1 format('%s.%s.%s/%s-%s', cnpj[0..1], cnpj[2..4], cnpj[5..7], cnpj[8..11], cnpj[12..13]) end |
.format_cnpj(cnpj) ⇒ String?
Formats a CNPJ (Brazilian Company Registration Number) string for visual display.
This function takes a CNPJ string as input, validates its format, and formats it with standard visual aid symbols for display purposes.
78 79 80 81 82 83 84 85 86 87 |
# File 'lib/brazilian-utils/cnpj-utils.rb', line 78 def self.format_cnpj(cnpj) return nil unless valid?(cnpj) format('%s.%s.%s/%s-%s', cnpj[0..1], cnpj[2..4], cnpj[5..7], cnpj[8..11], cnpj[12..13]) end |
.generate(branch: nil, version: 1) ⇒ String
Generates a random valid CNPJ digit string.
An optional branch number parameter can be given; it defaults to 1 (v1) or a random branch 1-9999 (v2, when not given).
155 156 157 158 159 160 161 162 163 164 |
# File 'lib/brazilian-utils/cnpj-utils.rb', line 155 def self.generate(branch: nil, version: 1) return generate_v2(branch) if version.to_i == 2 branch_num = branch.nil? ? 1 : branch % 10_000 branch_num = 1 if branch_num.zero? branch_str = branch_num.to_s.rjust(4, '0') base = format('%08d', rand(100_000_000)) + branch_str base + checksum(base) end |
.parse(value, options = {}) ⇒ String
Removes CNPJ formatting and returns the normalized value, capped to 14 characters.
301 302 303 304 305 306 307 308 309 310 311 312 313 |
# File 'lib/brazilian-utils/cnpj-utils.rb', line 301 def self.parse(value, = {}) return '' unless value.is_a?(String) || value.is_a?(Integer) version = ([:version] || ['version'] || 1).to_i cleaned = if version == 2 value.to_s.gsub(/[^a-zA-Z0-9]/, '').upcase else value.to_s.gsub(/\D/, '') end cleaned[0, 14] end |
.remove_symbols(dirty) ⇒ String
Removes specific symbols from a CNPJ string.
This function is an alias for the sieve function, offering a more descriptive name.
35 36 37 |
# File 'lib/brazilian-utils/cnpj-utils.rb', line 35 def self.remove_symbols(dirty) sieve(dirty) end |
.sieve(dirty) ⇒ String
This method should not be used in new code and is only provided for backward compatibility. Use remove_symbols instead.
Removes specific symbols from a CNPJ (Brazilian Company Registration Number) string.
This function takes a CNPJ string as input and removes all occurrences of the '.', '/' and '-' characters from it.
20 21 22 |
# File 'lib/brazilian-utils/cnpj-utils.rb', line 20 def self.sieve(dirty) dirty.to_s.delete('./-') end |
.valid?(cnpj, version: 1) ⇒ Boolean
Returns whether or not the verifying checksum digits of the given CNPJ match its base number.
This function does not verify the existence of the CNPJ; it only validates the format of the string.
134 135 136 137 138 |
# File 'lib/brazilian-utils/cnpj-utils.rb', line 134 def self.valid?(cnpj, version: 1) return false unless cnpj.is_a?(String) version.to_i == 2 ? valid_v2?(cnpj) : validate(cnpj) end |
.validate(cnpj) ⇒ Boolean
This method should not be used in new code and is only provided for backward compatibility. Use valid? instead.
Validates a CNPJ by comparing its verifying checksum digits to its base number.
This function checks the validity of a CNPJ by comparing its verifying checksum digits to its base number. The input should be a string of digits with the appropriate length.
107 108 109 110 111 112 113 114 |
# File 'lib/brazilian-utils/cnpj-utils.rb', line 107 def self.validate(cnpj) return false unless cnpj.to_s.match?(/^\d{14}$/) return false if cnpj.chars.uniq.length == 1 (0..1).all? do |i| hashdigit(cnpj, i + 13) == cnpj[12 + i].to_i end end |