Advertisement
Guest User

Untitled

a guest
Apr 11th, 2015
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.96 KB | None | 0 0
  1. require 'date.rb'
  2.  
  3. class SwedishSocialSecurityNumber
  4.     attr_reader :socialSecurityNumber, :year, :month, :day, :code, :isOver100
  5.    
  6.     def initialize(socialSecurityNumber)
  7.         @socialSecurityNumber = socialSecurityNumber
  8.         raise ArgumentError, "The format has to be YYMMDD-XXXX or YYMMDD+XXXX" unless right_pattern?
  9.        
  10.         @isOver100 = false;
  11.         @year = @socialSecurityNumber[0,2].to_i
  12.         @month = @socialSecurityNumber[2,2].to_i
  13.         @day = @socialSecurityNumber[4,2].to_i
  14.         @isOver100 = true if @socialSecurityNumber[6,1] == "+"
  15.         @code = @socialSecurityNumber[7,4].to_i
  16.        
  17.         raise ArgumentError, "The date is invalid or fictional" unless date_valid?
  18.         raise ArgumentError, "The control number is invalid" unless check_sum == @socialSecurityNumber[10,1].to_i
  19.     end
  20.    
  21.     def check_sum
  22.         variegated = 2;
  23.         temp = 0;
  24.         list = Array.new
  25.        
  26.         0.upto(@socialSecurityNumber.length - 2) { |position|
  27.             next if position == 6
  28.             temp = @socialSecurityNumber[position].to_i
  29.             temp = temp * variegated
  30.             temp.to_s.split("").each { |c|
  31.                 list.push(c.to_i)
  32.             }  
  33.            
  34.             variegated = variegated == 2 ? 1 : 2
  35.         }
  36.        
  37.         sum = 0
  38.         list.each { |i|
  39.             sum = sum + i
  40.         }              
  41.        
  42.         return (10 - (sum % 10))
  43.     end
  44.    
  45.     def full_year
  46.         current_year = Time.new.year
  47.         temp_str = ""
  48.         if @year < 10
  49.             temp_str = current_year.to_s[0,2] + "0" + @year.to_s
  50.         else
  51.             temp_str = current_year.to_s[0,2] + @year.to_s
  52.         end
  53.        
  54.         return temp_str.to_i - 100 if @isOver100
  55.         return temp_str.to_i - 100 if temp_str.to_i > @year
  56.        
  57.         return temp_str.to_i
  58.     end
  59.    
  60.     def female?
  61.         (@socialSecurityNumber[9,1].to_i % 2) == 0
  62.     end
  63.    
  64.     def male?
  65.         !female?
  66.     end
  67.    
  68.     private
  69.     def right_pattern?
  70.         !/^[0-9]{2}[0-1][0-9][0-3][0-9][-|+][0-9]{4}$/.match(@socialSecurityNumber).nil?
  71.     end
  72.    
  73.     def date_valid?
  74.         d = nil
  75.         begin
  76.             d = Date.new(full_year, @month, @day)
  77.         rescue
  78.             return false
  79.         end
  80.  
  81.         current_date = Time.new
  82.         return false if d > current_date.to_date
  83.         true
  84.     end
  85. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement