Class: ApraService::Grantee

Inherits:
Object
  • Object
show all
Defined in:
lib/apralib/grantee.rb

Constant Summary collapse

SSN_REGEX =
/(\d{6})([-+A])(\d{3})([0-9ABCDEFHJKLMNPRSTUVWXY])/
CHECK_DIGITS =
%w(0 1 2 3 4 5 6 7 8 9 A B C D E F H J K L M N P R S T U V W X Y)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#addressObject

Returns the value of attribute address.



8
9
10
# File 'lib/apralib/grantee.rb', line 8

def address
  @address
end

#first_namesObject

Returns the value of attribute first_names.



8
9
10
# File 'lib/apralib/grantee.rb', line 8

def first_names
  @first_names
end

#last_nameObject

Returns the value of attribute last_name.



8
9
10
# File 'lib/apralib/grantee.rb', line 8

def last_name
  @last_name
end

#ssnObject

Returns the value of attribute ssn.



8
9
10
# File 'lib/apralib/grantee.rb', line 8

def ssn
  @ssn
end

#zipObject

Returns the value of attribute zip.



8
9
10
# File 'lib/apralib/grantee.rb', line 8

def zip
  @zip
end

Class Method Details

.from_hash(hash) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# 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) ⇒ Object



52
53
54
55
56
57
58
59
60
# 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) ⇒ Object

Raises:

  • (RuntimeError)


45
46
47
48
49
50
# 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

Instance Method Details

#to_hashObject



10
11
12
13
14
15
16
17
18
19
20
21
22
# 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