# frozen_string_literal: true module Roqua module Healthy module A19 class AddressParser attr_reader :message def initialize(message) @message = message end def address_type return nil unless record record.fetch('PID.11.7') end def street return nil if record.blank? || record.fetch('PID.11.1').blank? record.fetch('PID.11.1').fetch('PID.11.1.1') end def city return nil unless record record.fetch('PID.11.3') end def zipcode return nil unless record record.fetch('PID.11.5') end def country return nil unless record record.fetch('PID.11.6') end def record # PID.11.7 address types: # M: Mailing # P: Permanent # H: Home # L: Legal address @record ||= get_address_of_type(message, 'M') || get_address_of_type(message, 'P') || get_address_of_type(message, 'H') || get_address_of_type(message, 'L') end private def get_address_of_type(message, type) message.fetch('PID').fetch('PID.11').find do |record| record.fetch('PID.11.7', :unknown_type_of_address_record) == type end end end end end end