Class: Stalkr::ChinaPost

Inherits:
Base
  • Object
show all
Defined in:
lib/stalkr/china_post.rb

Instance Method Summary collapse

Methods inherited from Base

#cleanup_html, extract_id, #fetchurl, is_valid?, #strip_tags

Instance Method Details

#track(id) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/stalkr/china_post.rb', line 10

def track(id)

    url = "http://www.emsairmailtracking.com/chinapost.php"
    html = fetchurl(url, {"itemNo" => id})

    begin

        td_scraper = Scraper.define do
            array :tds
            process "td", :tds => :text
            result :tds
        end

        row_scraper = Scraper.define do
            array :rows
            process "tr", :rows => td_scraper
            result :rows
        end

        rows = row_scraper.scrape(html)

        if not rows or rows.size < 2 then
            raise "ChinaPost scraper failed"
        end

        ret = Result.new(:ChinaPost)

        # ["Item No.", "Year", "Status", "Location", "Destination Country", "Status", "Recipient's Signature"]
        # get rid of the header row
        rows.shift

        status = rows.first[2].strip.downcase
        if status =~ /delivered/ then
            ret.status = DELIVERED
        elsif status =~ /arrival|departure/ then
            ret.status = IN_TRANSIT
        else
            ret.status = UNKNOWN
        end

        updated_at = cleanup_html( rows.first[5] )
        ret.updated_at = DateTime.strptime( updated_at, "%Y-%m-%d" ).to_time

        if ret.status == DELIVERED then
            ret.delivered_at = ret.updated_at
        end

        return ret

    rescue Exception => ex
        raise Stalkr::Error.new(ex, html)

    end

end