require 'identification/document' require 'identification/parser/passport' module Identification # Represents a passport with an MRZ or fields that create an MRZ. class Passport < Document attr_accessor :mrz_line_1, :mrz_line_2, :issuing_state, :first_names, :passport_number, :nationality, :expiry_date, :personal_number # Creates an instance of a passport. # Will automatically parse if both MRZ lines are given, and will automatically generate if all necessary fields are set. # (all except personal number which is rarely used) # # @param [Hash] details of the paramaters # @option opts [String] :mrz_line_1 first line of the machine readable zone (only set for parsing) # @option opts [String] :mrz_line_2 second line of the machine readable zone (only set for parsing) # @option opts [String] :issuing_state : issuing state of the individual's passport (only set for generating) # @option opts [String] :first_names : array of first names on the passport (only set for generating) # @option opts [String] :last_name surname of the individual (only set for generating) # @option opts [String] :date_of_birth date of birth of the individual (only set for generating) # @option opts [String] :gender gender of the individual (only set for generating) # @option opts [String] :passport_number : passport number of the individual (only set for generating) # @option opts [String] :nationality : nationality of the individual (only set for generating) # @option opts [String] :expiry_date : expiry date of the passport, given as a date object (only set for generating) # @option opts [String] :personal_number : personal number of the individual - optional (only set for generating) def initialize(params = {}) super(params) @mrz_line_1 = params[:mrz_line_1] @mrz_line_2 = params[:mrz_line_2] @issuing_state = params[:issuing_state] @first_names = params[:first_names] @passport_number = params[:passport_number] @nationality = params[:nationality] @expiry_date = params[:expiry_date] @personal_number = params[:personal_number] parse if params.key?(:mrz_line_1) && params.key?(:mrz_line_2) end # Parses MRZs and updates the instance variables. # Returns true if it was successfuly parsed, false if there was an issue. # # @return [Boolean] whether the passport is valid or not. # @raise [RuntimeError] if there is no MRZ set in the instance. def parse if !@mrz_line_1.nil? && !@mrz_line_2.nil? result = Parser::Passport.parse(@mrz_line_1, @mrz_line_2) @issuing_state = result[:issuing_state] if result.key?(:issuing_state) @last_name = result[:last_name] if result.key?(:last_name) @first_names = result[:first_names] if result.key?(:first_names) @passport_number = result[:passport_number] if result.key?(:passport_number) @nationality = result[:nationality] if result.key?(:nationality) @date_of_birth = result[:date_of_birth] if result.key?(:date_of_birth) @gender = result[:gender] if result.key?(:gender) @expiry_date = result[:expiry_date] if result.key?(:expiry_date) @personal_number = result[:personal_number] if result.key?(:personal_number) @validity = result[:validity] return true if @validity else fail 'No MRZ set to parse.' end false end end end