Class: FriendlyShipping::Services::UpsJson::ParseLabelsResponse

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

Class Method Summary collapse

Class Method Details

.build_cost_breakdown(package) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/friendly_shipping/services/ups_json/parse_labels_response.rb', line 48

def build_cost_breakdown(package)
  costs = [
    package["BaseServiceCharge"]&.merge("Code" => "BaseServiceCharge"),
    package["ServiceOptionsCharges"],
    package["ItemizedCharges"]
  ].flatten

  costs.map { |cost| ParseMoneyHash.call(cost, "UnknownSurcharge") }.compact.to_h
end

.build_labels(labels_result) ⇒ 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
# File 'lib/friendly_shipping/services/ups_json/parse_labels_response.rb', line 23

def build_labels(labels_result)
  shipment_result = labels_result["ShipmentResponse"]["ShipmentResults"]

  Array.wrap(shipment_result["PackageResults"]).map do |package|
    cost_breakdown = build_cost_breakdown(package)
    package_cost = cost_breakdown.values.any? ? cost_breakdown.values.sum : nil

    FriendlyShipping::Services::UpsJson::Label.new(
      shipment_id: shipment_result["ShipmentIdentificationNumber"],
      tracking_number: package["TrackingNumber"],
      usps_tracking_number: package["USPSPICNumber"],
      label_data: Base64.decode64(package["ShippingLabel"]["GraphicImage"]),
      label_format: package["ShippingLabel"]["ImageFormat"]["Code"],
      label_href: package["LabelURL"] || shipment_result["LabelURL"],
      cost: package_cost,
      shipment_cost: get_shipment_cost(shipment_result),
      data: {
        cost_breakdown: cost_breakdown,
        negotiated_rate: get_negotiated_rate(shipment_result),
        customer_context: labels_result["ShipmentResponse"]["Response"]["TransactionReference"]["CustomerContext"]
      }.compact
    )
  end
end

.call(request:, response:) ⇒ Object



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

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

.get_negotiated_rate(shipment_result) ⇒ Object



63
64
65
66
# File 'lib/friendly_shipping/services/ups_json/parse_labels_response.rb', line 63

def get_negotiated_rate(shipment_result)
  negotiated_total_hash = shipment_result.dig("NegotiatedRateCharges", "TotalCharge")
  ParseMoneyHash.call(negotiated_total_hash, "TotalCharge")&.last
end

.get_shipment_cost(shipment_result) ⇒ Object



58
59
60
61
# File 'lib/friendly_shipping/services/ups_json/parse_labels_response.rb', line 58

def get_shipment_cost(shipment_result)
  total_charges_hash = shipment_result.dig("ShipmentCharges", "TotalCharges")
  ParseMoneyHash.call(total_charges_hash, "TotalCharges")&.last
end