Class: FriendlyShipping::Services::TForceFreight::ParseCreateBOLResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/friendly_shipping/services/tforce_freight/parse_create_bol_response.rb

Overview

Parses a Bill of Lading (BOL) response into an ApiResult.

Class Method Summary collapse

Class Method Details

.call(request:, response:) ⇒ ApiResult<ShipmentInformation>

Returns the parsed result.

Parameters:

  • request (Request)

    the original request

  • response (RestClient::Response)

    the response to parse

Returns:



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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/friendly_shipping/services/tforce_freight/parse_create_bol_response.rb', line 12

def call(request:, response:)
  json = JSON.parse(response.body)

  bol_id = json.dig("detail", "bolId")
  pro_number = json.dig("detail", "pro")
  pickup_confirmation_number = json.dig("detail", "pickup", "transactionReference", "confirmationNumber")

  rate_detail = json.dig("detail", "rateDetail")&.first
  if rate_detail
    service_code = rate_detail.dig("service", "code")
    shipping_method = SHIPPING_METHODS.detect { |sm| sm.service_code == service_code }

    rates = rate_detail["rate"].each_with_object({}) do |rate, result|
      result[rate["code"].downcase.to_sym] = rate["value"].to_f
    end

    total_charges_value = rate_detail.dig("shipmentCharges", "total", "value")
    total_charges_currency = rate_detail.dig("shipmentCharges", "total", "currency")
    total_charges = Money.new(total_charges_value.to_f * 100, total_charges_currency || "USD")

    billable_weight_value = rate_detail.dig("shipmentWeights", "billable", "value")
    billable_weight_unit = rate_detail.dig("shipmentWeights", "billable", "unit")
    billable_weight = Measured::Weight(billable_weight_value, billable_weight_unit&.downcase || :lb)

    days_in_transit = rate_detail.dig("timeInTransit", "timeInTransit")&.to_i
    cost_breakdown = build_cost_breakdown(rate_detail)
  end

  origin_service_center = json.dig("detail", "originServiceCenter")
  email_sent = json.dig("detail", "pickup", "transactionReference", "emailSent") == "true"
  origin_is_rural = json.dig("detail", "pickup", "transactionReference", "originIsRural") == "true"
  destination_is_rural = json.dig("detail", "pickup", "transactionReference", "destinationIsRural") == "true"

  documents = json.dig("detail", "documents", "image")&.map do |image_data|
    ParseShipmentDocument.call(image_data: image_data)
  end

  FriendlyShipping::ApiResult.new(
    ShipmentInformation.new(
      bol_id: bol_id,
      pro_number: pro_number,
      pickup_confirmation_number: pickup_confirmation_number,
      origin_service_center: origin_service_center,
      email_sent: email_sent,
      origin_is_rural: origin_is_rural,
      destination_is_rural: destination_is_rural,
      rates: rates,
      total_charges: total_charges,
      billable_weight: billable_weight,
      days_in_transit: days_in_transit,
      shipping_method: shipping_method,
      documents: documents,
      data: {
        cost_breakdown: cost_breakdown
      }
    ),
    original_request: request,
    original_response: response
  )
end