Sha256: e41898a48d1941c2d998b85f87ab8a1432588cdfcec9cbddeb1f997381664840

Contents?: true

Size: 1.89 KB

Versions: 14

Compression:

Stored size: 1.89 KB

Contents

# frozen_string_literal: true

module FriendlyShipping
  module Services
    class Ups
      class ParseAddressValidationResponse
        extend Dry::Monads::Result::Mixin

        def self.call(request:, response:)
          parsing_result = ParseXMLResponse.call(
            request: request,
            response: response,
            expected_root_tag: 'AddressValidationResponse'
          )
          parsing_result.bind do |xml|
            if xml.at('NoCandidatesIndicator')
              Failure(
                FriendlyShipping::ApiFailure.new(
                  'Address is probably invalid. No similar valid addresses found.',
                  original_request: request,
                  original_response: response
                )
              )
            else
              Success(
                FriendlyShipping::ApiResult.new(
                  build_suggestions(xml),
                  original_request: request,
                  original_response: response
                )
              )
            end
          end
        end

        def self.build_suggestions(xml)
          xml.xpath('//AddressKeyFormat').map do |address_fragment|
            Physical::Location.new(
              address1: address_fragment.xpath('AddressLine[1]')[0]&.text,
              address2: address_fragment.xpath('AddressLine[2]')[0]&.text,
              company_name: address_fragment.at('ConsigneeName')&.text,
              city: address_fragment.at('PoliticalDivision2')&.text,
              region: address_fragment.at('PoliticalDivision1')&.text,
              country: address_fragment.at('CountryCode')&.text,
              zip: "#{address_fragment.at('PostcodePrimaryLow')&.text}-#{address_fragment.at('PostcodeExtendedLow')&.text}",
              address_type: address_fragment.at('AddressClassification/Description')&.text&.downcase
            )
          end
        end
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
friendly_shipping-0.9.0 lib/friendly_shipping/services/ups/parse_address_validation_response.rb
friendly_shipping-0.8.1 lib/friendly_shipping/services/ups/parse_address_validation_response.rb
friendly_shipping-0.8.0 lib/friendly_shipping/services/ups/parse_address_validation_response.rb
friendly_shipping-0.7.3 lib/friendly_shipping/services/ups/parse_address_validation_response.rb
friendly_shipping-0.7.2 lib/friendly_shipping/services/ups/parse_address_validation_response.rb
friendly_shipping-0.7.1 lib/friendly_shipping/services/ups/parse_address_validation_response.rb
friendly_shipping-0.7.0 lib/friendly_shipping/services/ups/parse_address_validation_response.rb
friendly_shipping-0.6.5 lib/friendly_shipping/services/ups/parse_address_validation_response.rb
friendly_shipping-0.6.4 lib/friendly_shipping/services/ups/parse_address_validation_response.rb
friendly_shipping-0.6.3 lib/friendly_shipping/services/ups/parse_address_validation_response.rb
friendly_shipping-0.6.2 lib/friendly_shipping/services/ups/parse_address_validation_response.rb
friendly_shipping-0.6.1 lib/friendly_shipping/services/ups/parse_address_validation_response.rb
friendly_shipping-0.6.0 lib/friendly_shipping/services/ups/parse_address_validation_response.rb
friendly_shipping-0.5.3 lib/friendly_shipping/services/ups/parse_address_validation_response.rb