class Deliveries::Couriers::Envialia::Shipments::Trace::FormatResponse

Constants

STATUS_CODES

Attributes

response[RW]

Public Class Methods

new(response:) click to toggle source
# File lib/deliveries/couriers/envialia/shipments/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/shipments/trace/format_response.rb, line 35
def execute
  body = response.dig('Envelope', 'Body', 'WebServService___ConsEnvEstadosResponse', 'strEnvEstados')
  parsed_response = Hash.from_xml(body).dig('CONSULTA', 'ENV_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] = formatted_checkpoints(parsed_response)

  tracking_info_params
end

Private Instance Methods

formatted_checkpoint(shipment_status) click to toggle source
# File lib/deliveries/couriers/envialia/shipments/trace/format_response.rb, line 65
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/shipments/trace/format_response.rb, line 52
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/shipments/trace/format_response.rb, line 78
def status(code)
  case code
  when 'Documentado'
    :registered
  when 'En TrĂ¡nsito', 'En Reparto', 'En CS destino'
    :in_transit
  when 'Entregado'
    :delivered
  when 'Devuelto'
    :cancelled
  else
    :unknown_status
  end
end