class Deliveries::Couriers::Spring::Shipments::Trace::FormatResponse

Attributes

response[RW]

Public Class Methods

new(response:) click to toggle source
# File lib/deliveries/couriers/spring/shipments/trace/format_response.rb, line 11
def initialize(response:)
  self.response = response
end

Public Instance Methods

execute() click to toggle source
# File lib/deliveries/couriers/spring/shipments/trace/format_response.rb, line 15
def execute
  checkpoints = response[:Events].sort_by { |event| event[:DateTime] }

  tracking_info_params = {}
  tracking_info_params[:courier_id] = Deliveries::Couriers::Spring::COURIER_ID
  tracking_info_params[:tracking_code] = response[:TrackingNumber]
  tracking_info_params[:status] = status(checkpoints.last[:Code])
  tracking_info_params[:checkpoints] = formatted_checkpoints(checkpoints)

  tracking_info_params
end

Private Instance Methods

formatted_checkpoint(shipment_status) click to toggle source
# File lib/deliveries/couriers/spring/shipments/trace/format_response.rb, line 35
def formatted_checkpoint(shipment_status)
  Deliveries::Checkpoint.new(
    status: status(shipment_status[:Code]),
    location: shipment_status[:Country],
    tracked_at: Time.zone.strptime(shipment_status[:DateTime], '%Y-%m-%d %H:%M:%S'),
    description: shipment_status[:CarrierDescription]
  )
end
formatted_checkpoints(shipment_statuses) click to toggle source
# File lib/deliveries/couriers/spring/shipments/trace/format_response.rb, line 29
def formatted_checkpoints(shipment_statuses)
  shipment_statuses.map do |shipment_status|
    formatted_checkpoint(shipment_status)
  end
end
status(code) click to toggle source
# File lib/deliveries/couriers/spring/shipments/trace/format_response.rb, line 44
def status(code)
  # Spring Tracking Event Codes List
  # ---
  #   0 - PARCEL CREATED
  #  20 - ACCEPTED
  #  21 - IN TRANSIT
  #  31 - DELIVERY EXCEPTION
  #  40 - IN CUSTOMS
  #  41 - CUSTOMS EXCEPTION
  #  91 - DELIVERY ATTEMPTED
  #  92 - DELIVERY AWAITING COLLECTION
  #  93 - DELIVERY SCHEDULED
  # 100 - DELIVERED
  # 111 - LOST OR DESTROYED
  # 124 - RETURN IN TRANSIT
  # 125 - RETURN RECEIVED

  case code
  when 0, 20
    :registered
  when 21, 40, 41, 91, 93
    :in_transit
  when 92
    :in_collection_point
  when 100
    :delivered
  else
    :unknown_status
  end
end