polar_express 0.0.1.alpha → 0.0.3.alpha

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -27,9 +27,25 @@ Or install it yourself as:
27
27
  @tracker = PolarExpress.new('DHL', '017219678663')
28
28
  info = @tracker.track!
29
29
  info.status # => :delivered
30
+ info.statuses
31
+ # [{:date=>
32
+ # #<DateTime: 2012-12-05T15:52:00+08:00>,
33
+ # :city=>nil,
34
+ # :status=>:shipping_instructions_received,
35
+ # :text=>
36
+ # "The instruction data for this shipment have been provided by the sender to DHL electronically"},
37
+ # {:date=>
38
+ # #<DateTime: 2012-12-06T07:31:00+08:00>,
39
+ # :city=>"Berlin-Friedrichshain",
40
+ # :status=>:in_delivery_vehicle,
41
+ # :text=>"The shipment has been loaded onto the delivery vehicle"},
42
+ # {:date=>
43
+ # #<DateTime: 2012-12-12T15:42:00+08:00>,
44
+ # :city=>nil,
45
+ # :status=>:delivered,
46
+ # :text=>"The recipient has picked up the shipment from the retail outlet"}]
30
47
  ```
31
48
 
32
-
33
49
  ## Contributing
34
50
 
35
51
  1. Fork it
@@ -2,6 +2,7 @@ require "polar_express/version"
2
2
  require "polar_express/polar_express"
3
3
  require "polar_express/tracker"
4
4
  require "polar_express/dhl"
5
+ require "polar_express/gls"
5
6
  require "polar_express/shipping_info"
6
7
 
7
8
  module PolarExpress
@@ -9,4 +10,5 @@ module PolarExpress
9
10
  require 'open-uri'
10
11
  require 'nokogiri'
11
12
  require 'date'
13
+ require 'json'
12
14
  end
@@ -1,4 +1,3 @@
1
- require 'pp'
2
1
  module PolarExpress
3
2
  module DHL
4
3
  class Tracker
@@ -25,28 +24,41 @@ module PolarExpress
25
24
  def timeline
26
25
  page.css("tr#toggle-#{@number}_1 table tbody tr").map do |tr|
27
26
  {
28
- date: DateTime.parse(tr.css('td.overflow').text.strip),
29
- city: tr.css('td.location').text.strip,
30
- status: text_to_status(tr.css('td.status').text.strip)
27
+ date: DateTime.parse(get_date(tr.css('td.overflow').text)),
28
+ city: (city = tr.css('td.location').text.strip) == '--' ? nil : city,
29
+ status: text_to_status(status_text = tr.css('td.status').text.strip),
30
+ text: status_text
31
31
  }
32
32
  end
33
33
  end
34
+ # TODO: look for better status names
34
35
  def text_to_status(text)
35
36
  case text
36
- when 'The shipment has been successfully delivered'
37
+ when /The recipient has picked up the shipment from/
37
38
  :delivered
38
- when "The shipment has been loaded onto the delivery vehicle"
39
+ when /The shipment has been successfully delivered/
40
+ :delivered
41
+ when /The shipment is on its way to the postal retail outlet/
42
+ :in_delivery_vehicle_to_retail_outler
43
+ when /The shipment has been delivered for pick-up at the/
44
+ :waiting_for_pick_up_in_retail_office
45
+ when /The shipment has been loaded onto the delivery vehicle/
39
46
  :in_delivery_vehicle
40
- when "The shipment has been processed in the destination parcel center"
41
- :destination_city
42
- when "The shipment has been processed in the parcel center of origin"
43
- :origin_city
44
- when "The instruction data for this shipment have been provided by the sender to DHL electronically"
47
+ when /The shipment has been processed in the destination parcel center/
48
+ :destination_parcel_center
49
+ when /The shipment has been processed in the parcel center of origin/
50
+ :origin_parcel_center
51
+ when /The instruction data for this shipment have been provided by the sender to DHL electronically/
45
52
  :shipping_instructions_received
53
+ when /A .+ attempt at delivery is being made/
54
+ :new_delivery_attempt
46
55
  else
47
56
  :other
48
57
  end
49
58
  end
59
+ def get_date(str)
60
+ str.scan(/[\d]{1,2}\.[\d]{1,2}\.[\d]{4} [\d]{1,2}:[\d]{1,2}/).flatten.first
61
+ end
50
62
  end
51
63
  end
52
64
  end
@@ -0,0 +1,61 @@
1
+ module PolarExpress
2
+ module GLS
3
+ class Tracker
4
+ attr_accessor :number
5
+ def initialize(number)
6
+ @number = number
7
+ end
8
+ def track!
9
+ info = ShippingInfo.new(@number)
10
+ info.statuses = timeline
11
+ info
12
+ end
13
+ private
14
+ def tracking_url
15
+ "https://gls-group.eu/app/service/open/rest/EU/en/rstt001?match=#{@number}&caller=witt002Scheme"
16
+ end
17
+ def page
18
+ @page ||= JSON.parse(open(tracking_url).read)
19
+ end
20
+ def history
21
+ @history ||= page["tuStatus"].first["history"].map do |event|
22
+ {
23
+ city: event['address']['city'],
24
+ date: DateTime.parse(event['date'] + ' ' + event['time'].split(':')[0] + ':00'),
25
+ text: event['evtDscr']
26
+ }
27
+ end.uniq.reverse
28
+ end
29
+ def timeline
30
+ history.each_with_index.map do |event, index|
31
+ # GLS specific logic
32
+ next if event[:text] == 'Inbound to GLS location to document physical hand over'
33
+ if event[:text] == 'Inbound to GLS location' and history[index-1][:text] == 'Outbound from GLS location'
34
+ event[:text] = 'The shipment has been processed in the destination parcel center'
35
+ end
36
+ event[:status] = text_to_status(event[:text])
37
+ event
38
+ end.compact
39
+ end
40
+ # TODO: look for better status names
41
+ def text_to_status(text)
42
+ case text
43
+ when /Delivered/
44
+ :delivered
45
+ when /Out for delivery on GLS vehicle/
46
+ :in_delivery_vehicle
47
+ when /The shipment has been processed in the destination parcel center/
48
+ :destination_parcel_center
49
+ when /Inbound to GLS location/
50
+ :origin_parcel_center
51
+ when /Outbound from GLS location/
52
+ :in_shipment
53
+ when /Information transmitted, no shipment available now/
54
+ :shipping_instructions_received
55
+ else
56
+ :other
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -7,7 +7,7 @@ module PolarExpress
7
7
  @courier_tracker = Object.const_get("PolarExpress::#{@courier}::Tracker").new(@shipping_number)
8
8
  end
9
9
  def track!
10
- @courier_tracker.track!
10
+ @track ||= @courier_tracker.track!
11
11
  end
12
12
 
13
13
  private
@@ -17,6 +17,8 @@ module PolarExpress
17
17
  case name.upcase
18
18
  when 'DHL'
19
19
  :DHL
20
+ when 'GLS'
21
+ :GLS
20
22
  end
21
23
  end
22
24
  end
@@ -1,3 +1,3 @@
1
1
  module PolarExpress
2
- VERSION = "0.0.1.alpha"
2
+ VERSION = "0.0.3.alpha"
3
3
  end
@@ -2,20 +2,41 @@ require 'spec_helper'
2
2
  describe PolarExpress do
3
3
  context 'Gem Basics' do
4
4
  it "creates a new instance of the gem" do
5
- @tracker = PolarExpress.new('DHL', '017219678663')
6
- @tracker.shipping_number.should == '017219678663'
5
+ @tracker = PolarExpress.new('DHL', '1234')
6
+ @tracker.should respond_to :shipping_number
7
7
  end
8
8
  end
9
9
  context 'DHL' do
10
10
  before do
11
- @tracker = PolarExpress.new('DHL', '017219678663')
11
+ @tracker = PolarExpress.new('DHL', '777707971894')
12
12
  end
13
- it "recognizes DHL" do
13
+ it "recognizes it" do
14
14
  @tracker.courier.should eq :DHL
15
15
  end
16
- it "tracks DHL" do
17
- info = @tracker.track!
18
- info.status.should eq :delivered
16
+ it "tracks it" do
17
+ @tracker.track!.status.should eq :delivered
18
+ end
19
+ it "records tracking statuses" do
20
+ statuses = @tracker.track!.statuses
21
+ statuses.length.should > 1
22
+ end
23
+ it "tracks date correctly" do
24
+ @tracker.track!.statuses.first[:date].should == DateTime.new(2013, 03, 15, 17, 45)
25
+ end
26
+ end
27
+ context 'GLS' do
28
+ before do
29
+ @tracker = PolarExpress.new('GLS', '291740105981')
30
+ end
31
+ it "recognizes it" do
32
+ @tracker.courier.should eq :GLS
33
+ end
34
+ it "tracks it" do
35
+ @tracker.track!.status.should eq :delivered
36
+ end
37
+ it "records tracking statuses" do
38
+ statuses = @tracker.track!.statuses
39
+ statuses.length.should > 1
19
40
  end
20
41
  end
21
42
  end
@@ -1 +1,13 @@
1
1
  require 'polar_express'
2
+
3
+ # http://stackoverflow.com/questions/1819614/how-do-i-globally-configure-rspec-to-keep-the-color-and-format-specdoc-o
4
+ RSpec.configure do |config|
5
+ # Use color in STDOUT
6
+ config.color_enabled = true
7
+
8
+ # Use color not only in STDOUT but also in pagers and files
9
+ config.tty = true
10
+
11
+ # Use the specified formatter
12
+ config.formatter = :documentation # :progress, :html, :textmate
13
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: polar_express
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1.alpha
4
+ version: 0.0.3.alpha
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-07 00:00:00.000000000 Z
12
+ date: 2013-03-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -90,6 +90,7 @@ files:
90
90
  - Rakefile
91
91
  - lib/polar_express.rb
92
92
  - lib/polar_express/dhl.rb
93
+ - lib/polar_express/gls.rb
93
94
  - lib/polar_express/polar_express.rb
94
95
  - lib/polar_express/shipping_info.rb
95
96
  - lib/polar_express/tracker.rb