Class: FriendlyShipping::Services::Ups::ParseAddressValidationResponse

Inherits:
Object
  • Object
show all
Extended by:
Dry::Monads::Result::Mixin
Defined in:
lib/friendly_shipping/services/ups/parse_address_validation_response.rb

Class Method Summary collapse

Class Method Details

.build_suggestions(xml) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/friendly_shipping/services/ups/parse_address_validation_response.rb', line 36

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

.call(request:, response:) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/friendly_shipping/services/ups/parse_address_validation_response.rb', line 9

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::ApiResult.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