class ApraService::Grantee

Constants

CHECK_DIGITS
SSN_REGEX

Attributes

address[RW]
first_names[RW]
last_name[RW]
ssn[RW]
zip[RW]

Public Class Methods

from_hash(hash) click to toggle source
# File lib/apralib/grantee.rb, line 24
def self.from_hash(hash)
  grantee = Grantee.new
  grantee.address = hash[:apurahansaajanosoite]
  grantee.zip = hash[:apurahansaajanpostinumero]
  grantee.first_names = [hash[:etunimi1]]
  grantee.first_names << hash[:etunimi2] if hash[:etunimi2]
  grantee.first_names << hash[:etunimi3] if hash[:etunimi3]
  grantee.ssn = hash[:henkilotunnus]
  grantee.last_name = hash[:sukunimi]
  grantee
end
verify_ssn(ssn) click to toggle source
# File lib/apralib/grantee.rb, line 52
def self.verify_ssn(ssn)
  begin
    verify_ssn! ssn
      true
  rescue RuntimeError => e
    puts e.message
    false
  end
end
verify_ssn!(ssn) click to toggle source
# File lib/apralib/grantee.rb, line 45
def self.verify_ssn!(ssn)
  raise RuntimeError, 'The Social Security Number is not in the correct format' unless ssn =~ SSN_REGEX
  big_number = "#{$1}#{$3}".to_i
  check = CHECK_DIGITS[big_number % 31]
  raise RuntimeError, "The check digit does not match, expected #{check}, got #{$4}" unless check == $4
end

Public Instance Methods

ssn=(ssn) click to toggle source
# File lib/apralib/grantee.rb, line 36
def ssn=(ssn)
  if ssn
    Grantee.verify_ssn!(ssn.upcase)
    self.instance_variable_set('@ssn', ssn.upcase)
  else
    self.instance_variable_set('@ssn', nil)
  end
end
to_hash() click to toggle source
# File lib/apralib/grantee.rb, line 10
def to_hash
  result = {}
  result[:apurahansaajanosoite] = address if address
  result[:apurahansaajanpostinumero] = zip if zip
  first_names.split(' ')[0..2].each_with_index do |name, index|
    result["etunimi#{index + 1}".to_sym] = name
  end
  result[:henkilotunnus] = ssn if ssn
  result[:sukunimi] = last_name

  result

end