class Deliveries::Couriers::Envialia::Pickups::Trace::FormatResponse

Constants

STATUS_CODES

Attributes

response[RW]

Public Class Methods

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

Public Instance Methods

execute() click to toggle source
# File lib/deliveries/couriers/envialia/pickups/trace/format_response.rb, line 30
def execute
  body = response.dig('Envelope', 'Body', 'WebServService___ConsRecEstadosResponse', 'strRecEstados')
  parsed_response = Hash.from_xml(body).dig('CONSULTA', 'REC_ESTADOS')

  checkpoints = formatted_checkpoints(parsed_response)

  tracking_info_params = {}
  tracking_info_params[:courier_id] = 'envialia'
  tracking_info_params[:tracking_code] = nil
  tracking_info_params[:status] = checkpoints.last.try(:status)
  tracking_info_params[:checkpoints] = checkpoints

  tracking_info_params
end

Private Instance Methods

formatted_checkpoint(shipment_status) click to toggle source
# File lib/deliveries/couriers/envialia/pickups/trace/format_response.rb, line 60
def formatted_checkpoint(shipment_status)
  status = STATUS_CODES[shipment_status['V_COD_TIPO_EST']]

  date = shipment_status['D_FEC_HORA_ALTA']

  Deliveries::Checkpoint.new(
    status: status(status),
    location: nil,
    tracked_at: Time.zone.strptime(date, '%m/%d/%Y %H:%M:%S'),
    description: status
  )
end
formatted_checkpoints(shipment_statuses) click to toggle source
# File lib/deliveries/couriers/envialia/pickups/trace/format_response.rb, line 47
def formatted_checkpoints(shipment_statuses)
  checkpoints = []
  if shipment_statuses.is_a?(Array)
    shipment_statuses.each do |shipment_status|
      checkpoints << formatted_checkpoint(shipment_status)
    end
  else
    checkpoints << formatted_checkpoint(shipment_statuses)
  end

  checkpoints
end
status(code) click to toggle source
# File lib/deliveries/couriers/envialia/pickups/trace/format_response.rb, line 73
def status(code)
  case code
  when 'Solicitada', 'Lectura en delegación', 'Asignada'
    :registered
  when 'Realizada', 'Recogida Parcial (Múltiples Destinos)'
    :in_transit
  when 'Recogida fallida', 'Datos insuficientes', 'Error al emitir la recogida'
    :delivery_failed
  when 'Anulada', 'Recogida anulada'
    :cancelled
  when 'Finalizada'
    :delivered
  else
    :unknown_status
  end
end