Class: FriendlyShipping::Services::Usps::ParseAddressValidationResponse
- Inherits:
-
Object
- Object
- FriendlyShipping::Services::Usps::ParseAddressValidationResponse
- Defined in:
- lib/friendly_shipping/services/usps/parse_address_validation_response.rb
Class Method Summary collapse
-
.call(request:, response:) ⇒ Result<FriendlyShipping::AddressValidationResult>
Parse a response from USPS’ address validation API.
Class Method Details
.call(request:, response:) ⇒ Result<FriendlyShipping::AddressValidationResult>
Parse a response from USPS’ address validation API
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/friendly_shipping/services/usps/parse_address_validation_response.rb', line 13 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 |