Class: Correios::Rastreamento

Inherits:
Object
  • Object
show all
Defined in:
lib/correios.rb

Constant Summary collapse

WEBSRO =
"http://websro.correios.com.br/sro_bin/txect01$.QueryList?P_LINGUA=001&P_TIPO=001&P_COD_UNI"

Instance Method Summary collapse

Constructor Details

#initialize(codigo) ⇒ Rastreamento

Returns a new instance of Rastreamento.



41
42
43
44
45
46
47
# File 'lib/correios.rb', line 41

def initialize(codigo)
  if codigo.nil? or codigo.empty?
    raise ArgumentError, "Especifique o código de rastreamento corretamente."
  end

  @codigo = codigo
end

Instance Method Details

#buscarObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/correios.rb', line 49

def buscar
  pagina = rastrear

  return if pagina.xpath("//tr").count == 0

  sro = []

  pagina.xpath("//tr[position() > 1]").each do |linha|
    if linha.search("td").count > 1
      sro << {
        :data => DateTime.strptime(linha.search("td[@rowspan][1]").text.strip, "%d/%m/%Y %H:%M"),
        :local => linha.search("td[2]").text.strip,
        :descricao => linha.search("td[3]").text.strip
      }

      if linha.search("td[@rowspan='2'][1]").count > 0
        sro.last.merge! :detalhes => linha.search(".//following-sibling::tr[1]").text.strip
      end
    end
  end

  sro
end

#chegou?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/correios.rb', line 73

def chegou?
  buscar.first[:descricao] == "Entrega Efetuada"
end