Sha256: 85ea0ba2cd3840683ba578704874634ff592dd592cbeadb356c6f8a367298255

Contents?: true

Size: 1.52 KB

Versions: 2

Compression:

Stored size: 1.52 KB

Contents

# 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
          # prefer PRN (Primary Residence Number) that contains a cell phone number
          # otherwise choose the first occuring cell phone number
          # any number for which phone_cell_number? returns false is ignored
          phone_cell_record = first_matching_prn_record || first_matching_record

          strip_non_phone_number_characters(phone_cell_record.fetch('PID.13.1')) if phone_cell_record.present?
        end

        private

        def pid13
          @pid13 ||= message.dig('PID', 'PID.13') || []
        end

        def first_matching_prn_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
        end

        def first_matching_record
          pid13.find do |record|
            phone_cell_number?(record.fetch('PID.13.1', '') || '')
          end
        end

        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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
roqua-healthy-1.6.0 lib/roqua/healthy/a19/phone_parser.rb
roqua-healthy-1.5.13 lib/roqua/healthy/a19/phone_parser.rb