Sha256: 2b3aecc04ac3adc3fc8ef18566310ab667985dc3fd1f197acb844f93c3494f76

Contents?: true

Size: 1.39 KB

Versions: 14

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

module FriendlyShipping
  module Services
    class Usps
      class ParseCityStateLookupResponse
        class << self
          # Parse a response from USPS' city/state lookup 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: 'CityStateLookupResponse'
            )
            parsing_result.fmap do |xml|
              address = xml.root.at('ZipCode')
              suggestions = [
                Physical::Location.new(
                  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_city_state_lookup_response.rb
friendly_shipping-0.8.1 lib/friendly_shipping/services/usps/parse_city_state_lookup_response.rb
friendly_shipping-0.8.0 lib/friendly_shipping/services/usps/parse_city_state_lookup_response.rb
friendly_shipping-0.7.3 lib/friendly_shipping/services/usps/parse_city_state_lookup_response.rb
friendly_shipping-0.7.2 lib/friendly_shipping/services/usps/parse_city_state_lookup_response.rb
friendly_shipping-0.7.1 lib/friendly_shipping/services/usps/parse_city_state_lookup_response.rb
friendly_shipping-0.7.0 lib/friendly_shipping/services/usps/parse_city_state_lookup_response.rb
friendly_shipping-0.6.5 lib/friendly_shipping/services/usps/parse_city_state_lookup_response.rb
friendly_shipping-0.6.4 lib/friendly_shipping/services/usps/parse_city_state_lookup_response.rb
friendly_shipping-0.6.3 lib/friendly_shipping/services/usps/parse_city_state_lookup_response.rb
friendly_shipping-0.6.2 lib/friendly_shipping/services/usps/parse_city_state_lookup_response.rb
friendly_shipping-0.6.1 lib/friendly_shipping/services/usps/parse_city_state_lookup_response.rb
friendly_shipping-0.6.0 lib/friendly_shipping/services/usps/parse_city_state_lookup_response.rb
friendly_shipping-0.5.3 lib/friendly_shipping/services/usps/parse_city_state_lookup_response.rb