Class: Stalkr::USPS
Instance Method Summary collapse
Methods inherited from Base
#cleanup_html, extract_id, #fetchurl, is_valid?, #strip_tags
Instance Method Details
#track(id) ⇒ Object
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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/stalkr/usps.rb', line 18 def track(id) # cleanup id id.gsub!(/ /, '') url = "http://trkcnfrm1.smi.usps.com/PTSInternetWeb/InterLabelInquiry.do?origTrackNum=%CODE%" url.gsub!(/%CODE%/, id) html = fetchurl(url) begin info_scraper = Scraper.define do array :details array :info array :txt process "span.mainTextbold", :info => :text process "td.mainTextbold", :details => :text process "td.mainText", :txt => :text result :details, :info, :txt end scrape = info_scraper.scrape(html) # verify its the correct response page if scrape.info[0].gsub(/ /, '') != id then raise "USPS scraper failed" end # extract and return ret = Result.new(:USPS) if scrape.txt.find{ |t| t =~ /There is no record of this item/ } then ret.status = UNKNOWN elsif scrape.info.find{ |i| i.downcase == "delivered" } then ret.status = DELIVERED else ret.status = UNKNOWN end if scrape.details and not scrape.details.empty? then if scrape.details[0] =~ /^(.*?), (.*? \d+, \d+), (\d+:\d+ .m), (.*?)$/ then ret.location = $4 # not sure if this time is always in EST or the time of the area in which it was delivered? ret.updated_at = DateTime.strptime( "#{$2} #{$3} -0500", "%B %d, %Y %I:%M %p %z" ).to_time if ret.status == DELIVERED then ret.delivered_at = ret.updated_at end elsif scrape.details[0] =~ /Electronic Shipping Info Received/ then ret.status = IN_TRANSIT end elsif s = scrape.txt.find{ |t| t =~ /files offline/ } s =~ /at (\d+:\d+ .m) on (.*? \d+, \d+) in (.*?)\.$/ ret.location = $3 # not sure if this time is always in EST or the time of the area in which it was delivered? ret.updated_at = DateTime.strptime( "#{$2} #{$1} -0500", "%B %d, %Y %I:%M %p %z" ).to_time if ret.status == DELIVERED then ret.delivered_at = ret.updated_at end end return ret rescue Exception => ex raise Stalkr::Error.new(ex, html) end end |