social_security_number 0.1.1

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 (38) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +12 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +5 -0
  5. data/CODE_OF_CONDUCT.md +74 -0
  6. data/Gemfile +6 -0
  7. data/LICENSE.txt +21 -0
  8. data/README.md +97 -0
  9. data/Rakefile +6 -0
  10. data/bin/console +14 -0
  11. data/bin/setup +8 -0
  12. data/lib/social_security_number/country/be.rb +33 -0
  13. data/lib/social_security_number/country/ca.rb +48 -0
  14. data/lib/social_security_number/country/ch.rb +34 -0
  15. data/lib/social_security_number/country/cn.rb +42 -0
  16. data/lib/social_security_number/country/cz.rb +46 -0
  17. data/lib/social_security_number/country/de.rb +80 -0
  18. data/lib/social_security_number/country/dk.rb +48 -0
  19. data/lib/social_security_number/country/es.rb +41 -0
  20. data/lib/social_security_number/country/fi.rb +30 -0
  21. data/lib/social_security_number/country/fr.rb +32 -0
  22. data/lib/social_security_number/country/ie.rb +38 -0
  23. data/lib/social_security_number/country/is.rb +38 -0
  24. data/lib/social_security_number/country/it.rb +27 -0
  25. data/lib/social_security_number/country/lt.rb +57 -0
  26. data/lib/social_security_number/country/mx.rb +56 -0
  27. data/lib/social_security_number/country/nl.rb +26 -0
  28. data/lib/social_security_number/country/no.rb +56 -0
  29. data/lib/social_security_number/country/pk.rb +14 -0
  30. data/lib/social_security_number/country/se.rb +54 -0
  31. data/lib/social_security_number/country/uk.rb +68 -0
  32. data/lib/social_security_number/country/us.rb +52 -0
  33. data/lib/social_security_number/country.rb +103 -0
  34. data/lib/social_security_number/validator.rb +40 -0
  35. data/lib/social_security_number/version.rb +3 -0
  36. data/lib/social_security_number.rb +7 -0
  37. data/social_security_number.gemspec +36 -0
  38. metadata +126 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 23095de06edf086b572205262a5ef456a386638d
4
+ data.tar.gz: 75bc4b1822421ee0a34d4cb3a6a1718485458eb0
5
+ SHA512:
6
+ metadata.gz: 05b26a037eae8180b9521d65d1f687b070e4ba6a94c81f19e752e821cca70b3bded8280af69d8624fc16b50254c699add65f7cc583e56bd8ae976817a493e775
7
+ data.tar.gz: 003227a177c941f2f4be9f4e9e63e60e03492b1e9d861e69b2f18f302534de75cdd91dd66c4e180352bcc3a1ef788b04a3ca1ca342446f312d0740345a2008d3
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.15.3
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at contact@samesystem.com . All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in social_security_number.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 SameSystem
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,97 @@
1
+ # SocialSecurityNumber
2
+
3
+ This small Gem adds useful methods to your Ruby or Ruby on Rails app to validate for national identification numbers.
4
+
5
+ Find version information in the CHANGELOG.
6
+
7
+ ## Suppoted cauntries and numbers:
8
+
9
+ * Belgium National Register Number (Rijksregisternummer)
10
+ * Canadian Social Insurance Numbers (SINs)
11
+ * Swiss social security numbers
12
+ * Chinese Resident Identity Card Number
13
+ * Czech birth numbers
14
+ * Germany Steuer-IdNr
15
+ * Denmark Personal Identification Number (Det Centrale Personregister (CPR))
16
+ * Spain National Identity Document (Documento Nacional de Identidad (DNI)) number
17
+ * Finland Personal Identity Code (Finnish: henkilötunnus (HETU))
18
+ * France INSEE code
19
+ * Ireland Personal Public Service Number (PPS No)
20
+ * Iceland personal and organisation identity code (Kennitala)
21
+ * Italy tax code for individuals (Codice fiscale)
22
+ * Lithuania Personal Code (Asmens kodas)
23
+ * Mexico Unique Population Registry Code (Clave Única de Registro de Población (CURP))
24
+ * Netherlands Citizen's Service Number (Burgerservicenummer)
25
+ * Norway birth number (Fødselsnummer)
26
+ * Pakistan computerised national identity card number (CNIC)
27
+ * Sweden Personal Identity Number (Personnummer)
28
+ * United Kingdom National Insurance number (NINO)
29
+ * United Kingdom National Health Service number
30
+ * United Kingdom CHI (Community Health Index) number
31
+ * U.S. Social Security number (SSN)
32
+ * U.S. Individual Taxpayer Identification Number (ITIN)
33
+ * U.S. Employer Identification Number (EIN)
34
+
35
+ ## Installation
36
+
37
+ Add this line to your application's Gemfile:
38
+
39
+ ```ruby
40
+ gem 'social_security_number'
41
+ ```
42
+
43
+ And then execute:
44
+
45
+ $ bundle
46
+
47
+ Or install it yourself as:
48
+
49
+ $ gem install social_security_number
50
+
51
+ ## Usage
52
+ The country_code should always be a ISO 3166-1 alpha-2 (http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2).
53
+ ```ruby
54
+ # Options:
55
+ # :number => Civil number.
56
+ # :country_code => Some fallback code (eg. 'nl').
57
+ # :birth_date => Birth date (eg. 'yyyy-mm-dd').
58
+ # :gender => Gender (eg. 'famale').
59
+ ```
60
+ Validations
61
+ ```ruby
62
+ SocialSecurityNumber::Validator.new({number:'Some number', country_code:'nl'}).valid? # => true
63
+
64
+ SocialSecurityNumber::Validator.new({number:'Some number', country_code:'nl', birth_date: 'yyyy-mm-dd'}).valid? # => true
65
+
66
+ SocialSecurityNumber::Validator.new({number:'Some number', country_code:'nl'}) # => #<SocialSecurityNumber::Validator:0x000000021e2420 @civil_number="Some number", @country_code="NL", @birth_date=birth_date from civil number information, @gender=gender from civil number information>
67
+
68
+ civil_number = SocialSecurityNumber::Validator.new({number:'Some number', country_code:'nl'})
69
+ civil_number.valid? # => false
70
+ civil_number.error # => "birth date 1933-09-25 dont match 1933-09-24"
71
+ ```
72
+
73
+ ## Development
74
+
75
+ 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.
76
+
77
+ 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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
78
+
79
+ ## Contributing
80
+
81
+ 1. Fork it
82
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
83
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
84
+ 4. Push to the branch (`git push origin my-new-feature`)
85
+ 5. Create new Pull Request
86
+
87
+ Don't forget to add tests and run rspec before creating a pull request :)
88
+
89
+ See all contributors on https://github.com/samesystem/social_security_number/graphs/contributors .
90
+
91
+ ## License
92
+
93
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
94
+
95
+ ## Code of Conduct
96
+
97
+ Everyone interacting in the SocialSecurityNumber project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/samesystem/social_security_number/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'social_security_number'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,33 @@
1
+ module SocialSecurityNumber
2
+ # SocialSecurityNumber::Be validates Belgium National Register Number (Rijksregisternummer)
3
+ # https://en.wikipedia.org/wiki/National_identification_number#Belgium
4
+ class Be < Country
5
+ def validate
6
+ @error = if !check_by_regexp(REGEXP)
7
+ 'bad number format'
8
+ elsif !(1..997).member?(@individual.to_i)
9
+ 'individual number is invalid'
10
+ elsif !birth_date && @month.to_i != 0 && @day.to_i != 0
11
+ 'number birth date is invalid'
12
+ elsif !check_control_sum
13
+ 'number control sum invalid'
14
+ end
15
+ end
16
+
17
+ def gender
18
+ @gender = @individual.to_i.odd? ? :male : :female
19
+ end
20
+
21
+ private
22
+
23
+ REGEXP = /^(?<year>\d{2})[- .]?(?<month>\d{2})[- .]?(?<day>\d{2})[- .]?(?<indv>\d{3})[- .]?(?<ctrl>\d{2})$/
24
+
25
+ def check_control_sum
26
+ count_last_number == @control_number.to_i || count_last_number('2') == @control_number.to_i
27
+ end
28
+
29
+ def count_last_number(number = '0')
30
+ 97 - (("#{number}#{@year}#{@month}#{@day}#{@individual}").to_i % 97)
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,48 @@
1
+ module SocialSecurityNumber
2
+ # SocialSecurityNumber::Ca validates Canadian Social Insurance Numbers (SINs)
3
+ # The Social Insurance Number (SIN) is a 9-digit identifier issued to
4
+ # individuals for various government programs. SINs that begin with a 9 are
5
+ # issued to temporary workers who are neither Canadian citizens nor permanent
6
+ # residents.
7
+ # https://en.wikipedia.org/wiki/Social_Insurance_Number
8
+ class Ca < Country
9
+ def validate
10
+ @error = if !check_by_regexp(REGEXP)
11
+ 'bad number format'
12
+ elsif !check_number
13
+ 'number control sum invalid'
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ MODULUS = 10
20
+
21
+ CONTROLCIPHERS = [1, 2, 1, 2, 1, 2, 1, 2, 1].freeze
22
+
23
+ REGEXP = /^(?<f_nmr>\d{3})[- .]?(?<s_nmr>\d{3})[- .]?(?<l_nmr>\d{3})$/
24
+
25
+ def check_number
26
+ (count_number_sum % 10).zero?
27
+ end
28
+
29
+ def count_number_sum
30
+ digits = digit_number.split(//)
31
+ new_number = []
32
+ sum = 0
33
+ digits.each_with_index do |digit, i|
34
+ n = digit.to_i * CONTROLCIPHERS[i].to_i
35
+ new_number << if n > 9
36
+ n - 9
37
+ else
38
+ n
39
+ end
40
+ end
41
+
42
+ new_number.each do |digit|
43
+ sum += digit.to_i
44
+ end
45
+ sum
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,34 @@
1
+ module SocialSecurityNumber
2
+ # SocialSecurityNumber::Ch validates Swiss social security numbers
3
+ # https://en.wikipedia.org/wiki/National_identification_number#Switzerland
4
+ # https://de.wikipedia.org/wiki/Sozialversicherungsnummer#Versichertennummer
5
+ # http://www.sozialversicherungsnummer.ch/aufbau-neu.htm
6
+ class Ch < Country
7
+ def validate
8
+ @error = if !check_by_regexp(REGEXP)
9
+ 'bad number format'
10
+ elsif !check_control_sum
11
+ 'number control sum invalid'
12
+ end
13
+ end
14
+
15
+ private
16
+
17
+ MODULUS = 10
18
+
19
+ CONTROLCIPHERS = [1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3].freeze
20
+
21
+ INV_REGEXP = /(?<indv1>\d{4})[.]?(?<indv2>\d{4})[.]?(?<indv3>\d{1})/
22
+ REGEXP = /^(?<adress>756)[.]?#{INV_REGEXP}(?<ctrl>\d{1})$/
23
+
24
+ def check_control_sum
25
+ count_last_number.to_i == @control_number.to_i
26
+ end
27
+
28
+ def count_last_number
29
+ sum = calc_sum(digit_number[0..11], CONTROLCIPHERS)
30
+ modus = sum % MODULUS
31
+ modus > 0 ? 10 - modus : modus
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,42 @@
1
+ module SocialSecurityNumber
2
+ # SocialSecurityNumber::Cn validates Chinese Resident Identity Card Number
3
+ # https://en.wikipedia.org/wiki/Resident_Identity_Card#Identity_card_number
4
+ class Cn < Country
5
+ def validate
6
+ @error = if !check_digits
7
+ 'it is not number'
8
+ elsif !check_by_regexp(REGEXP)
9
+ 'bad number format'
10
+ elsif !birth_date
11
+ 'number birth date is invalid'
12
+ elsif !check_control_sum
13
+ 'number control sum invalid'
14
+ end
15
+ end
16
+
17
+ def year
18
+ @year = @parsed_civil_number[:year].to_i
19
+ end
20
+
21
+ def gender
22
+ @gender = @individual.to_i.odd? ? :male : :female
23
+ end
24
+
25
+ private
26
+
27
+ MODULUS = 11
28
+
29
+ CONTROLCIPHERS = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1].freeze
30
+
31
+ DATE_REGEXP = /(?<year>\d{4})[- .]?(?<month>\d{2})[- .]?(?<day>\d{2})/
32
+ REGEXP = /^(?<adr>\d{6})[- .]?#{DATE_REGEXP}[- .]?(?<indv>\d{3})[- .]?(?<ctrl>\d{1})$/
33
+
34
+ def check_control_sum
35
+ count_last_number.to_i == @control_number.to_i
36
+ end
37
+
38
+ def count_last_number
39
+ (12 - (calc_sum(@civil_number[0..16], CONTROLCIPHERS) % MODULUS)) % MODULUS
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,46 @@
1
+ module SocialSecurityNumber
2
+ # SocialSecurityNumber::Cz validates Czech birth numbers
3
+ # https://en.wikipedia.org/wiki/National_identification_number#Czech_Republic_and_Slovakia
4
+ # https://www.npmjs.com/package/rodnecislo
5
+ # http://lorenc.info/3MA381/overeni-spravnosti-rodneho-cisla.htm
6
+ class Cz < Country
7
+ def validate
8
+ @error = if !check_by_regexp(REGEXP)
9
+ 'bad number format'
10
+ elsif !birth_date
11
+ 'number birth date is invalid'
12
+ elsif !check_control_sum
13
+ 'number control sum invalid'
14
+ end
15
+ end
16
+
17
+ def gender
18
+ @gender = @parsed_civil_number[:month].to_i > 32 ? :famale : :male
19
+ end
20
+
21
+ def month
22
+ @month = (@parsed_civil_number[:month].to_i % 50) % 20
23
+ end
24
+
25
+ private
26
+
27
+ MODULUS = 11
28
+
29
+ DATE_REGEXP = /(?<year>\d{2})[- .]?(?<month>\d{2})[- .]?(?<day>\d{2})/
30
+ REGEXP = %r{^#{DATE_REGEXP}[- .\/]?(?<indv>\d{3})[- .]?(?<ctrl>\d{1})?$}
31
+
32
+ def check_control_sum
33
+ if @control_number.to_s != ''
34
+ count_last_number == @control_number.to_i ||
35
+ (count_last_number == 10 &&
36
+ @control_number.to_i.zero? && @year < 1986)
37
+ else
38
+ true
39
+ end
40
+ end
41
+
42
+ def count_last_number
43
+ digit_number[0..8].to_i % MODULUS
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,80 @@
1
+ module SocialSecurityNumber
2
+ # SocialSecurityNumber::De validates Germany Steuer-IdNr
3
+ # https://en.wikipedia.org/wiki/National_identification_number#Germany
4
+ class De < Country
5
+ def validate
6
+ @error = if !check_digits
7
+ 'it is not number'
8
+ elsif !check_length(11)
9
+ 'number should be length of 11'
10
+ elsif @civil_number[0].to_i.zero?
11
+ 'first number is invalid'
12
+ # elsif !check_digits_apperings
13
+ # 'first number is invalid'
14
+ elsif !check_control_sum
15
+ 'number control sum invalid'
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def check_control_sum
22
+ count_last_number == @civil_number[10].to_i
23
+ end
24
+
25
+ def count_last_number
26
+ sum = 0
27
+ product = 10
28
+
29
+ digits(@civil_number.to_s[0..9]).each_with_index do |digit, _i|
30
+ sum = (digit.to_i + product) % 10
31
+ sum = 10 if sum.zero?
32
+ product = (sum * 2) % 11
33
+ end
34
+ checksum = 11 - product
35
+ checksum = 0 if checksum == 10
36
+ checksum
37
+ end
38
+
39
+ def group_number(number)
40
+ out = []
41
+ digits(number).group_by(&:itself).map { |k, v| out[k.to_i] = v.count }
42
+ out
43
+ end
44
+
45
+ def digits(number)
46
+ number.split(//)
47
+ end
48
+
49
+ def check_digits_apperings
50
+ out = true
51
+
52
+ if validate_2015
53
+ out = false
54
+ elsif validate_2016
55
+ out = false
56
+ end
57
+ out
58
+ end
59
+
60
+ def count_dig(digits, number)
61
+ (digits.select { |count| count.to_i == number }).count
62
+ end
63
+
64
+ def validate_2015
65
+ # validate ids that are only valid since 2015
66
+ # one digit appears exactly twice and all other digits appear exactly once
67
+ digit = group_number(@civil_number.to_s[0..9]).compact
68
+ count_dig(digit, 2) != 1 && count_dig(digit, 1) != 8
69
+ end
70
+
71
+ def validate_2016
72
+ # validate ids that are only valid since 2016
73
+ # two digits appear zero times and one digit appears exactly three times
74
+ # and all other digits appear exactly once
75
+ digit = group_number(@civil_number.to_s[0..9]).compact
76
+ count_dig(digit, 2).zero? && count_dig(digit, 3) != 1 &&
77
+ count_dig(digit, 1) != 7
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,48 @@
1
+ module SocialSecurityNumber
2
+ # SocialSecurityNumber::Dk validates Denmark Personal Identification Number (Det Centrale Personregister (CPR))
3
+ # https://en.wikipedia.org/wiki/National_identification_number#Denmark
4
+ class Dk < Country
5
+ def validate
6
+ @error = if !check_by_regexp(REGEXP)
7
+ 'bad number format'
8
+ elsif !birth_date
9
+ 'number birth date is invalid'
10
+ elsif !valid_1968 && !valid_2007
11
+ 'control code invalid'
12
+ end
13
+ end
14
+
15
+ private
16
+
17
+ REGEXP = /^#{SHORT_DATE2_REGEXP}(?<divider>[\-]{0,1})(?<indv>\d{3})(?<gnd>\d{1})$/
18
+
19
+ MODULUS_1968 = 11
20
+ MODULUS_2007 = 6
21
+
22
+ CONTROLCIPHERS = [4, 3, 2, 7, 6, 5, 4, 3, 2, 1].freeze
23
+
24
+ FEMALE_SEEDS = {
25
+ 4 => 10..9994,
26
+ 2 => 8..9998,
27
+ 0 => 12..9996
28
+ }.freeze
29
+
30
+ MALE_SEEDS = {
31
+ 1 => 7..9997,
32
+ 3 => 9..9999,
33
+ 5 => 11..9995
34
+ }.freeze
35
+
36
+ def valid_1968
37
+ sum = calc_sum(digit_number, CONTROLCIPHERS)
38
+ (sum % MODULUS_1968).zero?
39
+ end
40
+
41
+ def valid_2007
42
+ control = digit_number[6, 4].to_i
43
+ rem2007 = control % MODULUS_2007
44
+ series = FEMALE_SEEDS[rem2007] || MALE_SEEDS[rem2007]
45
+ series.include?(control)
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,41 @@
1
+ module SocialSecurityNumber
2
+ # SocialSecurityNumber::Es validates Spain National Identity Document (Documento Nacional de Identidad (DNI)) number
3
+ # and NIE Number (Número de Identificación de Extranjeros, Foreigner's Identification Number)
4
+ # The NIE is an identification number for foreigners. It is a 9 digit number
5
+ # where the first digit is either X, Y or Z and last digit is a checksum letter.
6
+ # The DNI is a 9 digit number used to identify Spanish citizens. The last digit is a checksum letter.
7
+ # https://en.wikipedia.org/wiki/National_identification_number#Spain
8
+ class Es < Country
9
+ def validate
10
+ @error = if !validate_formats
11
+ 'bad number format'
12
+ elsif !dni_validation && !nie_validation
13
+ 'bad DNI, NIE number'
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ DNI_REGEXP = /^(?<individual>\d{8})[- ]?(?<ctrl>[A-Z])$/
20
+ NIE_REGEXP = /^(?<first_letter>[XYZ])[- ]?(?<individual>\d{7})[- ]?(?<ctrl>[A-Z])$/
21
+
22
+ def validate_formats
23
+ check_by_regexp(DNI_REGEXP) || check_by_regexp(NIE_REGEXP)
24
+ end
25
+
26
+ def dni_validation
27
+ count_last_simbol(@civil_number[0..7]) == @civil_number[-1]
28
+ end
29
+
30
+ def nie_validation
31
+ remap = %W[X Y Z]
32
+ number = "#{remap.index(@civil_number[0])}#{@civil_number[1..7]}"
33
+ count_last_simbol(number) == @civil_number[-1]
34
+ end
35
+
36
+ def count_last_simbol(number)
37
+ letters = "TRWAGMYFPDXBNJZSQVHLCKE"
38
+ letters[number.to_i % 23].chr
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,30 @@
1
+ module SocialSecurityNumber
2
+ # SocialSecurityNumber::Fi validates Finland Personal Identity Code (Finnish: henkilötunnus (HETU))
3
+ # https://en.wikipedia.org/wiki/National_identification_number#Finland
4
+ class Fi < Country
5
+ def validate
6
+ @error = if !check_by_regexp(REGEXP)
7
+ 'bad number format'
8
+ elsif !check_control_simbol
9
+ 'number control sum invalid'
10
+ end
11
+ end
12
+
13
+ private
14
+
15
+ MODULUS = 31
16
+
17
+ CONTROL_REGEXP = /(?<ctrl>[0-9ABCDEFHJKLMNPRSTUVWXY])/
18
+ REGEXP = /^#{SHORT_DATE2_REGEXP}(?<century>[-+A])(?<indv>\d{3})#{CONTROL_REGEXP}$/
19
+
20
+ def check_control_simbol
21
+ count_last_simbol.to_s == @control_number.to_s
22
+ end
23
+
24
+ def count_last_simbol
25
+ number = "#{@civil_number[0..5]}#{@individual}"
26
+ last_number = number.to_i % MODULUS
27
+ '0123456789ABCDEFHJKLMNPRSTUVWXY'[last_number]
28
+ end
29
+ end
30
+ end