Class: Stalkr::DHL

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

Instance Method Summary collapse

Methods inherited from Base

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

Instance Method Details

#track(id) ⇒ Object



9
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
65
66
67
68
69
# File 'lib/stalkr/dhl.rb', line 9

def track(id)

    url = "http://track.dhl-usa.com/TrackByNbr.asp?ShipmentNumber="
    html = fetchurl(url + id)

    begin

        info_scraper = Scraper.define do

            array :updated_at
            array :history

            process "td.headerRegular.pT5", :status => :text
            process "td.bodytext.pT5", :updated_at => :text
            process ".pL5_pT4_pB4_pR4", :history => :text

            result :status, :updated_at, :history
        end

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

        info = info_scraper.scrape(html)

        if not info.status or info.status.strip.empty? then
            raise "DHL scraper failed"
        end

        ret = Result.new(:DHL)

        status = cleanup_html(info.status).downcase
        if status =~ /delivered/ then
            ret.status = DELIVERED
        elsif info.history.size > 0 then
            ret.status = IN_TRANSIT
        else
            ret.status = UNKNOWN
        end

        updated_at = cleanup_html( info.updated_at.first )
        ret.updated_at = DateTime.strptime( updated_at, "Tracking detail provided by DHL: %m/%d/%Y, %I:%M:%S %p" ).to_time # 2/19/2011, 12:42:51 pm pt.

        if ret.status == DELIVERED then
            delivered_at = cleanup_html(info.history.shift)
            ret.delivered_at = DateTime.strptime( updated_at, "%m/%d/%Y %I:%M %p" ).to_time # 2/17/2011 10:13 am
        end

        # update location
        ret.location = cleanup_html(info.history[2])

        return ret

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

    end

end