validates_identity-gt_dpi 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: '034002682d24ebce0711e2d096a93c1e4c3ecec1acc16779ed29ae9185974e8f'
4
+ data.tar.gz: f7b990288f64326204c6d78ab7be9d93651ed793b354de0653f44cc5ec633778
5
+ SHA512:
6
+ metadata.gz: 5b636ff9194d547c94e0f315d878aff8a29f47554aad21b860380e7bcbe1f1628763ddfa033daaeb8ec3d3b0b22fdb390a65ae8472d4e25c5e72aa3636050d22
7
+ data.tar.gz: c2712e586de37611b28f7f7e12c5d55429b8c88b8099700817b1e11ee6acb965e2141efa06e891e9e8da86ef6767152debe5427aef158f182fc416bd87897dae
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,16 @@
1
+ require:
2
+ - rubocop-rake
3
+ - rubocop-rspec
4
+
5
+ AllCops:
6
+ NewCops: enable
7
+ TargetRubyVersion: 2.6
8
+
9
+ Layout/LineLength:
10
+ Max: 150
11
+
12
+ Metrics/BlockLength:
13
+ Enabled: false
14
+
15
+ Style/Documentation:
16
+ Enabled: false
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ validate_identity
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.3.0
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [0.1.0] - 2024-02-26
2
+
3
+ ### Added
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at plribeiro3000@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Paulo Ribeiro
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,69 @@
1
+ # ValidatesIdentity::GtDpi
2
+
3
+ This projects aims to validate Guatemala [Documento Personal de Identificación](https://es.wikipedia.org/wiki/Documento_Personal_de_Identificaci%C3%B3n) identification document.
4
+ This project depends on `validates_identity` gem and is a plugin for it.
5
+ This project is based on documentations found [here](https://learn.sayari.com/interpreting-guatemalan-national-id-numbers/) and [here](https://www.mineduc.gob.gt/DIGEESP/documents/adecuacionesCurriculares/Documentos%20de%20Apoyo/C%C3%B3digos%20Departamentos-Municipios-Idiomas.pdf)
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'validates_identity-gt_dpi'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install validates_identity-gt_dpi
22
+
23
+ ## Usage
24
+
25
+ Just use as any other validator:
26
+
27
+ ```ruby
28
+ class User < ActiveRecord::Base
29
+ # :identity_type is the attribute that will be used to determine the identity type and is required
30
+ validates :identity, identity: { identity_type: :identity_type }
31
+ end
32
+ ```
33
+
34
+ ## Advanced Usage
35
+
36
+ New Identity Validators can be registered through the public apis of `ValidatesIdentity`
37
+
38
+ ```ruby
39
+ ValidatesIdentity.register_identity_type('CustomIdentity', CustomIdentityValidator)
40
+ ```
41
+
42
+ Each Validator should have:
43
+
44
+ - a constructor with 2 params: `value` and `options` as a hash
45
+ - a `valid?` method that returns a boolean
46
+
47
+ In case of a legacy system where keys were already defined and differ from the official ones, aliases can be registered as well
48
+
49
+ ```ruby
50
+ ValidatesIdentity.register_identity_type_alias('LegacyIdentity', 'CustomIdentity')
51
+ ```
52
+
53
+ ## Development
54
+
55
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
56
+
57
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
58
+
59
+ ## Contributing
60
+
61
+ Bug reports and pull requests are welcome on GitHub at https://github.com/plribeiro3000/validates_identity-gt_dpi. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/plribeiro3000/validates_identity-gt_dpi/blob/master/CODE_OF_CONDUCT.md).
62
+
63
+ ## License
64
+
65
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
66
+
67
+ ## Code of Conduct
68
+
69
+ Everyone interacting in the ValidatesIdentity::GtDpi project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/plribeiro3000/validates_identity-gt_dpi/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require 'rubocop/rake_task'
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,112 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ValidatesIdentity
4
+ module GtDpi
5
+ class Validator
6
+ VALIDATION_REGULAR_EXPRESSION = /\A([0-9]{8})([0-9])([0-9]{2})([0-9]{2})\z/i.freeze
7
+
8
+ attr_reader :value
9
+
10
+ def initialize(value)
11
+ @value = value.to_s
12
+ end
13
+
14
+ def valid?
15
+ return true if value.blank?
16
+ return false if number.nil?
17
+ return false if birth_state_invalid?
18
+ return false if birth_city_invalid?
19
+
20
+ calculated_checksum == checksum
21
+ end
22
+
23
+ def formatted
24
+ value
25
+ end
26
+
27
+ private
28
+
29
+ def result
30
+ @result ||= VALIDATION_REGULAR_EXPRESSION.match(value)
31
+ end
32
+
33
+ def number
34
+ return if result.nil?
35
+
36
+ @number ||= result[1]
37
+ end
38
+
39
+ def checksum
40
+ return if result.nil?
41
+
42
+ @checksum ||= result[2].to_i
43
+ end
44
+
45
+ def birth_state_code
46
+ return if result.nil?
47
+
48
+ @birth_state_code ||= result[3].to_i
49
+ end
50
+
51
+ def birth_state_invalid?
52
+ birth_state_code.zero? || birth_state_code > biggest_state_code
53
+ end
54
+
55
+ def birth_city_code
56
+ return if result.nil?
57
+
58
+ @birth_city_code ||= result[4].to_i
59
+ end
60
+
61
+ def birth_city_invalid?
62
+ birth_city_code.zero? || birth_city_code > biggest_city_code_for(birth_state_code)
63
+ end
64
+
65
+ def calculated_checksum
66
+ multiplied_numbers =
67
+ (0..7).map do |index|
68
+ (number[index].to_i * (index + 2))
69
+ end
70
+
71
+ multiplied_numbers.sum % 11
72
+ end
73
+
74
+ def biggest_city_code_for(state_code)
75
+ city_county_by_state[state_code]
76
+ end
77
+
78
+ def biggest_state_code
79
+ city_county_by_state.keys.max
80
+ end
81
+
82
+ # rubocop:disable Metrics/MethodLength
83
+ def city_county_by_state
84
+ {
85
+ 1 => 17, # Guatemala
86
+ 2 => 8, # El Progreso
87
+ 3 => 16, # Sacatepéquez
88
+ 4 => 16, # Chimaltenango
89
+ 5 => 13, # Escuintla
90
+ 6 => 14, # Santa Rosa
91
+ 7 => 19, # Sololá
92
+ 8 => 8, # Totonicapán
93
+ 9 => 24, # Quetzaltenango
94
+ 10 => 20, # Suchitepéquez
95
+ 11 => 9, # Retalhuleu
96
+ 12 => 29, # San Marcos
97
+ 13 => 32, # Huehuetenango
98
+ 14 => 22, # Quiché
99
+ 15 => 8, # Baja Verapaz
100
+ 16 => 17, # Alta Verapaz
101
+ 17 => 12, # Petén
102
+ 18 => 5, # Izabal
103
+ 19 => 10, # Zacapa
104
+ 20 => 11, # Chiquimula
105
+ 21 => 7, # Jalapa
106
+ 22 => 17 # Jutiapa
107
+ }
108
+ end
109
+ # rubocop:enable Metrics/MethodLength
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ValidatesIdentity
4
+ module GtDpi
5
+ VERSION = '0.1.0'
6
+ end
7
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'validates_identity'
4
+ require_relative 'gt_dpi/version'
5
+
6
+ class ValidatesIdentity
7
+ module GtDpi
8
+ autoload :Validator, 'validates_identity/gt_dpi/validator'
9
+ end
10
+ end
11
+
12
+ ValidatesIdentity.register_identity_type('GT_DPI', ValidatesIdentity::GtDpi::Validator)
13
+ ValidatesIdentity::ShouldaMatchers.register_allowed_values('GT_DPI', %w[1234567891507 2345678990102])
14
+ ValidatesIdentity::ShouldaMatchers.register_disallowed_values('GT_DPI', %w[1234567881507 1234567892307 1234567891618])
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: validates_identity-gt_dpi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Paulo Ribeiro
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-02-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activemodel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: validates_identity
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description:
42
+ email:
43
+ - plribeiro3000@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".rspec"
49
+ - ".rubocop.yml"
50
+ - ".ruby-gemset"
51
+ - ".ruby-version"
52
+ - CHANGELOG.md
53
+ - CODE_OF_CONDUCT.md
54
+ - LICENSE.txt
55
+ - README.md
56
+ - Rakefile
57
+ - lib/validates_identity/gt_dpi.rb
58
+ - lib/validates_identity/gt_dpi/validator.rb
59
+ - lib/validates_identity/gt_dpi/version.rb
60
+ homepage: https://github.com/plribeiro3000/validates_identity-gt_dpi
61
+ licenses:
62
+ - MIT
63
+ metadata:
64
+ homepage_uri: https://github.com/plribeiro3000/validates_identity-gt_dpi
65
+ source_code_uri: https://github.com/plribeiro3000/validates_identity-gt_dpi/blob/master
66
+ changelog_uri: https://github.com/plribeiro3000/validates_identity-gt_dpi/blob/master/CHANGELOG.md
67
+ rubygems_mfa_required: 'true'
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: 2.6.0
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubygems_version: 3.5.3
84
+ signing_key:
85
+ specification_version: 4
86
+ summary: Validates Guatemala DPI Document and test it with matchers in a simple way.
87
+ test_files: []