Class: Stalkr::FEDEX

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

Instance Method Summary collapse

Methods inherited from Base

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

Instance Method Details

#track(id) ⇒ Object



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
70
71
72
73
74
75
76
# File 'lib/stalkr/fedex.rb', line 13

def track(id)

    url = "http://www.fedex.com/Tracking?tracknumbers=%CODE%"

    url.gsub!(/%CODE%/, id)
    html = fetchurl(url)

    begin

        if html =~ /invalid._header/ then
            ret = Result.new(:FEDEX)
            ret.status = UNKNOWN
            return ret
        end

        info_scraper = Scraper.define do

            array :dates

            process "div.detailshipmentstatus", :status => :text
            process "div.detailshipdates > div.fielddata", :dates => :text
            process "div.detaildestination > div.fielddata", :destination => :text
            process_first "div.detailshipfacts div.dataentry", :service_type => :text

            result :status, :dates, :destination, :service_type

        end

        ret = Result.new(:FEDEX)

        info = info_scraper.scrape(html)

        status = info.status.strip.downcase
        if status == "delivered" then
            ret.status = DELIVERED
        elsif status == "on schedule" then
            ret.status = IN_TRANSIT
        else
            ret.status = UNKNOWN
        end
        ret.location = info.destination.strip

        # obj.service_type = info.service_type.strip

        # try to get dates
        shipped_date = info.dates[0].strip
        delivery_date = info.dates[1].strip if info.dates.length == 2
        if shipped_date.empty? then
            shipped_date = DateTime.strptime( $1, "%b %d, %Y" ).to_time if html =~ /var shipDate = "(.*?);$"/
            if html =~ /var deliveryDateTime = "(.*?)";$/ and not ($1.nil? or $1.strip.empty?) then
                delivery_date = DateTime.strptime( "#{$1} -5", "%b %d, %Y %I:%M %p %z" ).to_time
            end
        end
        ret.delivered_at = delivery_date
        ret.updated_at = delivery_date

        return ret

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

    end

end