Class: Identification::Parser::Passport

Inherits:
Parser
  • Object
show all
Defined in:
lib/identification/parser/passport.rb

Overview

Represents a passport parser.

Class Method Summary collapse

Class Method Details

.parse(mrz_line_1, mrz_line_2) ⇒ Object



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
# File 'lib/identification/parser/passport.rb', line 9

def self.parse(mrz_line_1, mrz_line_2)
  return { validity: false }  if mrz_line_1.length != 44 || mrz_line_2.length != 44

  doc_data = Hash.new(10)

  # Document will be valid unless an error occurs.
  doc_data[:validity] = true

  # Document may not be valid, but still, continue parsing.
  doc_data[:validity] = false unless mrz_check_line_checksum(mrz_line_2)

  # Read the first line without chevrons
  split = mrz_line_1.split(/<+/)

  doc_data[:date_of_birth] = yymmdd_to_ruby_date(mrz_line_2[13...19])
  doc_data[:expiry_date] = yymmdd_to_ruby_date(mrz_line_2[21...27])

  # In case of an invalid date...
  if doc_data['date_of_birth'] == false || doc_data['expiry_date'] == false
    doc_data[:validity] = 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(/<+/, '')

  doc_data
end