class Deliveries::Couriers::CorreosExpress::Pickups::Trace::FormatResponse

Attributes

response[RW]

Public Class Methods

new(response:) click to toggle source
# File lib/deliveries/couriers/correos_express/pickups/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/correos_express/pickups/trace/format_response.rb, line 15
def execute
  tracking_info_params = {}

  checkpoints = formatted_checkpoints([response[:situaciones]].flatten)

  tracking_info_params[:status] = checkpoints.last.try(:status)
  tracking_info_params[:checkpoints] = checkpoints
  tracking_info_params[:courier_id] = 'correos_express'
  tracking_info_params[:tracking_code] = response[:recogida]

  tracking_info_params
end

Private Instance Methods

formatted_checkpoint(shipment_status) click to toggle source
# File lib/deliveries/couriers/correos_express/pickups/trace/format_response.rb, line 39
def formatted_checkpoint(shipment_status)
  description = shipment_status[:desc_situacion].to_s
  description += " (#{shipment_status[:desc_motivo]})" if shipment_status[:desc_motivo].present?
  Deliveries::Checkpoint.new(
    status: status_code(shipment_status[:desc_situacion]),
    location: nil,
    tracked_at: Time.zone.strptime((shipment_status[:fec_situacion]).to_s, '%d/%m/%Y %H:%M:%S'),
    description: description
  )
end
formatted_checkpoints(shipment_statuses) click to toggle source
# File lib/deliveries/couriers/correos_express/pickups/trace/format_response.rb, line 30
def formatted_checkpoints(shipment_statuses)
  checkpoints = []
  shipment_statuses.each do |shipment_status|
    checkpoints << formatted_checkpoint(shipment_status)
  end
  checkpoints.delete_if { |k, _v| k.status == :unknown_status }
             .sort_by(&:tracked_at)
end
status_code(code) click to toggle source
# File lib/deliveries/couriers/correos_express/pickups/trace/format_response.rb, line 50
def status_code(code)
  case code
  when 'PDTE ASIGNAR'
    :registered
  when 'TRANSMITIDA'
    :in_transit
  when 'FALLIDA'
    :delivery_failed
  when 'ANULADA'
    :canceled
  when 'EFECTUADA'
    :delivered
  else
    :unknown_status
  end
end