require "identification/parser/parser" module Identification module Parser # Represents a DVLA parser. # # @private class DVLA < Parser def self.parse(driver_number) # Remove whitespace driver_number.gsub!(/\s+/, '') return { validity: false } if driver_number.length != 18 && driver_number.length != 16 doc_data = Hash.new(6) # 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 dvla_check_line_checksum(driver_number) doc_data[:last_name] = driver_number[0...5].sub(/9+/, '') doc_data[:first_initials] = driver_number[11...13].sub(/9+/, '') if driver_number.length == 18 doc_data[:license_numbers] = driver_number[16...18] else doc_data[:license_numbers] = '00' end doc_data[:date_of_birth] = yymmdd_to_ruby_date(driver_number[5...11]) if driver_number[6] == '5' || driver_number == '6' doc_data[:gender] = 'F' else doc_data[:gender] = 'M' end # In case of an invalid date... doc_data[:validity] = false if doc_data[:date_of_birth] == false return doc_data end def self.yymmdd_to_ruby_date(input_date) # Change the month back (if gender is set to F) input_date[1] = '0' if input_date[1] == '5' input_date[1] = '1' if input_date[1] == '6' # Put to YYMMDD format new_date = String.new(input_date) new_date[1] = input_date[5] new_date[2] = input_date[1] new_date[3] = input_date[2] new_date[4] = input_date[3] new_date[5] = input_date[4] super(new_date) end def self.dvla_check_line_checksum(driver_number) # Awaiting response from DVLA... # Boilerplate content for now... their_check_digit = driver_number[13...16] # Returning true until I know how to calculate check digits true end private_class_method :dvla_check_line_checksum end end end