Class: MRZ::TD1Parser

Inherits:
BaseParser show all
Defined in:
lib/mrz/td1_parser.rb

Constant Summary collapse

FORMAT_ONE =
/\A(.{2})(.{3})(.{9})(\d)(.{15})\z/
FORMAT_TWO =
/\A(\d{6})(\d)(.)(\d{6})(\d)(.{3})(.{11})(\d)\z/
FORMAT_THREE =
/\A([^<]+)<<(.+)\z/

Constants inherited from BaseParser

BaseParser::EMPTY_SPACE, BaseParser::SPECIAL_CHAR, BaseParser::WHITESPACE

Instance Method Summary collapse

Constructor Details

#initialize(code_ary) ⇒ TD1Parser

Returns a new instance of TD1Parser.



7
8
9
10
11
12
# File 'lib/mrz/td1_parser.rb', line 7

def initialize(code_ary)
  @code  = code_ary
  @one   = code_ary[0]
  @two   = code_ary[1]
  @three = code_ary[2]
end

Instance Method Details

#parseObject



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
# File 'lib/mrz/td1_parser.rb', line 14

def parse
  if @code.size != 3
    raise MRZ::InvalidFormatError, "td1 requires 3 mrz lines"
  end

  line_one_matches = FORMAT_ONE.match(@one)
  line_two_matches = FORMAT_TWO.match(@two)
  line_three_matches = FORMAT_THREE.match(@three)

  if line_one_matches.nil?
    raise MRZ::InvalidFormatError, "td1 first line does not match the required format"
  end

  if line_two_matches.nil?
    raise MRZ::InvalidFormatError, "td1 second line does not match the required format"
  end

  if @three.size != 30 || line_three_matches.nil?
    raise MRZ::InvalidFormatError, "td1 third line does not match the required format"
  end

  MRZ::Result.new(
    birth_date: line_two_matches[1],
    birth_date_check_digit: line_two_matches[2],
    composite_check_digit: line_two_matches[8],
    document_code: special_char_to_empty_space(line_one_matches[1]),
    document_number: special_char_to_empty_space(line_one_matches[3]),
    document_number_check_digit: line_one_matches[4],
    expiration_date: line_two_matches[4],
    expiration_date_check_digit: line_two_matches[5],
    first_name: special_char_to_white_space(line_three_matches[2]).strip,
    issuing_state: special_char_to_empty_space(line_one_matches[2]),
    last_name: line_three_matches[1],
    nationality: special_char_to_empty_space(line_two_matches[6]),
    optional1: special_char_to_empty_space(line_one_matches[5]),
    optional2: special_char_to_empty_space(line_two_matches[7]),
    sex: special_char_to_empty_space(line_two_matches[3]),
    type: :td1,
  )
end