Class: FriendlyShipping::Services::UpsJson::ParseCityStateLookupResponse

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

Class Method Summary collapse

Class Method Details

.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
35
36
37
38
39
40
# File 'lib/friendly_shipping/services/ups_json/parse_city_state_lookup_response.rb', line 9

def self.call(request:, response:)
  parsed_response = ParseJsonResponse.call(
    request: request,
    response: response,
    expected_root_key: 'XAVResponse'
  )
  parsed_response.bind do |city_state_lookup_response|
    if city_state_lookup_response['XAVResponse'].keys.include?('NoCandidatesIndicator')
      Failure(
        FriendlyShipping::ApiResult.new(
          "No candidates found.",
          original_request: request,
          original_response: response
        )
      )
    else
      candidate = city_state_lookup_response.dig('XAVResponse', 'Candidate').first
      Success(
        FriendlyShipping::ApiResult.new(
          Physical::Location.new(
            city: candidate.dig('AddressKeyFormat', 'PoliticalDivision2'),
            region: candidate.dig('AddressKeyFormat', 'PoliticalDivision1'),
            country: candidate.dig('AddressKeyFormat', 'CountryCode'),
            zip: candidate.dig('AddressKeyFormat', 'PostcodePrimaryLow')
          ),
          original_request: request,
          original_response: response
        )
      )
    end
  end
end