br-utils 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.
Files changed (40) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +120 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +5 -0
  5. data/Gemfile +3 -0
  6. data/LICENSE.txt +21 -0
  7. data/README.md +348 -0
  8. data/Rakefile +6 -0
  9. data/examples/boleto_usage_example.rb +79 -0
  10. data/examples/cep_usage_example.rb +148 -0
  11. data/examples/cnh_usage_example.rb +120 -0
  12. data/examples/cnpj_usage_example.rb +227 -0
  13. data/examples/cpf_usage_example.rb +237 -0
  14. data/examples/currency_usage_example.rb +266 -0
  15. data/examples/date_usage_example.rb +259 -0
  16. data/examples/email_usage_example.rb +321 -0
  17. data/examples/legal_nature_usage_example.rb +437 -0
  18. data/examples/legal_process_usage_example.rb +444 -0
  19. data/examples/license_plate_usage_example.rb +440 -0
  20. data/examples/phone_usage_example.rb +595 -0
  21. data/examples/pis_usage_example.rb +588 -0
  22. data/examples/renavam_usage_example.rb +499 -0
  23. data/examples/voter_id_usage_example.rb +573 -0
  24. data/lib/brazilian-utils/boleto-utils.rb +176 -0
  25. data/lib/brazilian-utils/cep-utils.rb +330 -0
  26. data/lib/brazilian-utils/cnh-utils.rb +88 -0
  27. data/lib/brazilian-utils/cnpj-utils.rb +202 -0
  28. data/lib/brazilian-utils/cpf-utils.rb +192 -0
  29. data/lib/brazilian-utils/currency-utils.rb +226 -0
  30. data/lib/brazilian-utils/data/legal_process_ids.json +38 -0
  31. data/lib/brazilian-utils/date-utils.rb +244 -0
  32. data/lib/brazilian-utils/email-utils.rb +54 -0
  33. data/lib/brazilian-utils/legal-nature-utils.rb +235 -0
  34. data/lib/brazilian-utils/legal-process-utils.rb +240 -0
  35. data/lib/brazilian-utils/license-plate-utils.rb +279 -0
  36. data/lib/brazilian-utils/phone-utils.rb +272 -0
  37. data/lib/brazilian-utils/pis-utils.rb +151 -0
  38. data/lib/brazilian-utils/renavam-utils.rb +113 -0
  39. data/lib/brazilian-utils/voter-id-utils.rb +165 -0
  40. metadata +123 -0
@@ -0,0 +1,440 @@
1
+ require 'brazilian-utils/license-plate-utils'
2
+
3
+ include BrazilianUtils::LicensePlateUtils
4
+
5
+ puts "=" * 80
6
+ puts "Brazilian License Plate Utils - Usage Examples"
7
+ puts "=" * 80
8
+
9
+ # ============================================================================
10
+ # Section 1: Format Detection
11
+ # ============================================================================
12
+ puts "\n1. Format Detection"
13
+ puts "-" * 80
14
+
15
+ plates_to_detect = [
16
+ 'ABC1234',
17
+ 'ABC-1234',
18
+ 'ABC1D34',
19
+ 'abc1d34',
20
+ 'XYZ9876'
21
+ ]
22
+
23
+ plates_to_detect.each do |plate|
24
+ format = get_format(plate)
25
+ puts " #{plate.ljust(15)} → Format: #{format || 'INVALID'}"
26
+ end
27
+
28
+ # ============================================================================
29
+ # Section 2: Old Format Validation
30
+ # ============================================================================
31
+ puts "\n2. Old Format Validation"
32
+ puts "-" * 80
33
+
34
+ old_format_plates = [
35
+ 'ABC1234', # Valid
36
+ 'ABC-1234', # Valid with dash
37
+ 'abc1234', # Valid lowercase
38
+ 'XYZ9876', # Valid
39
+ 'ABCD123', # Invalid - 4 letters
40
+ 'ABC123', # Invalid - too short
41
+ 'ABC1D34' # Invalid - Mercosul format
42
+ ]
43
+
44
+ old_format_plates.each do |plate|
45
+ valid = is_valid(plate, :old_format)
46
+ status = valid ? '✓ VALID' : '✗ INVALID'
47
+ puts " #{plate.ljust(15)} → #{status}"
48
+ end
49
+
50
+ # ============================================================================
51
+ # Section 3: Mercosul Format Validation
52
+ # ============================================================================
53
+ puts "\n3. Mercosul Format Validation"
54
+ puts "-" * 80
55
+
56
+ mercosul_plates = [
57
+ 'ABC1D34', # Valid
58
+ 'abc1d34', # Valid lowercase
59
+ 'XYZ2E56', # Valid
60
+ 'ABC1234', # Invalid - old format
61
+ 'ABC-1D34', # Invalid - has dash
62
+ 'ABCD123' # Invalid - wrong pattern
63
+ ]
64
+
65
+ mercosul_plates.each do |plate|
66
+ valid = is_valid(plate, :mercosul)
67
+ status = valid ? '✓ VALID' : '✗ INVALID'
68
+ puts " #{plate.ljust(15)} → #{status}"
69
+ end
70
+
71
+ # ============================================================================
72
+ # Section 4: General Validation (Any Format)
73
+ # ============================================================================
74
+ puts "\n4. General Validation (Any Format)"
75
+ puts "-" * 80
76
+
77
+ mixed_plates = [
78
+ 'ABC1234', # Valid old
79
+ 'ABC1D34', # Valid Mercosul
80
+ 'ABC-1234', # Valid old with dash
81
+ 'abc1d34', # Valid Mercosul lowercase
82
+ 'ABCD123', # Invalid
83
+ '1234567', # Invalid - only numbers
84
+ 'ABCDEFG' # Invalid - only letters
85
+ ]
86
+
87
+ mixed_plates.each do |plate|
88
+ valid = is_valid(plate)
89
+ format = get_format(plate)
90
+ status = valid ? "✓ VALID (#{format})" : '✗ INVALID'
91
+ puts " #{plate.ljust(15)} → #{status}"
92
+ end
93
+
94
+ # ============================================================================
95
+ # Section 5: Using the valid? Alias
96
+ # ============================================================================
97
+ puts "\n5. Using the valid? Alias"
98
+ puts "-" * 80
99
+
100
+ test_plates = ['ABC1234', 'ABC1D34', 'INVALID']
101
+
102
+ test_plates.each do |plate|
103
+ if valid?(plate)
104
+ puts " ✓ #{plate} is valid"
105
+ else
106
+ puts " ✗ #{plate} is invalid"
107
+ end
108
+ end
109
+
110
+ # ============================================================================
111
+ # Section 6: Formatting Old Format Plates
112
+ # ============================================================================
113
+ puts "\n6. Formatting Old Format Plates"
114
+ puts "-" * 80
115
+
116
+ old_plates_to_format = [
117
+ 'ABC1234',
118
+ 'abc1234',
119
+ 'ABC-1234',
120
+ 'xyz9876'
121
+ ]
122
+
123
+ old_plates_to_format.each do |plate|
124
+ formatted = format_license_plate(plate)
125
+ puts " #{plate.ljust(15)} → #{formatted}"
126
+ end
127
+
128
+ # ============================================================================
129
+ # Section 7: Formatting Mercosul Plates
130
+ # ============================================================================
131
+ puts "\n7. Formatting Mercosul Plates"
132
+ puts "-" * 80
133
+
134
+ mercosul_to_format = [
135
+ 'abc1d34',
136
+ 'ABC1D34',
137
+ 'xyz2e56',
138
+ 'XYZ2E56'
139
+ ]
140
+
141
+ mercosul_to_format.each do |plate|
142
+ formatted = format_license_plate(plate)
143
+ puts " #{plate.ljust(15)} → #{formatted}"
144
+ end
145
+
146
+ # ============================================================================
147
+ # Section 8: Using the format Alias
148
+ # ============================================================================
149
+ puts "\n8. Using the format Alias"
150
+ puts "-" * 80
151
+
152
+ plates_for_alias = ['ABC1234', 'ABC1D34', 'xyz9876']
153
+
154
+ plates_for_alias.each do |plate|
155
+ formatted = format(plate)
156
+ puts " #{plate.ljust(15)} → #{formatted}"
157
+ end
158
+
159
+ # ============================================================================
160
+ # Section 9: Converting Old Format to Mercosul
161
+ # ============================================================================
162
+ puts "\n9. Converting Old Format to Mercosul"
163
+ puts "-" * 80
164
+
165
+ plates_to_convert = [
166
+ 'ABC0000', # 0 → A
167
+ 'ABC1234', # 2 → C
168
+ 'ABC4567', # 5 → F
169
+ 'ABC9999', # 9 → J
170
+ 'XYZ7890', # 8 → I
171
+ 'abc-1234' # lowercase with dash
172
+ ]
173
+
174
+ plates_to_convert.each do |plate|
175
+ mercosul = convert_to_mercosul(plate)
176
+ if mercosul
177
+ puts " #{plate.ljust(15)} → #{mercosul} (digit #{plate.gsub('-', '')[4]} → letter #{mercosul[4]})"
178
+ else
179
+ puts " #{plate.ljust(15)} → Cannot convert (invalid or already Mercosul)"
180
+ end
181
+ end
182
+
183
+ # ============================================================================
184
+ # Section 10: Digit to Letter Conversion Table
185
+ # ============================================================================
186
+ puts "\n10. Digit to Letter Conversion Table"
187
+ puts "-" * 80
188
+
189
+ puts " When converting old format to Mercosul, the 5th character (first digit) is converted:"
190
+ puts
191
+
192
+ (0..9).each do |digit|
193
+ old_plate = "ABC#{digit}000"
194
+ mercosul_plate = convert_to_mercosul(old_plate)
195
+ letter = mercosul_plate[4] if mercosul_plate
196
+ puts " Digit #{digit} → Letter #{letter} (Example: #{old_plate} → #{mercosul_plate})"
197
+ end
198
+
199
+ # ============================================================================
200
+ # Section 11: Removing Symbols
201
+ # ============================================================================
202
+ puts "\n11. Removing Symbols"
203
+ puts "-" * 80
204
+
205
+ plates_with_symbols = [
206
+ 'ABC-1234',
207
+ 'XYZ-9876',
208
+ 'ABC1234',
209
+ 'ABC1D34',
210
+ 'A-B-C-1-2-3-4'
211
+ ]
212
+
213
+ plates_with_symbols.each do |plate|
214
+ clean = remove_symbols(plate)
215
+ puts " #{plate.ljust(20)} → #{clean}"
216
+ end
217
+
218
+ # ============================================================================
219
+ # Section 12: Generating Random Old Format Plates
220
+ # ============================================================================
221
+ puts "\n12. Generating Random Old Format Plates"
222
+ puts "-" * 80
223
+
224
+ puts " Generating 5 random old format plates (LLLNNNN):"
225
+ 5.times do
226
+ plate = generate('LLLNNNN')
227
+ formatted = format_license_plate(plate)
228
+ puts " Generated: #{formatted} (raw: #{plate})"
229
+ end
230
+
231
+ # ============================================================================
232
+ # Section 13: Generating Random Mercosul Plates
233
+ # ============================================================================
234
+ puts "\n13. Generating Random Mercosul Plates"
235
+ puts "-" * 80
236
+
237
+ puts " Generating 5 random Mercosul format plates (LLLNLNN):"
238
+ 5.times do
239
+ plate = generate('LLLNLNN')
240
+ formatted = format_license_plate(plate)
241
+ puts " Generated: #{formatted}"
242
+ end
243
+
244
+ # ============================================================================
245
+ # Section 14: Generating with Default Format
246
+ # ============================================================================
247
+ puts "\n14. Generating with Default Format"
248
+ puts "-" * 80
249
+
250
+ puts " Generating 3 plates with default format (Mercosul):"
251
+ 3.times do
252
+ plate = generate # Defaults to 'LLLNLNN'
253
+ puts " Generated: #{plate}"
254
+ end
255
+
256
+ # ============================================================================
257
+ # Section 15: Complete Workflow Example
258
+ # ============================================================================
259
+ puts "\n15. Complete Workflow Example"
260
+ puts "-" * 80
261
+
262
+ puts " Scenario: User enters an old format plate, validate and convert"
263
+ puts
264
+
265
+ user_input = 'abc-1234'
266
+ puts " User input: #{user_input}"
267
+
268
+ if is_valid(user_input)
269
+ # Get format
270
+ format_type = get_format(user_input)
271
+ puts " Format detected: #{format_type}"
272
+
273
+ # Format for display
274
+ formatted = format_license_plate(user_input)
275
+ puts " Formatted: #{formatted}"
276
+
277
+ # Remove symbols for storage
278
+ clean = remove_symbols(user_input)
279
+ puts " Clean (for database): #{clean}"
280
+
281
+ # Convert to Mercosul if old format
282
+ if format_type == 'LLLNNNN'
283
+ mercosul = convert_to_mercosul(user_input)
284
+ puts " Converted to Mercosul: #{mercosul}"
285
+
286
+ mercosul_formatted = format_license_plate(mercosul)
287
+ puts " Mercosul formatted: #{mercosul_formatted}"
288
+
289
+ # Verify the conversion is valid
290
+ if is_valid(mercosul, :mercosul)
291
+ puts " ✓ Conversion successful and valid!"
292
+ end
293
+ else
294
+ puts " Already in Mercosul format - no conversion needed"
295
+ end
296
+ else
297
+ puts " ✗ Invalid plate!"
298
+ end
299
+
300
+ # ============================================================================
301
+ # Section 16: Batch Processing Example
302
+ # ============================================================================
303
+ puts "\n16. Batch Processing Example"
304
+ puts "-" * 80
305
+
306
+ plate_database = [
307
+ 'ABC-1234',
308
+ 'DEF5678',
309
+ 'GHI1J23',
310
+ 'INVALID',
311
+ 'xyz-9876',
312
+ 'JKL2M34'
313
+ ]
314
+
315
+ puts " Processing #{plate_database.length} plates from database:"
316
+ puts
317
+
318
+ old_count = 0
319
+ mercosul_count = 0
320
+ invalid_count = 0
321
+
322
+ plate_database.each do |plate|
323
+ if is_valid(plate)
324
+ format_type = get_format(plate)
325
+
326
+ if format_type == 'LLLNNNN'
327
+ old_count += 1
328
+ mercosul = convert_to_mercosul(plate)
329
+ puts " #{format_license_plate(plate).ljust(15)} [OLD] → #{mercosul} (converted)"
330
+ else
331
+ mercosul_count += 1
332
+ puts " #{format_license_plate(plate).ljust(15)} [MERCOSUL] (no conversion needed)"
333
+ end
334
+ else
335
+ invalid_count += 1
336
+ puts " #{plate.ljust(15)} [INVALID]"
337
+ end
338
+ end
339
+
340
+ puts
341
+ puts " Summary:"
342
+ puts " Old format: #{old_count}"
343
+ puts " Mercosul format: #{mercosul_count}"
344
+ puts " Invalid: #{invalid_count}"
345
+ puts " Total: #{plate_database.length}"
346
+
347
+ # ============================================================================
348
+ # Section 17: Format-Specific Validation Example
349
+ # ============================================================================
350
+ puts "\n17. Format-Specific Validation Example"
351
+ puts "-" * 80
352
+
353
+ puts " Scenario: System only accepts Mercosul format"
354
+ puts
355
+
356
+ test_plates_mercosul_only = ['ABC1234', 'ABC1D34', 'XYZ2E56']
357
+
358
+ test_plates_mercosul_only.each do |plate|
359
+ if is_valid(plate, :mercosul)
360
+ puts " ✓ #{plate} accepted (Mercosul format)"
361
+ else
362
+ if is_valid(plate, :old_format)
363
+ mercosul = convert_to_mercosul(plate)
364
+ puts " ⚠ #{plate} rejected (old format) - converted to #{mercosul}"
365
+ else
366
+ puts " ✗ #{plate} rejected (invalid)"
367
+ end
368
+ end
369
+ end
370
+
371
+ # ============================================================================
372
+ # Section 18: Error Handling Example
373
+ # ============================================================================
374
+ puts "\n18. Error Handling Example"
375
+ puts "-" * 80
376
+
377
+ invalid_inputs = [
378
+ nil,
379
+ '',
380
+ 12345,
381
+ 'ABCD123',
382
+ 'ABC@1234',
383
+ 'ABC 1234'
384
+ ]
385
+
386
+ invalid_inputs.each do |input|
387
+ result = is_valid(input)
388
+ format_result = get_format(input)
389
+ convert_result = convert_to_mercosul(input) if input.is_a?(String)
390
+
391
+ puts " Input: #{input.inspect.ljust(20)}"
392
+ puts " is_valid: #{result}"
393
+ puts " get_format: #{format_result.inspect}"
394
+ puts " convert_to_mercosul: #{convert_result.inspect}" if input.is_a?(String)
395
+ puts
396
+ end
397
+
398
+ # ============================================================================
399
+ # Section 19: Case Sensitivity Test
400
+ # ============================================================================
401
+ puts "\n19. Case Sensitivity Test"
402
+ puts "-" * 80
403
+
404
+ case_variations = [
405
+ 'ABC1234', # Uppercase
406
+ 'abc1234', # Lowercase
407
+ 'AbC1234', # Mixed case
408
+ 'ABC1D34', # Uppercase Mercosul
409
+ 'abc1d34', # Lowercase Mercosul
410
+ 'AbC1d34' # Mixed case Mercosul
411
+ ]
412
+
413
+ case_variations.each do |plate|
414
+ valid = is_valid(plate)
415
+ formatted = format_license_plate(plate)
416
+ status = valid ? '✓' : '✗'
417
+ puts " #{status} #{plate.ljust(15)} → #{formatted} (#{valid ? 'valid' : 'invalid'})"
418
+ end
419
+
420
+ # ============================================================================
421
+ # Section 20: Generation with Invalid Format
422
+ # ============================================================================
423
+ puts "\n20. Generation with Invalid Format"
424
+ puts "-" * 80
425
+
426
+ invalid_formats = [
427
+ 'LLLLNNN', # Wrong pattern
428
+ 'INVALID', # Not a pattern
429
+ '', # Empty
430
+ 'NNNNLLL' # Reversed
431
+ ]
432
+
433
+ invalid_formats.each do |format_str|
434
+ result = generate(format_str)
435
+ puts " Format: #{format_str.ljust(15)} → Result: #{result.inspect}"
436
+ end
437
+
438
+ puts "\n" + "=" * 80
439
+ puts "License Plate Utils Examples Complete!"
440
+ puts "=" * 80