stalkr 0.9.3 → 0.9.4
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/stalkr.rb +1 -0
- data/lib/stalkr/base.rb +11 -5
- data/lib/stalkr/dhl.rb +72 -0
- data/lib/stalkr/fedex.rb +3 -1
- data/lib/stalkr/ups.rb +3 -1
- data/lib/stalkr/usps.rb +10 -3
- data/test/stalkr/dhl_test.rb +38 -0
- data/test/stalkr/usps_test.rb +12 -3
- metadata +7 -4
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.9.
|
1
|
+
0.9.4
|
data/lib/stalkr.rb
CHANGED
@@ -18,6 +18,7 @@ require "#{stalkr_dir}/stalkr/error"
|
|
18
18
|
require "#{stalkr_dir}/stalkr/ups"
|
19
19
|
require "#{stalkr_dir}/stalkr/usps"
|
20
20
|
require "#{stalkr_dir}/stalkr/fedex"
|
21
|
+
require "#{stalkr_dir}/stalkr/dhl"
|
21
22
|
require "#{stalkr_dir}/stalkr/china_post"
|
22
23
|
|
23
24
|
if not DateTime.new.public_methods.include? "to_time" then
|
data/lib/stalkr/base.rb
CHANGED
@@ -39,11 +39,17 @@ module Stalkr
|
|
39
39
|
end
|
40
40
|
|
41
41
|
def self.extract_id(str)
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
42
|
+
r = regex()
|
43
|
+
if r.kind_of? Regexp then
|
44
|
+
r = [ r ]
|
45
|
+
end
|
46
|
+
r.each do |re|
|
47
|
+
if str =~ re then
|
48
|
+
# return the first non-nil backreference
|
49
|
+
m = $~.to_a
|
50
|
+
m.shift
|
51
|
+
return m.find{ |s| not s.nil? }.gsub(/ /, '').upcase
|
52
|
+
end
|
47
53
|
end
|
48
54
|
return nil
|
49
55
|
end
|
data/lib/stalkr/dhl.rb
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
|
2
|
+
module Stalkr
|
3
|
+
|
4
|
+
class DHL < Base
|
5
|
+
|
6
|
+
# ########## (10 digit number)
|
7
|
+
self.regex = /\b(\d{10})\b/i
|
8
|
+
|
9
|
+
def track(id)
|
10
|
+
|
11
|
+
url = "http://track.dhl-usa.com/TrackByNbr.asp?ShipmentNumber="
|
12
|
+
html = fetchurl(url + id)
|
13
|
+
|
14
|
+
begin
|
15
|
+
|
16
|
+
info_scraper = Scraper.define do
|
17
|
+
|
18
|
+
array :updated_at
|
19
|
+
array :history
|
20
|
+
|
21
|
+
process "td.headerRegular.pT5", :status => :text
|
22
|
+
process "td.bodytext.pT5", :updated_at => :text
|
23
|
+
process ".pL5_pT4_pB4_pR4", :history => :text
|
24
|
+
|
25
|
+
result :status, :updated_at, :history
|
26
|
+
end
|
27
|
+
|
28
|
+
# row_scraper = Scraper.define do
|
29
|
+
# array :rows
|
30
|
+
# process "tr", :rows => td_scraper
|
31
|
+
# result :rows
|
32
|
+
# end
|
33
|
+
|
34
|
+
info = info_scraper.scrape(html)
|
35
|
+
|
36
|
+
if not info.status or info.status.strip.empty? then
|
37
|
+
raise "DHL scraper failed"
|
38
|
+
end
|
39
|
+
|
40
|
+
ret = Result.new(:DHL)
|
41
|
+
|
42
|
+
status = cleanup_html(info.status).downcase
|
43
|
+
if status =~ /delivered/ then
|
44
|
+
ret.status = DELIVERED
|
45
|
+
elsif info.history.size > 0 then
|
46
|
+
ret.status = IN_TRANSIT
|
47
|
+
else
|
48
|
+
ret.status = UNKNOWN
|
49
|
+
end
|
50
|
+
|
51
|
+
updated_at = cleanup_html( info.updated_at.first )
|
52
|
+
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.
|
53
|
+
|
54
|
+
if ret.status == DELIVERED then
|
55
|
+
delivered_at = cleanup_html(info.history.shift)
|
56
|
+
ret.delivered_at = DateTime.strptime( updated_at, "%m/%d/%Y %I:%M %p" ).to_time # 2/17/2011 10:13 am
|
57
|
+
end
|
58
|
+
|
59
|
+
# update location
|
60
|
+
ret.location = cleanup_html(info.history[2])
|
61
|
+
|
62
|
+
return ret
|
63
|
+
|
64
|
+
rescue Exception => ex
|
65
|
+
raise Stalkr::Error.new(ex, html)
|
66
|
+
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
|
71
|
+
end # class ChinaPost
|
72
|
+
end # module Stalkr
|
data/lib/stalkr/fedex.rb
CHANGED
@@ -6,7 +6,9 @@ module Stalkr
|
|
6
6
|
|
7
7
|
class FEDEX < Base
|
8
8
|
|
9
|
-
self.regex = /\b((96\d\d\d\d\d ?\d\d\d\d|96\d\d) ?\d\d\d\d ?d\d\d\d( ?\d\d\d)?)\b
|
9
|
+
self.regex = [ /\b((96\d\d\d\d\d ?\d\d\d\d|96\d\d) ?\d\d\d\d ?d\d\d\d( ?\d\d\d)?)\b/i,
|
10
|
+
/\b(\d{12}|\d{15})\b/i
|
11
|
+
]
|
10
12
|
|
11
13
|
def track(id)
|
12
14
|
|
data/lib/stalkr/ups.rb
CHANGED
@@ -16,7 +16,9 @@ module Stalkr
|
|
16
16
|
|
17
17
|
class UPS < Base
|
18
18
|
|
19
|
-
self.regex = /\b(1Z ?[0-9A-Z]{3} ?[0-9A-Z]{3} ?[0-9A-Z]{2} ?[0-9A-Z]{4} ?[0-9A-Z]{3} ?[0-9A-Z]
|
19
|
+
self.regex = [ /\b(1Z ?[0-9A-Z]{3} ?[0-9A-Z]{3} ?[0-9A-Z]{2} ?[0-9A-Z]{4} ?[0-9A-Z]{3} ?[0-9A-Z])\b/i,
|
20
|
+
/\b([\dT]\d{3} ?\d{4} ?\d{3})\b/i
|
21
|
+
]
|
20
22
|
|
21
23
|
def track(id)
|
22
24
|
|
data/lib/stalkr/usps.rb
CHANGED
@@ -4,9 +4,16 @@ module Stalkr
|
|
4
4
|
class USPS < Base
|
5
5
|
|
6
6
|
# 1) 20 or 22 digits, beginning with 91 (and with or without spaces between groupings)
|
7
|
-
# 2)
|
8
|
-
#
|
9
|
-
|
7
|
+
# 2) 22 digits, beginning with 94 (and with or without spaces between groupings)
|
8
|
+
# 3) 20 digits (no real pattern)
|
9
|
+
# 4) XX#########XX (start and end with 2 letter, 9 digit number in the middle)
|
10
|
+
|
11
|
+
# TODO: 3rd pattern is too generic. Could also match other carriers.
|
12
|
+
self.regex = [ /\b((91|94)\d{2} ?\d{4} ?\d{4} ?\d{4} ?\d{4} ?\d{2})\b/i,
|
13
|
+
/\b(91\d{2} ?\d{4} ?\d{4} ?\d{4} ?\d{4})\b/i,
|
14
|
+
/\b(\d{20})\b/i,
|
15
|
+
/\b([A-Z]{2}\d{9}[A-Z]{2})\b/i
|
16
|
+
]
|
10
17
|
|
11
18
|
def track(id)
|
12
19
|
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../test_helper.rb'
|
2
|
+
|
3
|
+
class DHL_Test < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_track
|
6
|
+
id = "5391587692"
|
7
|
+
info = Stalkr::DHL.new.track(id)
|
8
|
+
assert(info.shipper == :DHL)
|
9
|
+
assert(info.status == Stalkr::IN_TRANSIT)
|
10
|
+
assert(info.updated_at.kind_of? Time)
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_track_bad_code
|
14
|
+
|
15
|
+
begin
|
16
|
+
id = "foobar"
|
17
|
+
info = Stalkr::DHL.new.track(id)
|
18
|
+
flunk("no exception was thrown")
|
19
|
+
|
20
|
+
rescue Exception => ex
|
21
|
+
if ex.message != "DHL scraper failed" then
|
22
|
+
flunk("wrong exception was thrown")
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_valid_id
|
29
|
+
id = "5391587692"
|
30
|
+
assert(Stalkr::DHL.is_valid?(id))
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_extract_id
|
34
|
+
str = "*5391587692*"
|
35
|
+
assert(Stalkr::DHL.extract_id(str) == "5391587692")
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
data/test/stalkr/usps_test.rb
CHANGED
@@ -3,7 +3,7 @@ require File.dirname(__FILE__) + '/../test_helper.rb'
|
|
3
3
|
class USPS_Test < Test::Unit::TestCase
|
4
4
|
|
5
5
|
def test_track
|
6
|
-
id = "
|
6
|
+
id = "9400110200793331353367"
|
7
7
|
info = Stalkr::USPS.new.track(id)
|
8
8
|
assert(info.shipper == :USPS)
|
9
9
|
assert(info.status == Stalkr::DELIVERED)
|
@@ -32,8 +32,17 @@ class USPS_Test < Test::Unit::TestCase
|
|
32
32
|
|
33
33
|
def test_extract_id_2
|
34
34
|
str = "Your tracking number is: 02185456301085740874. "
|
35
|
-
|
36
|
-
|
35
|
+
assert(Stalkr::USPS.extract_id(str) == "02185456301085740874")
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_extract_id_3
|
39
|
+
str = "Your tracking number is: 9400110200793331353367. "
|
40
|
+
assert(Stalkr::USPS.extract_id(str) == "9400110200793331353367")
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_extract_id_4
|
44
|
+
str = "Your tracking number is: LJ337239295US. "
|
45
|
+
assert(Stalkr::USPS.extract_id(str) == "LJ337239295US")
|
37
46
|
end
|
38
47
|
|
39
48
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: stalkr
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 51
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 9
|
9
|
-
-
|
10
|
-
version: 0.9.
|
9
|
+
- 4
|
10
|
+
version: 0.9.4
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Chetan Sarva
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-02-
|
18
|
+
date: 2011-02-19 00:00:00 -05:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -112,12 +112,14 @@ files:
|
|
112
112
|
- lib/stalkr.rb
|
113
113
|
- lib/stalkr/base.rb
|
114
114
|
- lib/stalkr/china_post.rb
|
115
|
+
- lib/stalkr/dhl.rb
|
115
116
|
- lib/stalkr/error.rb
|
116
117
|
- lib/stalkr/fedex.rb
|
117
118
|
- lib/stalkr/result.rb
|
118
119
|
- lib/stalkr/ups.rb
|
119
120
|
- lib/stalkr/usps.rb
|
120
121
|
- test/stalkr/china_post_test.rb
|
122
|
+
- test/stalkr/dhl_test.rb
|
121
123
|
- test/stalkr/fedex_test.rb
|
122
124
|
- test/stalkr/ups_test.rb
|
123
125
|
- test/stalkr/usps_test.rb
|
@@ -159,6 +161,7 @@ specification_version: 3
|
|
159
161
|
summary: Ruby library for tracking packages
|
160
162
|
test_files:
|
161
163
|
- test/stalkr/china_post_test.rb
|
164
|
+
- test/stalkr/dhl_test.rb
|
162
165
|
- test/stalkr/fedex_test.rb
|
163
166
|
- test/stalkr/ups_test.rb
|
164
167
|
- test/stalkr/usps_test.rb
|