Class: MRZ::Check

Inherits:
Object
  • Object
show all
Defined in:
lib/verified/parser/mrz/check.rb

Instance Method Summary collapse

Constructor Details

#initializeCheck

Returns a new instance of Check.



3
4
5
6
# File 'lib/verified/parser/mrz/check.rb', line 3

def initialize
  @@digit_checker = CheckDigit.new
  @@date_converter = YYDate.new
end

Instance Method Details

#check(mrz_line_1, mrz_line_2) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
# File 'lib/verified/parser/mrz/check.rb', line 8

def check(mrz_line_1, mrz_line_2)

  if mrz_line_1.length != 44 && mrz_line_2.length != 44
    return false
  end

  # Grabbing the MRZ's check digits
  doc_check = Array.new
  doc_check[0] = mrz_line_2[9].to_s
  doc_check[1] = mrz_line_2[19].to_s
  doc_check[2] = mrz_line_2[27].to_s
  doc_check[3] = mrz_line_2[42].to_s
  doc_check[4] = mrz_line_2[43].to_s

  # Calculating our own check digits...
  our_check = Array.new
  our_check[0] = @@digit_checker.check_calc(mrz_line_2[0...9])
  our_check[1] = @@digit_checker.check_calc(mrz_line_2[13...19])
  our_check[2] = @@digit_checker.check_calc(mrz_line_2[21...27])
  our_check[3] = @@digit_checker.check_calc(mrz_line_2[28...42])
  our_check[4]  = @@digit_checker.check_calc(mrz_line_2[0...10]+mrz_line_2[13...20]+mrz_line_2[21...43])

  # The 4th check digit can be either > or 0, we always return 0 from our CheckDigit calc.
  if our_check[3] == "0" && doc_check[3] == "<"
    our_check[3] = "<"
  end

  if doc_check.uniq.sort == our_check.uniq.sort
    # Read the first line without chevrons
    split = mrz_line_1.split(/<+/)

    doc_data = Hash.new(9)
    doc_data["date_of_birth"] = @@date_converter.convert_to_date(mrz_line_2[13...19])
    doc_data["expiry_date"] = @@date_converter.convert_to_date(mrz_line_2[21...27])
    
    # Incase of an invalid date...
    if doc_data["date_of_birth"] == false || doc_data["expiry_date"] == false
      return false
    end
    
    doc_data["issuing_state"] = mrz_line_1[2...5].sub(/<+/, '')
    doc_data["last_name"] = split[1][3..-1]
    doc_data["first_names"] = split[2..-1]
    doc_data["passport_number"] = mrz_line_2[0...9]
    doc_data["nationality"] = mrz_line_2[10...13].sub(/<+/, '')
    doc_data["gender"] = mrz_line_2[20].sub(/<+/, '')
    doc_data["personal_number"] = mrz_line_2[28...42].sub(/<+/, '')
		
    return doc_data
  end

  return false
end