curp_generator 1.0.0 → 1.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1a65ed003039b6871fef8ca8eca44a529706baf765010619457d8955d58ad032
4
- data.tar.gz: 184facb4300807434ad265dd068b1924e2ad884c29333839564efa2e79be8d9c
3
+ metadata.gz: 0d11b87f0397f7dcd5831ef07ea67eb6f29889fc95a893716b1dba19a19dd615
4
+ data.tar.gz: 2f890f4ce2669444a87c3eb8e75e06bd13eb6980969a2eb2dd6771be067ee403
5
5
  SHA512:
6
- metadata.gz: a3d031d50701c6554cdc9f7c024e472157c27d58d1d8c42f4ccc60399b3a4b4b824ef144a26ebf9fa56eca25c4aae41d2120d98b0b5e8fd26fe6a93dcc8a3283
7
- data.tar.gz: c6647a39d66e0445feb5f1e0ccaf8508387f075916ce560750490e986d412976e5a3367bf821a9ac8103e65f0fbb611dbde2a9bccd63b7a1b37390b08ee48e75
6
+ metadata.gz: aa637fec84655aad27564a7b4aea659fcfabc055fe70f3b49731e800cac7263ed18d752e9210d3c0299d5c990e676ef6fe8ba87afd3a626b5dc90477590d8459
7
+ data.tar.gz: 0ff4930ccb17cd5f2a545f7381db129babb42510fa84379d001e375d4eed0d005b2265a53195bf83fcce0400f1b2dc7964df5b105a72c53e5bd6b6d372b5562d
data/lib/base.rb ADDED
@@ -0,0 +1,54 @@
1
+ require 'curp'
2
+
3
+ class CurpGenerator::Base
4
+ include CurpGenerator::Catalogs
5
+
6
+ InvalidCurpArgumentError = Class.new(StandardError)
7
+
8
+ def generate
9
+ raise NotImplementedError
10
+ end
11
+
12
+ private
13
+
14
+ def valid_params?
15
+ raise NotImplementedError
16
+ end
17
+
18
+ def error_message!
19
+ raise NotImplementedError
20
+ end
21
+
22
+ def parse_attribute(attribute)
23
+ return if blank_string?(attribute)
24
+
25
+ str = remove_special_chars(normalize(attribute))
26
+ remove_composed_names(str)
27
+ end
28
+
29
+ def blank_string?(value)
30
+ value.to_s.strip.empty?
31
+ end
32
+
33
+ def remove_special_chars(string)
34
+ string&.gsub(/[.'\d-]/, "")
35
+ end
36
+
37
+ def normalize(string)
38
+ string&.tr(
39
+ "ÀàÁáÄäÈèÉéËëÌìÍíÏïÒòÓóÖöÙùÚúÜüÑñ",
40
+ "AaAaAaEeEeEeIiIiIiOoOoOoUuUuUuXx"
41
+ )
42
+ end
43
+
44
+ def remove_composed_names(name)
45
+ name = name.upcase
46
+
47
+ COMPOSED_NAMES.each do |composed|
48
+ next unless name.include?(composed)
49
+
50
+ name = name.gsub(composed, '')
51
+ end
52
+ name
53
+ end
54
+ end
data/lib/birth_date.rb ADDED
@@ -0,0 +1,37 @@
1
+ require 'base'
2
+
3
+ class CurpGenerator::BirthDate < CurpGenerator::Base
4
+ def initialize(birth_date)
5
+ @birth_date = birth_date
6
+ end
7
+
8
+ def self.generate(birth_date)
9
+ new(birth_date).generate
10
+ end
11
+
12
+ def self.homoclave_digit(birth_date)
13
+ new(birth_date).homoclave_digit
14
+ end
15
+
16
+ def generate
17
+ invalid_params? ? error_message! : parsed_date
18
+ end
19
+
20
+ def homoclave_digit
21
+ @birth_date.year < 2000 ? '0' : 'A'
22
+ end
23
+
24
+ private
25
+
26
+ def invalid_params?
27
+ blank_string?(@birth_date) || !@birth_date.respond_to?(:strftime)
28
+ end
29
+
30
+ def error_message!
31
+ raise InvalidCurpArgumentError, 'Invalid date format'
32
+ end
33
+
34
+ def parsed_date
35
+ @birth_date.strftime("%y%m%d")
36
+ end
37
+ end
data/lib/catalogs.rb CHANGED
@@ -1,39 +1,103 @@
1
1
  module CurpGenerator::Catalogs
2
- VALID_CHARACTERS = '0123456789ABCDEFGHIJKLMNÑOPQRSTUVWXYZ'.freeze
3
-
4
2
  STATES = {
5
- 'AGUASCALIENTES' => 'AS',
3
+ 'AGUASCALIENTES' => 'AS',
4
+ 'AG' => 'AS',
5
+ 'AGU' => 'AS',
6
6
  'BAJA CALIFORNIA NORTE' => 'BC',
7
- 'BAJA CALIFORNIA SUR' => 'BS',
8
- 'CAMPECHE' => 'CC',
9
- 'CHIAPAS' => 'CS',
10
- 'CHIHUAHUA' => 'CH',
11
- 'CIUDAD DE MEXICO' => 'DF',
12
- 'COAHUILA' => 'CL',
13
- 'COLIMA' => 'CM',
14
- 'DURANGO' => 'DG',
15
- 'GUANAJUATO' => 'GT',
16
- 'GUERRERO' => 'GR',
17
- 'HIDALGO' => 'HG',
18
- 'JALISCO' => 'JC',
19
- 'MEXICO' => 'MC',
20
- 'MICHOACAN' => 'MN',
21
- 'MORELOS' => 'MS',
22
- 'NAYARIT' => 'NT',
23
- 'NUEVO LEON' => 'NL',
24
- 'OAXACA' => 'OC',
25
- 'PUEBLA' => 'PL',
26
- 'QUERETARO' => 'QT',
27
- 'QUINTANA ROO' => 'QR',
28
- 'SAN LUIS POTOSI' => 'SP',
29
- 'SINALOA' => 'SL',
30
- 'SONORA' => 'SR',
31
- 'TABASCO' => 'TC',
32
- 'TAMAULIPAS' => 'TS',
33
- 'TLAXCALA' => 'TL',
34
- 'VERACRUZ' => 'VZ',
35
- 'YUCATAN' => 'YN',
36
- 'ZACATECAS' => 'ZS'
7
+ 'BC' => 'BC',
8
+ 'BCN' => 'BC',
9
+ 'BAJA CALIFORNIA SUR' => 'BS',
10
+ 'BS' => 'BS',
11
+ 'BCS' => 'BS',
12
+ 'CAMPECHE' => 'CC',
13
+ 'CM' => 'CC',
14
+ 'CAM' => 'CC',
15
+ 'CHIAPAS' => 'CS',
16
+ 'CS' => 'CS',
17
+ 'CHP' => 'CS',
18
+ 'CHIHUAHUA' => 'CH',
19
+ 'CH' => 'CH',
20
+ 'CHH' => 'CH',
21
+ 'COAHUILA' => 'CL',
22
+ 'CO' => 'CL',
23
+ 'COA' => 'CL',
24
+ 'COLIMA' => 'CM',
25
+ 'CL' => 'CM',
26
+ 'COL' => 'CM',
27
+ 'CIUDAD DE MEXICO' => 'DF',
28
+ 'DF' => 'DF',
29
+ 'CMX' => 'DF',
30
+ 'CDMX' => 'DF',
31
+ 'DURANGO' => 'DG',
32
+ 'DG' => 'DG',
33
+ 'DUR' => 'DG',
34
+ 'GUANAJUATO' => 'GT',
35
+ 'GT' => 'GT',
36
+ 'GUA' => 'GT',
37
+ 'GUERRERO' => 'GR',
38
+ 'GR' => 'GR',
39
+ 'GRO' => 'GR',
40
+ 'HIDALGO' => 'HG',
41
+ 'HG' => 'HG',
42
+ 'HID' => 'HG',
43
+ 'JALISCO' => 'JC',
44
+ 'JA' => 'JC',
45
+ 'JAL' => 'JC',
46
+ 'MEXICO' => 'MC',
47
+ 'EM' => 'MC',
48
+ 'MEX' => 'MC',
49
+ 'ESTADO DE MEXICO' => 'MC',
50
+ 'MICHOACAN' => 'MN',
51
+ 'MI' => 'MN',
52
+ 'MIC' => 'MN',
53
+ 'MORELOS' => 'MS',
54
+ 'MO' => 'MS',
55
+ 'MOR' => 'MS',
56
+ 'NAYARIT' => 'NT',
57
+ 'NA' => 'NT',
58
+ 'NAY' => 'NT',
59
+ 'NUEVO LEON' => 'NL',
60
+ 'NL' => 'NL',
61
+ 'NLE' => 'NL',
62
+ 'OAXACA' => 'OC',
63
+ 'OA' => 'OC',
64
+ 'OAX' => 'OC',
65
+ 'PUEBLA' => 'PL',
66
+ 'PU' => 'PL',
67
+ 'PUE' => 'PL',
68
+ 'QUERETARO' => 'QT',
69
+ 'QT' => 'QT',
70
+ 'QUE' => 'QT',
71
+ 'QUINTANA ROO' => 'QR',
72
+ 'QR' => 'QR',
73
+ 'ROO' => 'QR',
74
+ 'SAN LUIS POTOSI' => 'SP',
75
+ 'SL' => 'SP',
76
+ 'SLP' => 'SP',
77
+ 'SINALOA' => 'SL',
78
+ 'SI' => 'SL',
79
+ 'SIN' => 'SL',
80
+ 'SONORA' => 'SR',
81
+ 'SO' => 'SR',
82
+ 'SON' => 'SR',
83
+ 'TABASCO' => 'TC',
84
+ 'TB' => 'TC',
85
+ 'TAB' => 'TC',
86
+ 'TAMAULIPAS' => 'TS',
87
+ 'TM' => 'TS',
88
+ 'TAM' => 'TS',
89
+ 'TLAXCALA' => 'TL',
90
+ 'TL' => 'TL',
91
+ 'TLA' => 'TL',
92
+ 'VERACRUZ' => 'VZ',
93
+ 'VE' => 'VZ',
94
+ 'VER' => 'VZ',
95
+ 'YUCATAN' => 'YN',
96
+ 'YU' => 'YN',
97
+ 'YUC' => 'YN',
98
+ 'ZACATECAS' => 'ZS',
99
+ 'ZA' => 'ZS',
100
+ 'ZAC' => 'ZS'
37
101
  }.freeze
38
102
 
39
103
  FORBIDDEN_WORDS = {
data/lib/curp.rb CHANGED
@@ -1,98 +1,68 @@
1
1
  require 'version'
2
2
  require 'catalogs'
3
- require 'element'
4
- require 'helpers'
3
+ require 'name'
4
+ require 'birth_date'
5
+ require 'digit_verifier'
6
+ require 'gender'
7
+ require 'state'
5
8
 
6
9
  class CurpGenerator::Curp
7
10
  include CurpGenerator::Catalogs
8
- include CurpGenerator::Helpers
9
11
 
10
- MissingParamError = Class.new(StandardError)
12
+ InvalidArgumentError = Class.new(StandardError)
11
13
 
12
- PARAMS_TO_VALIDATE = %w(
13
- first_last_name
14
- first_name
15
- birth_date
16
- birth_state
17
- gender
18
- ).freeze
19
-
20
- def initialize(data = {})
21
- @first_name = data.dig(:first_name)
22
- @second_name = data.dig(:second_name)
23
- @first_last_name = parse_attribute(data.dig(:first_last_name))
24
- @second_last_name = parse_attribute(data.dig(:second_last_name)) || ''
25
- @birth_date = data.dig(:birth_date)
26
- @birth_state = parse_attribute(data.dig(:birth_state))
27
- @gender = data.dig(:gender)
14
+ def initialize(data={})
15
+ @first_name = data[:first_name]
16
+ @second_name = data[:second_name]
17
+ @first_last_name = data[:first_last_name]
18
+ @second_last_name = data[:second_last_name]
19
+ @birth_date = data[:birth_date]
20
+ @birth_state = data[:birth_state]
21
+ @gender = data[:gender]
28
22
  end
29
23
 
30
24
  def generate
31
- validate_params
32
-
33
- prefix = partial_curp
34
- prefix + verifying_digit(prefix)
25
+ "#{partial_curp}#{verifier_digit}"
26
+ rescue => e
27
+ raise InvalidArgumentError, e
35
28
  end
36
29
 
37
30
  private
38
31
 
39
- def validate_params
40
- PARAMS_TO_VALIDATE.each do |param|
41
- raise MissingParamError, "Missing #{param}" if blank_string?(instance_variable_get("@#{param}"))
42
- end
43
- end
44
-
45
32
  def partial_curp
46
- validate_first_four_curp_digits +
47
- parse_date(@birth_date, "%y") +
48
- parse_date(@birth_date, "%m") +
49
- parse_date(@birth_date, "%d") +
50
- gender_character +
51
- state_code +
52
- first_last_name_element.next_consonant +
53
- second_last_name_element.next_consonant +
54
- first_name_element.next_consonant +
55
- homoclave_digit
56
- end
57
-
58
- def validate_first_four_curp_digits
59
- FORBIDDEN_WORDS.dig(first_four_curp_digits) || first_four_curp_digits
33
+ @partial_curp ||= prefix_name +
34
+ parsed_date +
35
+ gender_character +
36
+ state_character +
37
+ sufix_name +
38
+ homoclave_digit
60
39
  end
61
40
 
62
- def gender_character
63
- @gender == 'male' ? 'H' : 'M'
64
- end
65
-
66
- def state_code
67
- STATES.dig(@birth_state.upcase) || 'NE'
41
+ def verifier_digit
42
+ CurpGenerator::DigitVerifier.generate(partial_curp)
68
43
  end
69
44
 
70
- def homoclave_digit
71
- @birth_date.year < 2000 ? '0' : 'A'
45
+ def prefix_name
46
+ CurpGenerator::Name.prefix_name(@first_name, @second_name, @first_last_name, @second_last_name)
72
47
  end
73
48
 
74
- def first_four_curp_digits
75
- @first_four_curp_digits ||=
76
- first_last_name_element.first_character +
77
- first_last_name_element.next_vowel +
78
- second_last_name_element.first_character +
79
- first_name_element.first_character
49
+ def parsed_date
50
+ CurpGenerator::BirthDate.generate(@birth_date)
80
51
  end
81
52
 
82
- def first_last_name_element
83
- @first_last_name_element ||= CurpGenerator::Element.new(@first_last_name)
53
+ def gender_character
54
+ CurpGenerator::Gender.generate(@gender)
84
55
  end
85
56
 
86
- def second_last_name_element
87
- @second_last_name_element ||= CurpGenerator::Element.new(@second_last_name)
57
+ def state_character
58
+ CurpGenerator::State.generate(@birth_state)
88
59
  end
89
60
 
90
- def first_name_element
91
- @first_name_element ||= CurpGenerator::Element.new(validate_first_name)
61
+ def sufix_name
62
+ CurpGenerator::Name.sufix_name(@first_name, @second_name, @first_last_name, @second_last_name)
92
63
  end
93
64
 
94
- def validate_first_name
95
- name = parse_attribute(@first_name)
96
- COMMON_NAMES.include?(name) ? @second_name : name
65
+ def homoclave_digit
66
+ CurpGenerator::BirthDate.homoclave_digit(@birth_date)
97
67
  end
98
68
  end
@@ -0,0 +1,38 @@
1
+ require 'base'
2
+
3
+ class CurpGenerator::DigitVerifier < CurpGenerator::Base
4
+ VALID_CHARACTERS = '0123456789ABCDEFGHIJKLMNÑOPQRSTUVWXYZ'.freeze
5
+
6
+ def initialize(partial_curp)
7
+ @partial_curp = partial_curp
8
+ end
9
+
10
+ def self.generate(partial_curp)
11
+ new(partial_curp).generate
12
+ end
13
+
14
+ def generate
15
+ invalid_params? ? error_message! : verifying_digit
16
+ end
17
+
18
+ private
19
+
20
+ def invalid_params?
21
+ blank_string?(@partial_curp) || @partial_curp.size != 17
22
+ end
23
+
24
+ def error_message!
25
+ raise InvalidCurpArgumentError, 'Missing partial curp'
26
+ end
27
+
28
+ def verifying_digit
29
+ length_sum = 0.0
30
+
31
+ @partial_curp.split('').each_with_index do |character, index|
32
+ length_sum += VALID_CHARACTERS.index(character) * (18 - index)
33
+ end
34
+
35
+ last_digit = 10 - (length_sum % 10)
36
+ last_digit == 10 ? '0' : last_digit.to_i.to_s
37
+ end
38
+ end
data/lib/gender.rb ADDED
@@ -0,0 +1,47 @@
1
+ require 'base'
2
+
3
+ class CurpGenerator::Gender < CurpGenerator::Base
4
+ VALID_MALE_GENDERS = %w[
5
+ male
6
+ hombre
7
+ masculino
8
+ h
9
+ ].freeze
10
+
11
+ VALID_FEMALE_GENDERS = %w[
12
+ female
13
+ mujer
14
+ femenino
15
+ m
16
+ ].freeze
17
+
18
+ def initialize(gender)
19
+ @gender = gender.to_s.downcase
20
+ end
21
+
22
+ def self.generate(gender)
23
+ new(gender).generate
24
+ end
25
+
26
+ def generate
27
+ invalid_params? ? error_message! : parsed_gender
28
+ end
29
+
30
+ private
31
+
32
+ def invalid_params?
33
+ blank_string?(@gender) || invalid_gender?
34
+ end
35
+
36
+ def invalid_gender?
37
+ !VALID_MALE_GENDERS.include?(@gender) && !VALID_FEMALE_GENDERS.include?(@gender)
38
+ end
39
+
40
+ def error_message!
41
+ raise InvalidCurpArgumentError, "Available gender options are #{VALID_MALE_GENDERS + VALID_FEMALE_GENDERS}"
42
+ end
43
+
44
+ def parsed_gender
45
+ VALID_MALE_GENDERS.include?(@gender) ? 'H' : 'M'
46
+ end
47
+ end
data/lib/name.rb ADDED
@@ -0,0 +1,101 @@
1
+ require 'base'
2
+ require 'catalogs'
3
+
4
+ class CurpGenerator::Name < CurpGenerator::Base
5
+ include CurpGenerator::Catalogs
6
+
7
+ def initialize(first_name, second_name, first_last_name, second_last_name)
8
+ @first_name = parse_attribute(first_name&.upcase)
9
+ @second_name = parse_attribute(second_name&.upcase)
10
+ @first_last_name = parse_attribute(first_last_name&.upcase)
11
+ @second_last_name = parse_attribute(second_last_name&.upcase)
12
+ end
13
+
14
+ def self.prefix_name(first_name, second_name, first_last_name, second_last_name)
15
+ new(first_name, second_name, first_last_name, second_last_name).prefix_name
16
+ end
17
+
18
+ def self.sufix_name(first_name, second_name, first_last_name, second_last_name)
19
+ new(first_name, second_name, first_last_name, second_last_name).sufix_name
20
+ end
21
+
22
+ def generate
23
+ error_message! if invalid_params?
24
+ end
25
+
26
+ def prefix_name
27
+ generate
28
+ FORBIDDEN_WORDS[prefix_consonants] || prefix_consonants
29
+ end
30
+
31
+ def sufix_name
32
+ generate
33
+ sufix_consonants
34
+ end
35
+
36
+ private
37
+
38
+ def invalid_params?
39
+ blank_string?(@first_name) || blank_string?(@first_last_name)
40
+ end
41
+
42
+ def error_message!
43
+ raise InvalidCurpArgumentError, 'Invalid name arguments'
44
+ end
45
+
46
+ def prefix_consonants
47
+ @prefix_consonants ||=
48
+ first_character(@first_last_name) +
49
+ next_vowel(@first_last_name) +
50
+ first_character(@second_last_name) +
51
+ first_character(valid_first_name)
52
+ end
53
+
54
+ def sufix_consonants
55
+ next_consonant(@first_last_name) +
56
+ next_consonant(@second_last_name) +
57
+ next_consonant(valid_first_name)
58
+ end
59
+
60
+ def valid_first_name
61
+ valid_second_name? ? @second_name : @first_name
62
+ end
63
+
64
+ def valid_second_name?
65
+ COMMON_NAMES.include?(@first_name) && !blank_string?(@second_name)
66
+ end
67
+
68
+ def first_character(str)
69
+ return 'X' if blank_string?(str)
70
+
71
+ str[0].upcase
72
+ end
73
+
74
+ def next_consonant(str)
75
+ return 'X' if blank_string?(str)
76
+
77
+ consonants = remove_vowels(str)
78
+ consonants.size.zero? ? 'X' : consonants[0].upcase
79
+ end
80
+
81
+ def next_vowel(str)
82
+ return 'X' if blank_string?(str)
83
+
84
+ vowels = remove_consonants(str)
85
+ vowels.size.zero? ? 'X' : vowels[0].upcase
86
+ end
87
+
88
+ def remove_vowels(str)
89
+ new_str = removing_first_char(str)
90
+ new_str.downcase.tr('aeiou', '')
91
+ end
92
+
93
+ def remove_consonants(str)
94
+ new_str = removing_first_char(str)
95
+ new_str.downcase.tr('^aeiou', '')
96
+ end
97
+
98
+ def removing_first_char(str)
99
+ str[1..]
100
+ end
101
+ end
data/lib/state.rb ADDED
@@ -0,0 +1,31 @@
1
+ require 'base'
2
+
3
+ class CurpGenerator::State < CurpGenerator::Base
4
+ include CurpGenerator::Catalogs
5
+
6
+ def initialize(birth_state)
7
+ @birth_state = parse_attribute(birth_state&.upcase)
8
+ end
9
+
10
+ def self.generate(birth_state)
11
+ new(birth_state).generate
12
+ end
13
+
14
+ def generate
15
+ invalid_params? ? error_message! : parsed_state
16
+ end
17
+
18
+ private
19
+
20
+ def invalid_params?
21
+ blank_string?(@birth_state)
22
+ end
23
+
24
+ def error_message!
25
+ raise InvalidCurpArgumentError, 'Missing birth state argument'
26
+ end
27
+
28
+ def parsed_state
29
+ STATES[@birth_state.upcase] || 'NE'
30
+ end
31
+ end
data/lib/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module CurpGenerator
2
- VERSION = '1.0.0'.freeze
2
+ VERSION = '1.1.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: curp_generator
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Juan Carlos Estebes
@@ -17,12 +17,15 @@ executables: []
17
17
  extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
+ - lib/base.rb
21
+ - lib/birth_date.rb
20
22
  - lib/catalogs.rb
21
23
  - lib/curp.rb
22
24
  - lib/curp_generator.rb
23
- - lib/element.rb
24
- - lib/helpers.rb
25
- - lib/string_format.rb
25
+ - lib/digit_verifier.rb
26
+ - lib/gender.rb
27
+ - lib/name.rb
28
+ - lib/state.rb
26
29
  - lib/version.rb
27
30
  homepage: https://github.com/yotepresto-com/curp-generator
28
31
  licenses:
@@ -46,7 +49,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
46
49
  - !ruby/object:Gem::Version
47
50
  version: '0'
48
51
  requirements: []
49
- rubygems_version: 3.0.6
52
+ rubygems_version: 3.0.9
50
53
  signing_key:
51
54
  specification_version: 4
52
55
  summary: Generates a mexican CURP given the information of a person
data/lib/element.rb DELETED
@@ -1,45 +0,0 @@
1
- require 'helpers'
2
- require 'string_format'
3
-
4
- class CurpGenerator::Element
5
- include CurpGenerator::Helpers
6
-
7
- attr_accessor :name, :consonant_index, :vowel_index
8
-
9
- def initialize(name)
10
- @name = name
11
- @consonant_index = 0
12
- @vowel_index = 0
13
- end
14
-
15
- def first_character
16
- return 'X' if blank_string?(name)
17
-
18
- character = name[0].upcase
19
- update_next_index_attribute(character)
20
- character == 'Ñ' ? 'X' : character
21
- end
22
-
23
- def next_consonant
24
- return 'X' if blank_string?(name)
25
-
26
- consonants = CurpGenerator::StringFormat.new(name).remove_vowels.upcase
27
- consonants.size < 2 ? consonants[0] : consonants[consonant_index]
28
- end
29
-
30
- def next_vowel
31
- return 'X' if blank_string?(name)
32
-
33
- consonants = CurpGenerator::StringFormat.new(name).remove_consonants.upcase
34
- consonants.size < 2 ? 'X' : consonants[vowel_index]
35
- end
36
-
37
- private
38
-
39
- def update_next_index_attribute(character)
40
- new_consonant_value = CurpGenerator::StringFormat.new(character).consonant? ? 1 : 0
41
- new_vowel_value = CurpGenerator::StringFormat.new(character).vowel? ? 1 : 0
42
- self.consonant_index = new_consonant_value
43
- self.vowel_index = new_vowel_value
44
- end
45
- end
data/lib/helpers.rb DELETED
@@ -1,41 +0,0 @@
1
- require 'date'
2
- require 'catalogs'
3
- require 'string_format'
4
-
5
- module CurpGenerator::Helpers
6
- include CurpGenerator::Catalogs
7
-
8
- def verifying_digit(partial_curp)
9
- length_sum = 0.0
10
-
11
- partial_curp.split('').each_with_index do |character, index|
12
- length_sum += VALID_CHARACTERS.index(character) * (18 - index)
13
- end
14
-
15
- last_digit = 10 - (length_sum % 10)
16
- last_digit == 10 ? '0' : last_digit.to_i.to_s
17
- end
18
-
19
- def remove_composed_names(name)
20
- name = name.upcase
21
-
22
- COMPOSED_NAMES.each do |composed|
23
- next unless name.include?(composed)
24
- name = name.gsub(composed, '')
25
- end
26
- name
27
- end
28
-
29
- def parse_attribute(attribute)
30
- return if blank_string?(attribute)
31
- CurpGenerator::StringFormat.new(remove_composed_names(attribute)).str
32
- end
33
-
34
- def parse_date(date, format)
35
- date.strftime(format)
36
- end
37
-
38
- def blank_string?(value)
39
- value.to_s.strip.empty?
40
- end
41
- end
data/lib/string_format.rb DELETED
@@ -1,43 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class CurpGenerator::StringFormat
4
-
5
- attr_accessor :str
6
-
7
- def initialize(str)
8
- @str = remove_special_chars(normalize(str.strip))
9
- end
10
-
11
- def remove_vowels
12
- str.downcase.tr('aeiou', '')
13
- end
14
-
15
- def remove_consonants
16
- str.downcase.tr('^aeiou', '')
17
- end
18
-
19
- def vowel?
20
- %w(A E I O U).include?(str.upcase)
21
- end
22
-
23
- def consonant?
24
- !vowel?
25
- end
26
-
27
- def first_vowel
28
- str.downcase.match(/a|e|i|o|u/).to_s.upcase
29
- end
30
-
31
- private
32
-
33
- def normalize(string)
34
- string&.tr(
35
- "ÀàÁáÄäÈèÉéËëÌìÍíÏïÒòÓóÖöÙùÚúÜüÑñ",
36
- "AaAaAaEeEeEeIiIiIiOoOoOoUuUuUuNn"
37
- )
38
- end
39
-
40
- def remove_special_chars(string)
41
- string&.gsub(/[\.\'\d-]/, "")
42
- end
43
- end