stalkr 0.9.1 → 0.9.2

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.1
1
+ 0.9.2
@@ -9,13 +9,16 @@ require 'scrapi'
9
9
  require 'htmlentities'
10
10
  require 'date'
11
11
 
12
- require 'stalkr/base'
13
- require 'stalkr/result'
14
- require 'stalkr/error'
15
12
 
16
- require 'stalkr/ups'
17
- require 'stalkr/usps'
18
- require 'stalkr/fedex'
13
+ stalkr_dir = File.dirname(__FILE__)
14
+ require "#{stalkr_dir}/stalkr/base"
15
+ require "#{stalkr_dir}/stalkr/result"
16
+ require "#{stalkr_dir}/stalkr/error"
17
+
18
+ require "#{stalkr_dir}/stalkr/ups"
19
+ require "#{stalkr_dir}/stalkr/usps"
20
+ require "#{stalkr_dir}/stalkr/fedex"
21
+ require "#{stalkr_dir}/stalkr/china_post"
19
22
 
20
23
  if not DateTime.new.public_methods.include? "to_time" then
21
24
  # monkey patch DateTime to add to_time (exists in Ruby 1.9.2 and above)
@@ -7,8 +7,14 @@ module Stalkr
7
7
  attr_accessor :regex
8
8
  end
9
9
 
10
- def fetchurl(url)
11
- return Net::HTTP.get_response(URI.parse(URI.escape(url))).body
10
+ def fetchurl(url, data = nil)
11
+ url = URI.parse(URI.escape(url))
12
+ if data.nil? then
13
+ # GET request
14
+ return Net::HTTP.get_response(url).body
15
+ else
16
+ return Net::HTTP.post_form(url, data).body
17
+ end
12
18
  end
13
19
 
14
20
  def strip_tags(html)
@@ -0,0 +1,68 @@
1
+
2
+ module Stalkr
3
+
4
+ class ChinaPost < Base
5
+
6
+ # RA#########CN
7
+ # not sure if the RA is constant or not
8
+ self.regex = /\b(RA\d{9}CN)\b/i
9
+
10
+ def track(id)
11
+
12
+ url = "http://www.emsairmailtracking.com/chinapost.php"
13
+ html = fetchurl(url, {"itemNo" => id})
14
+
15
+ begin
16
+
17
+ td_scraper = Scraper.define do
18
+ array :tds
19
+ process "td", :tds => :text
20
+ result :tds
21
+ end
22
+
23
+ row_scraper = Scraper.define do
24
+ array :rows
25
+ process "tr", :rows => td_scraper
26
+ result :rows
27
+ end
28
+
29
+ rows = row_scraper.scrape(html)
30
+
31
+ if not rows or rows.size < 2 then
32
+ raise "ChinaPost scraper failed"
33
+ end
34
+
35
+ ret = Result.new(:ChinaPost)
36
+
37
+ # ["Item No.", "Year", "Status", "Location", "Destination Country", "Status", "Recipient's Signature"]
38
+ # get rid of the header row
39
+ rows.shift
40
+
41
+ status = rows.first[2].strip.downcase
42
+ puts status
43
+ if status =~ /delivered/ then
44
+ ret.status = DELIVERED
45
+ elsif status =~ /arrival|departure/ then
46
+ ret.status = IN_TRANSIT
47
+ else
48
+ ret.status = UNKNOWN
49
+ end
50
+
51
+ updated_at = cleanup_html( rows.first[5] )
52
+ ret.updated_at = DateTime.strptime( updated_at, "%Y-%m-%d" ).to_time
53
+
54
+ if ret.status == DELIVERED then
55
+ ret.delivered_at = ret.updated_at
56
+ end
57
+
58
+ return ret
59
+
60
+ rescue Exception => ex
61
+ raise Stalkr::Error.new(ex, html)
62
+
63
+ end
64
+
65
+ end
66
+
67
+ end # class ChinaPost
68
+ end # module Stalkr
@@ -0,0 +1,44 @@
1
+ require File.dirname(__FILE__) + '/../test_helper.rb'
2
+
3
+ class ChinaPost_Test < Test::Unit::TestCase
4
+
5
+ def test_track
6
+ id = "RA103044224CN"
7
+ info = Stalkr::ChinaPost.new.track(id)
8
+ assert(info.shipper == :ChinaPost)
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::ChinaPost.new.track(id)
18
+ flunk("no exception was thrown")
19
+ rescue Exception => ex
20
+ if ex.message != "ChinaPost scraper failed" then
21
+ flunk("wrong exception was thrown")
22
+ end
23
+ end
24
+
25
+ end
26
+
27
+ def test_valid_id
28
+ id = "RA103044224CN"
29
+ ret = Stalkr::ChinaPost.is_valid?(id)
30
+ assert(ret)
31
+ end
32
+
33
+ def test_invalid_id
34
+ id = "1X799470341200708"
35
+ ret = Stalkr::ChinaPost.is_valid?(id)
36
+ assert(!ret)
37
+ end
38
+
39
+ def test_extract_id
40
+ str = "asdf RA103044224CN asdf"
41
+ assert(Stalkr::ChinaPost.extract_id(str) == "RA103044224CN")
42
+ end
43
+
44
+ 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: 57
4
+ hash: 63
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 9
9
- - 1
10
- version: 0.9.1
9
+ - 2
10
+ version: 0.9.2
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-16 00:00:00 -05:00
18
+ date: 2011-02-17 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -111,11 +111,13 @@ files:
111
111
  - VERSION
112
112
  - lib/stalkr.rb
113
113
  - lib/stalkr/base.rb
114
+ - lib/stalkr/china_post.rb
114
115
  - lib/stalkr/error.rb
115
116
  - lib/stalkr/fedex.rb
116
117
  - lib/stalkr/result.rb
117
118
  - lib/stalkr/ups.rb
118
119
  - lib/stalkr/usps.rb
120
+ - test/stalkr/china_post_test.rb
119
121
  - test/stalkr/fedex_test.rb
120
122
  - test/stalkr/ups_test.rb
121
123
  - test/stalkr/usps_test.rb
@@ -156,6 +158,7 @@ signing_key:
156
158
  specification_version: 3
157
159
  summary: Ruby library for tracking packages
158
160
  test_files:
161
+ - test/stalkr/china_post_test.rb
159
162
  - test/stalkr/fedex_test.rb
160
163
  - test/stalkr/ups_test.rb
161
164
  - test/stalkr/usps_test.rb