Class: NationalIdsValidator
- Inherits:
-
Object
- Object
- NationalIdsValidator
- Defined in:
- lib/national_ids_validator.rb
Constant Summary collapse
- SUPPORTED_COUNTRY_CODES =
%w(PL NO)
Class Method Summary collapse
Instance Method Summary collapse
-
#gender ⇒ Object
0 - man, 1 - woman.
-
#initialize(value, country_code) ⇒ NationalIdsValidator
constructor
A new instance of NationalIdsValidator.
- #number_data ⇒ Object
- #valid? ⇒ Boolean
- #valid_check_digits? ⇒ Boolean
- #valid_length? ⇒ Boolean
Constructor Details
#initialize(value, country_code) ⇒ NationalIdsValidator
Returns a new instance of NationalIdsValidator.
13 14 15 16 17 18 19 |
# File 'lib/national_ids_validator.rb', line 13 def initialize(value,country_code) @country_code = country_code.to_s.strip.gsub(/\s+/, '').upcase @value = value.to_s.strip.gsub(/\s+/, '').upcase unless self.class::SUPPORTED_COUNTRY_CODES.include?(@country_code) raise RuntimeError.new("Unexpected country code '#{country_code}' that is not yet supported") end end |
Class Method Details
.specifications ⇒ Object
9 10 11 |
# File 'lib/national_ids_validator.rb', line 9 def self.specifications @@rules ||= YAML.load_file(File.("national-ids-validator/rules.yml", File.dirname(__FILE__))) end |
.valid?(value, code) ⇒ Boolean
21 22 23 |
# File 'lib/national_ids_validator.rb', line 21 def self.valid?(value,code) new(value,code).valid? end |
Instance Method Details
#gender ⇒ Object
0 - man, 1 - woman
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/national_ids_validator.rb', line 83 def gender #0 - man, 1 - woman if self.valid? case @country_code when "NO" number_data['individual'][2] % 2 == 0 ? 1 : 0 when "PL" number_data['individual'][3] % 2 == 0 ? 1 : 0 end end end |
#number_data ⇒ Object
79 80 81 |
# File 'lib/national_ids_validator.rb', line 79 def number_data @number_data ||= Regexp.new("^#{specification['regexp']}$").match(@value) if specification end |
#valid? ⇒ Boolean
25 26 27 |
# File 'lib/national_ids_validator.rb', line 25 def valid? valid_length? && valid_check_digits? end |
#valid_check_digits? ⇒ Boolean
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/national_ids_validator.rb', line 33 def valid_check_digits? status = false v = self.number_data # binding.pry case @country_code when "NO" #http://no.wikipedia.org/wiki/F%C3%B8dselsnummer #first control digit weights1 = specification['weights1'] first_sum = 0 0.upto(@value.length - 3).each do |i| first_sum += @value[i].to_i*weights1[i].to_i end first_digit = 11 - (first_sum % 11) #second control digit weights2 = specification['weights2'] second_sum = 0 0.upto(@value.length - 2).each do |i| second_sum += @value[i].to_i*weights2[i].to_i end second_digit = 11 - (second_sum % 11) #check digits calculated = [first_digit, second_digit].map!{|k| k == 11 ? 0 : k} control = [v["control"][0].to_i, v["control"][1].to_i] if calculated[0] == control[0] && calculated[1] == control[1] status = true end # binding.pry when "PL" weights = specification['weights'] sum = 0 0.upto(@value.length - 2).each do |i| sum += @value[i].to_i*weights[i].to_i end first_digit = 10 - (sum % 10) calculated = first_digit == 10 ? 0 : first_digit control = v["control"][0].to_i if calculated == control status = true end end return status end |
#valid_length? ⇒ Boolean
29 30 31 |
# File 'lib/national_ids_validator.rb', line 29 def valid_length? !!specification && specification['length'] == @value.length end |