# frozen_string_literal: true module Roqua module Healthy module A19 class PhoneParser attr_reader :message def initialize(message) @message = message end # this is a heuristic to pick likely dutch cell phone numbers from hl7 messages def to_s pid13 = message.fetch('PID').fetch('PID.13') # prefer PRN (Primary Residence Number) that contains a cell phone number phone_cell_record = pid13.find do |record| phone_cell_number?(record.fetch('PID.13.1', '') || '') && record.fetch('PID.13.2', :unknown_type_of_phone_record) == 'PRN' end # otherwise choose the first occuring cell phone number phone_cell_record ||= pid13.find do |record| phone_cell_number?(record.fetch('PID.13.1', '') || '') end # any number for which phone_cell_number? returns false is ignored strip_non_phone_number_characters(phone_cell_record.fetch('PID.13.1')) if phone_cell_record.present? end private def strip_non_phone_number_characters(number) Roqua::Healthy::A19::PhoneValidator.strip_non_phone_number_characters number end def phone_cell_number?(number) Roqua::Healthy::A19::PhoneValidator.phone_cell_number? number end end end end end