Class: FriendlyShipping::Services::UpsJson::ParseTimingsResponse

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

Class Method Summary collapse

Class Method Details

.build_timings(timings_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
# File 'lib/friendly_shipping/services/ups_json/parse_timings_response.rb', line 23

def build_timings(timings_result, shipment)
  service_timings = Array.wrap(timings_result.dig('emsResponse', 'services'))
  service_timings.map do |timing|
    service_description = timing['serviceLevelDescription']
    shipping_method = SHIPPING_METHODS.detect do |potential_shipping_method|
      service_description == potential_shipping_method.name &&
        potential_shipping_method.origin_countries.map(&:code).include?(shipment.origin.country.code)
    end
    delivery_date = timing['deliveryDate']
    delivery_time = timing['deliveryTime']
    delivery = Time.parse("#{delivery_date} #{delivery_time}")
    pickup_date = timing['pickupDate']
    pickup_time = timing['pickupTime']
    pickup = Time.parse("#{pickup_date} #{pickup_time}")

    guaranteed = timing['guaranteeIndicator'] == '1'
    business_transit_days = timing['businessTransitDays']

    FriendlyShipping::Timing.new(
      shipping_method: shipping_method,
      pickup: pickup,
      delivery: delivery,
      guaranteed: guaranteed,
      data: {
        business_transit_days: business_transit_days
      }
    )
  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_json/parse_timings_response.rb', line 8

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