Module: BrazilianUtils::LegalProcessUtils
- Defined in:
- lib/brazilian-utils/legal-process-utils.rb
Overview
Utilities for formatting, validating, and generating Brazilian Legal Process IDs.
A legal process ID (Número de Processo Judicial) is a 20-digit code that identifies a legal case in the Brazilian judiciary system. The format is: NNNNNNN-DD.AAAA.J.TR.OOOO
Where:
- NNNNNNN: Sequential number (7 digits)
- DD: Verification digits (2 digits) - checksum
- AAAA: Year the process was filed (4 digits)
- J: Judicial segment (1 digit) - Orgão
- TR: Court (2 digits) - Tribunal
- OOOO: Court of origin (4 digits) - Foro
This module does not verify if a legal process ID corresponds to a real case; it only validates the format and structure of the ID.
Constant Summary collapse
- DATA_FILE =
Path to the JSON file containing valid tribunal and foro IDs
File.join(File.dirname(__FILE__), 'data', 'legal_process_ids.json')
Class Method Summary collapse
-
.format_legal_process(legal_process_id) ⇒ String?
Formats a legal process ID into the standard format.
-
.generate(year = Time.now.year, orgao = nil) ⇒ String?
Generates a random legal process ID.
-
.is_valid(legal_process_id) ⇒ Boolean
(also: valid?)
Checks if a legal process ID is valid.
-
.parse(value) ⇒ String
Removes legal process formatting and keeps only digits, capped to 20 digits.
-
.remove_symbols(legal_process) ⇒ String
Removes specific symbols (dots and hyphens) from a legal process ID.
Class Method Details
.format_legal_process(legal_process_id) ⇒ String?
Formats a legal process ID into the standard format.
Takes a 20-digit string and formats it as: NNNNNNN-DD.AAAA.J.TR.OOOO
65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/brazilian-utils/legal-process-utils.rb', line 65 def self.format_legal_process(legal_process_id) return nil unless legal_process_id.is_a?(String) return nil unless legal_process_id =~ /^\d{20}$/ # Extract fields: NNNNNNN DD AAAA J TR OOOO nnnnnnn = legal_process_id[0, 7] dd = legal_process_id[7, 2] aaaa = legal_process_id[9, 4] j = legal_process_id[13, 1] tr = legal_process_id[14, 2] oooo = legal_process_id[16, 4] "#{nnnnnnn}-#{dd}.#{aaaa}.#{j}.#{tr}.#{oooo}" end |
.generate(year = Time.now.year, orgao = nil) ⇒ String?
Generates a random legal process ID.
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/brazilian-utils/legal-process-utils.rb', line 156 def self.generate(year = Time.now.year, orgao = nil) if year.is_a?(Hash) = year year = [:year] || ['year'] || Time.now.year orgao = [:court] || [:orgao] || ['court'] || ['orgao'] end orgao ||= rand(1..9) return nil if year < Time.now.year return nil unless (1..9).include?(orgao) data = load_legal_process_data return nil unless data orgao_data = data["orgao_#{orgao}"] return nil unless orgao_data # Get random tribunal and foro tribunals = orgao_data['id_tribunal'] foros = orgao_data['id_foro'] tr = tribunals[rand(tribunals.length)].to_s.rjust(2, '0') oooo = foros[rand(foros.length)].to_s.rjust(4, '0') # Generate random sequential number nnnnnnn = rand(0..9999999).to_s.rjust(7, '0') # Calculate checksum base_for_checksum = nnnnnnn + year.to_s + orgao.to_s + tr + oooo dd = checksum(base_for_checksum.to_i) "#{nnnnnnn}#{dd}#{year}#{orgao}#{tr}#{oooo}" end |
.is_valid(legal_process_id) ⇒ Boolean Also known as: valid?
Checks if a legal process ID is valid.
This function validates:
- The format (20 digits)
- The checksum (DD verification digits)
- The tribunal (TR) and foro (OOOO) combination against the official table
This function does not verify if the legal process ID corresponds to a real case; it only validates the format and structure of the ID.
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/brazilian-utils/legal-process-utils.rb', line 104 def self.is_valid(legal_process_id) return false unless legal_process_id.is_a?(String) clean_id = remove_symbols(legal_process_id) return false unless clean_id =~ /^\d{20}$/ # Extract fields nnnnnnn = clean_id[0, 7] dd = clean_id[7, 2] aaaa = clean_id[9, 4] j = clean_id[13, 1] tr = clean_id[14, 2] oooo = clean_id[16, 4] # Validate checksum base_for_checksum = nnnnnnn + aaaa + j + tr + oooo expected_dd = checksum(base_for_checksum.to_i) return false unless dd == expected_dd # Validate tribunal and foro against JSON data validate_tribunal_and_foro(j.to_i, tr.to_i, oooo.to_i) end |
.parse(value) ⇒ String
Removes legal process formatting and keeps only digits, capped to 20 digits.
264 265 266 267 268 |
# File 'lib/brazilian-utils/legal-process-utils.rb', line 264 def self.parse(value) return '' unless value.is_a?(String) || value.is_a?(Integer) value.to_s.gsub(/\D/, '')[0, 20] end |
.remove_symbols(legal_process) ⇒ String
Removes specific symbols (dots and hyphens) from a legal process ID.
This function takes a legal process ID as input and removes all occurrences of the '.' and '-' characters from it.
42 43 44 45 46 |
# File 'lib/brazilian-utils/legal-process-utils.rb', line 42 def self.remove_symbols(legal_process) return '' unless legal_process.is_a?(String) legal_process.gsub('.', '').gsub('-', '') end |