Sha256: ce0d2539c1f2c6e70535ed5ce8f622e0aa8ba76a9a1cfb034c8bad7ae2ed0cf1

Contents?: true

Size: 1.56 KB

Versions: 14

Compression:

Stored size: 1.56 KB

Contents

# frozen_string_literal: true

module FriendlyShipping
  module Services
    class Usps
      class ParseAddressValidationResponse
        class << self
          # Parse a response from USPS' address validation API
          #
          # @param [FriendlyShipping::Request] request The request that was used to obtain this Response
          # @param [FriendlyShipping::Response] response The response that USPS returned
          # @return [Result<FriendlyShipping::AddressValidationResult>]
          def call(request:, response:)
            # Filter out error responses and directly return a failure
            parsing_result = ParseXMLResponse.call(
              request: request,
              response: response,
              expected_root_tag: 'AddressValidateResponse'
            )
            parsing_result.fmap do |xml|
              address = xml.root.at('Address')
              suggestions = [
                Physical::Location.new(
                  address1: address&.at('Address2')&.text, # USPS swaps Address1 and Address2 in the response
                  address2: address&.at('Address1')&.text,
                  city: address&.at('City')&.text,
                  region: address&.at('State')&.text,
                  zip: address&.at('Zip5')&.text,
                  country: 'US'
                )
              ]
              FriendlyShipping::ApiResult.new(
                suggestions,
                original_request: request,
                original_response: response
              )
            end
          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/usps/parse_address_validation_response.rb
friendly_shipping-0.8.1 lib/friendly_shipping/services/usps/parse_address_validation_response.rb
friendly_shipping-0.8.0 lib/friendly_shipping/services/usps/parse_address_validation_response.rb
friendly_shipping-0.7.3 lib/friendly_shipping/services/usps/parse_address_validation_response.rb
friendly_shipping-0.7.2 lib/friendly_shipping/services/usps/parse_address_validation_response.rb
friendly_shipping-0.7.1 lib/friendly_shipping/services/usps/parse_address_validation_response.rb
friendly_shipping-0.7.0 lib/friendly_shipping/services/usps/parse_address_validation_response.rb
friendly_shipping-0.6.5 lib/friendly_shipping/services/usps/parse_address_validation_response.rb
friendly_shipping-0.6.4 lib/friendly_shipping/services/usps/parse_address_validation_response.rb
friendly_shipping-0.6.3 lib/friendly_shipping/services/usps/parse_address_validation_response.rb
friendly_shipping-0.6.2 lib/friendly_shipping/services/usps/parse_address_validation_response.rb
friendly_shipping-0.6.1 lib/friendly_shipping/services/usps/parse_address_validation_response.rb
friendly_shipping-0.6.0 lib/friendly_shipping/services/usps/parse_address_validation_response.rb
friendly_shipping-0.5.3 lib/friendly_shipping/services/usps/parse_address_validation_response.rb