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 CHANGED
@@ -1 +1 @@
1
- 0.9.3
1
+ 0.9.4
@@ -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
@@ -39,11 +39,17 @@ module Stalkr
39
39
  end
40
40
 
41
41
  def self.extract_id(str)
42
- if str =~ regex() then
43
- # return the first non-nil backreference
44
- m = $~.to_a
45
- m.shift
46
- return m.find{ |s| not s.nil? }.gsub(/ /, '').upcase
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
@@ -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
@@ -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|\b(\d{12}|\d{15})\b/i
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
 
@@ -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]|[\dT]\d{3} ?\d{4} ?\d{3})\b/i
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
 
@@ -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) 20 digits (no real pattern)
8
- # TODO: 2nd pattern is too generic. Could also match other carriers.
9
- self.regex = /\b(91\d{2} ?\d{4} ?\d{4} ?\d{4} ?\d{4} ?\d{2}|91\d{2} ?\d{4} ?\d{4} ?\d{4} ?\d{4})\b|\b(\d{20})\b/i
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
@@ -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 = "9102901001299192824023"
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
- id = Stalkr::USPS.extract_id(str)
36
- assert(!id.nil? && id == "02185456301085740874")
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: 61
4
+ hash: 51
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 9
9
- - 3
10
- version: 0.9.3
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-17 00:00:00 -05:00
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