Class: Deliveries::Couriers::Ups::Shipments::Trace

Inherits:
Object
  • Object
show all
Includes:
JsonRequest
Defined in:
lib/deliveries/couriers/ups/shipments/trace.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tracking_code:, language: nil) ⇒ Trace

Returns a new instance of Trace.



10
11
12
13
# File 'lib/deliveries/couriers/ups/shipments/trace.rb', line 10

def initialize(tracking_code:, language: nil)
  self.tracking_code = tracking_code
  self.language = language
end

Instance Attribute Details

#languageObject

Returns the value of attribute language.



8
9
10
# File 'lib/deliveries/couriers/ups/shipments/trace.rb', line 8

def language
  @language
end

#tracking_codeObject

Returns the value of attribute tracking_code.



8
9
10
# File 'lib/deliveries/couriers/ups/shipments/trace.rb', line 8

def tracking_code
  @tracking_code
end

Instance Method Details

#executeObject

Raises:



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/deliveries/couriers/ups/shipments/trace.rb', line 15

def execute
  response = call nil, method: :get, url_params: { inquiry_number: tracking_code },
                       query_params: { locale: locale }

  package = response.dig(:trackResponse, :shipment, 0, :package, 0, :activity)
  raise Error, 'Cannot obtain package activity data' if package.blank?

  activities = package.map do |activity|
    {
      status: status(activity.dig(:status, :type), activity.dig(:status, :code)),
      location: activity.dig(:location, :address, :city),
      tracked_at: DateTime.parse("#{activity[:date]}T#{activity[:time]}"),
      description: activity.dig(:status, :description)
    }
  end

  activities.sort_by { |activity| activity[:tracked_at] }
end