7
8
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
41
42
43
44
45
46
47
48
49
50
51
52
|
# File 'lib/friendly_shipping/services/ups/parse_time_in_transit_response.rb', line 7
def self.call(request:, response:)
parsing_result = ParseXMLResponse.call(
request: request,
response: response,
expected_root_tag: 'TimeInTransitResponse'
)
parsing_result.fmap do |xml|
origin_country_code = xml.at('TransitResponse/TransitFrom/AddressArtifactFormat/CountryCode').text
timings = xml.root.xpath('//TransitResponse/ServiceSummary').map do |service_summary|
service_description = service_summary.at('Service/Description').text.gsub(/\s+/, ' ').strip
shipping_method = SHIPPING_METHODS.detect do |potential_shipping_method|
potential_shipping_method.name.starts_with?(service_description) &&
potential_shipping_method.origin_countries.map(&:code).include?(origin_country_code)
end
delivery_date = service_summary.at('EstimatedArrival/Date').text
delivery_time = service_summary.at('EstimatedArrival/Time').text
delivery = Time.parse("#{delivery_date} #{delivery_time}")
pickup_date = service_summary.at('EstimatedArrival/PickupDate').text
pickup_time = service_summary.at('EstimatedArrival/PickupTime')&.text
pickup = Time.parse("#{pickup_date} #{pickup_time}")
guaranteed = service_summary.at('Guaranteed/Code').text == 'Y'
business_transit_days = service_summary.at('EstimatedArrival/BusinessTransitDays').text
FriendlyShipping::Timing.new(
shipping_method: shipping_method,
pickup: pickup,
delivery: delivery,
guaranteed: guaranteed,
data: {
business_transit_days: business_transit_days
}
)
end
FriendlyShipping::ApiResult.new(
timings,
original_request: request,
original_response: response
)
end
end
|