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
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/friendly_shipping/services/ups_json/parse_rates_response.rb', line 23
def build_rates(rates_result, shipment)
rates = []
rated_shipments = Array.wrap(rates_result.dig('RateResponse', 'RatedShipment'))
rated_shipments.each do |rated_shipment|
service_code = rated_shipment.dig('Service', 'Code')
shipping_method = CARRIER.shipping_methods.detect do |sm|
sm.service_code == service_code && shipment.origin.country.in?(sm.origin_countries)
end
days_to_delivery = rated_shipment.dig('GuaranteedDelivery', 'BusinessDaysInTransit').to_i
total_cost = ParseMoneyHash.call(rated_shipment['TotalCharges'], 'TotalCharges').last
insurance_price = ParseMoneyHash.call(rated_shipment['ServiceOptionsCharges'], 'ServiceOptionsCharges')&.last
negotiated_rate = ParseMoneyHash.call(rated_shipment.dig('NegotiatedRateCharges', 'TotalCharge'), 'TotalCharge')&.last
negotiated_charges = (rated_shipment.dig('NegotiatedRateCharges', 'ItemizedCharges'), 'ItemizedCharges')
itemized_charges = (rated_shipment['ItemizedCharges'], 'ItemizedCharges')
rated_shipment_warnings = Array.wrap(rated_shipment['RatedShipmentAlert'])
rated_shipment_warnings = rated_shipment_warnings.map { |alert| alert["Description"] }
if rated_shipment_warnings.any? { |e| e.match?(/to Residential/) }
new_address_type = 'residential'
elsif rated_shipment_warnings.any? { |e| e.match?(/to Commercial/) }
new_address_type = 'commercial'
end
rates << FriendlyShipping::Rate.new(
shipping_method: shipping_method,
amounts: { total: total_cost },
warnings: rated_shipment_warnings,
errors: [],
data: {
insurance_price: insurance_price,
negotiated_rate: negotiated_rate,
negotiated_charges: negotiated_charges,
days_to_delivery: days_to_delivery,
new_address_type: new_address_type,
itemized_charges: itemized_charges,
packages: build_packages(rated_shipment)
}.compact
)
end
rates
end
|