Class: FriendlyShipping::Services::UpsJson::ParseRatesResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/friendly_shipping/services/ups_json/parse_rates_response.rb

Class Method Summary collapse

Class Method Details

.build_rates(rates_result, shipment) ⇒ Object



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 = extract_charges(rated_shipment.dig('NegotiatedRateCharges', 'ItemizedCharges'), 'ItemizedCharges')
    itemized_charges = extract_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

.call(request:, response:, shipment:) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/friendly_shipping/services/ups_json/parse_rates_response.rb', line 8

def call(request:, response:, shipment:)
  parsed_response = ParseJsonResponse.call(
    request: request,
    response: response,
    expected_root_key: 'RateResponse'
  )
  parsed_response.fmap do |parsing_result|
    FriendlyShipping::ApiResult.new(
      build_rates(parsing_result, shipment),
      original_request: request,
      original_response: response
    )
  end
end