lib/friendly_shipping/services/usps/parse_address_validation_response.rb
# 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>]
Parse a response from USPS' address validation API
Parse a response from USPS ' address validation API
def call(request:, response:)
            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, 
                  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
def call ( request : , response : ) 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 , 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
Parse a response from USPS' address validation API
Parse a response from USPS ' address validation API
# Filter out error responses and directly return a failure # USPS swaps Address1 and Address2 in the response
None
Result<FriendlyShipping::AddressValidationResult>
request
<not_specific>
response
<not_specific>
<not_specific>
response The response that USPS returned
response The response that USPS returned