Class: Muamba::Persistence

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

Constant Summary collapse

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

Instance Method Summary collapse

Instance Method Details

#listObject



17
18
19
# File 'lib/muamba/persistence.rb', line 17

def list
  muambas { |m| puts m }
end

#muambasObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/muamba/persistence.rb', line 38

def muambas
  begin
    File.open(File.expand_path("~/.muambas"), "r+") do |file|
      @muambas = Marshal.load(file.read) rescue {}
      yield @muambas
      file.truncate(0)
      file.rewind
      file.write Marshal.dump(@muambas)
    end

  rescue Errno::ENOENT
    File.open(File.expand_path("~/.muambas"), "w+") { |f| f.write "" }
    retry
  end
end

#track(tag, tracking) ⇒ Object



9
10
11
# File 'lib/muamba/persistence.rb', line 9

def track(tag, tracking)
  muambas { |m| m.merge! tag => tracking }
end

#untrack(tag) ⇒ Object



13
14
15
# File 'lib/muamba/persistence.rb', line 13

def untrack(tag)
  muambas { |m| m.reject! {|k,v| k == tag } }
end

#wtfObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/muamba/persistence.rb', line 21

def wtf
  muambas do |muamba|
    muamba.each do |tag, tracking|
      puts "\n\n:::::::::::::::::::::::  #{tag} (#{tracking})  :::::::::::::::::::::::"
      html = Nokogiri::HTML open(URL + tracking)
      rows = []
      html.css("tr").each do |tr|
        rows << tr.css("td").map(&:text)
      end
      header = rows.shift
      rows.unshift :separator
      rows.unshift header
      puts Terminal::Table.new :rows => rows
    end
  end
end