Class: FriendlyShipping::Services::Ups::ParseRateResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/friendly_shipping/services/ups/parse_rate_response.rb

Class Method Summary collapse

Class Method Details

.build_rates(xml, 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
# File 'lib/friendly_shipping/services/ups/parse_rate_response.rb', line 23

def build_rates(xml, shipment)
  xml.root.css('> RatedShipment').map do |rated_shipment|
    service_code = rated_shipment.at('Service/Code').text
    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.at('GuaranteedDaysToDelivery').text.to_i

    total = ParseMoneyElement.call(rated_shipment.at('TotalCharges')).last
    insurance_price = ParseMoneyElement.call(rated_shipment.at('ServiceOptionsCharges'))&.last
    negotiated_rate = ParseMoneyElement.call(
      rated_shipment.at('NegotiatedRates/NetSummaryCharges/GrandTotal')
    )&.last
    negotiated_charges = extract_charges(rated_shipment.xpath('NegotiatedRates/ItemizedCharges'))
    itemized_charges = extract_charges(rated_shipment.xpath('ItemizedCharges'))

    rated_shipment_warnings = rated_shipment.css('RatedShipmentWarning').map { |e| e.text.strip }
    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

    FriendlyShipping::Rate.new(
      shipping_method: shipping_method,
      amounts: { total: total },
      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
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/parse_rate_response.rb', line 8

def call(request:, response:, shipment:)
  parsing_result = ParseXMLResponse.call(
    request: request,
    response: response,
    expected_root_tag: 'RatingServiceSelectionResponse'
  )
  parsing_result.fmap do |xml|
    FriendlyShipping::ApiResult.new(
      build_rates(xml, shipment),
      original_request: request,
      original_response: response
    )
  end
end