CurpMX 0.3.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 (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/curp_mx.rb +124 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7819449ad32b97f499f11eb10f68db65583939637084330439c750c898e11e98
4
+ data.tar.gz: 604fb5e29eeef5d75879de5ae325430012484253765239724d8e95c882a6fcf1
5
+ SHA512:
6
+ metadata.gz: f1b4c3b213baa582c13107df963805b42b3a52a18fec062f4689e638b35f9d3160c62bd76d277da365841298b1ec4ee2de505adc118377e32781748d7d4ca46f
7
+ data.tar.gz: c9f9256bcdde887f8ec5f0d328cc1e5f67876b264acaaa940d55639071b9c0a7e666a7420964b9041b08c200794cad0da552c80bea8ea53bdfca1f997c8ef604
data/lib/curp_mx.rb ADDED
@@ -0,0 +1,124 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'date'
4
+
5
+ module CurpMx
6
+ VERSION = '0.1.0'
7
+
8
+ # Used to validate a CURPs format and a few data points in it
9
+ class Validator
10
+ attr_reader :errors, :raw_input
11
+
12
+ # Basic CURP regex structure
13
+ REGEX = /\A(?<father_initial>[A-Z]{2})
14
+ (?<mother_initial>[A-Z]{1})
15
+ (?<name_initial>[A-Z]{1})
16
+ (?<birth_year>[0-9]{2})
17
+ (?<birth_month>[0-1][0-9])
18
+ (?<birth_day>[0-3][0-9])
19
+ (?<sex>[HM])
20
+ (?<state>[A-Z]{2})
21
+ (?<father_consonant>[^AEIOU])
22
+ (?<mother_consonant>[^AEIOU])
23
+ (?<name_consonant>[^AEIOU])
24
+ (?<key>[0-9]{2})\z/x.freeze
25
+
26
+ # States' initials as listed in
27
+ # Registro Nacional de Población (RENAPO)
28
+ STATES_RENAPO = %w[AS BC BS CC CS CH CL CM DF CX DG GT GR HG JC MC MN MS
29
+ NT NL OC PL QT QR SP SL SR TC TS TL VZ YN ZS].freeze
30
+
31
+ # Problematic name initials
32
+ NAME_ISSUES = %w[BACA LOCO BUEI BUEY MAME CACA MAMO
33
+ CAGA MEAS CAGO MEON CAKA MIAR CAKO MION COGE
34
+ MOCO COGI MOKO COJA MULA COJE MULO COJI NACA
35
+ COJO NACO COLA PEDA CULO PEDO FALO PENE FETO
36
+ PIPI GETA PITO GUEI POPO GUEY PUTA JETA PUTO
37
+ JOTO QULO KACA RATA KACO ROBA KAGA ROBE KAGO
38
+ ROBO KAKA RUIN KAKO SENO KOGE TETA KOGI VACA
39
+ KOJA VAGA KOJE VAGO KOJI VAKA KOJO VUEI KOLA
40
+ VUEY KULO WUEI LILO WUEY LOCA CACO MEAR].freeze
41
+
42
+ def self.valid?(curp)
43
+ new(curp).valid?
44
+ end
45
+
46
+ def initialize(curp)
47
+ @raw_input = curp
48
+ @errors = {}
49
+
50
+ validate
51
+ end
52
+
53
+ def valid?
54
+ @errors.empty?
55
+ end
56
+
57
+ def validate
58
+ @md = REGEX.match(@raw_input)
59
+
60
+ if @md.nil?
61
+ @errors[:format] ||= []
62
+ @errors[:format] << 'Invalid format'
63
+ return false
64
+ end
65
+
66
+ validate_state
67
+ validate_name_initials
68
+ validate_birth_date
69
+ validate_date_exists
70
+ end
71
+
72
+ private
73
+
74
+ def validate_state
75
+ return if STATES_RENAPO.include? @md[:state]
76
+
77
+ @errors[:state] << "Invalid state: '#{@md[:state]}'"
78
+ end
79
+
80
+ def validate_name_initials
81
+ return unless NAME_ISSUES.include?(name_initials)
82
+
83
+ @errors[:problematic_name] << "Problematic name initials: '#{name_initials}'"
84
+ end
85
+
86
+ def validate_birth_date
87
+ validate_birth_day
88
+ validate_birth_month
89
+ end
90
+
91
+ def validate_birth_day
92
+ birth_day = @md[:birth_day].to_i
93
+ return unless birth_day <= 0 || birth_day > 31
94
+
95
+ @errors[:birth_day] ||= []
96
+ @errors[:birth_day] << "Invalid birth day: '#{@md[:birth_day]}'"
97
+ end
98
+
99
+ def validate_birth_month
100
+ birth_month = @md[:birth_month].to_i
101
+ return unless birth_month <= 0 || birth_month > 12
102
+
103
+ @errors[:birth_month] ||= []
104
+ @errors[:birth_month] << "Invalid birth month: '#{@md[:birth_month]}'"
105
+ end
106
+
107
+ def validate_date_exists
108
+ date_str = "#{@md[:birth_year]}-#{@md[:birth_month]}-#{@md[:birth_day]}"
109
+ return if valid_date?(date_str)
110
+
111
+ @errors[:birth_date] ||= []
112
+ @errors[:birth_date] << "Invalid birth date (YYYY-mm-dd): #{date_str}"
113
+ end
114
+
115
+ def name_initials
116
+ [@md[:father_initial], @md[:mother_initial], @md[:name_initial]].join
117
+ end
118
+
119
+ def valid_date?(date_str)
120
+ # Inner works: Date.valid_date? 2020, 7, 21
121
+ Date.valid_date?(*date_str.split('-').map(&:to_i))
122
+ end
123
+ end
124
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: CurpMX
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - Salazar
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-10-18 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A simple validator for CURPs. Fast, easy, descriptive.
14
+ email:
15
+ - contacto@capisalazar.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/curp_mx.rb
21
+ homepage:
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubygems_version: 3.2.3
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Mexican CURP validation library
44
+ test_files: []