Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Change via to service, add service_code and event codes, add delivery…

…_at datetime
  • Loading branch information...
commit 0a8395e05cd8f47538a0ce874cf5c31441e50e85 1 parent 93219fa
Jeff Keen authored March 25, 2013
35  lib/trackerific/details.rb
@@ -9,16 +9,19 @@ class Details
9 9
     # @api private
10 10
     def initialize(details = {})
11 11
       required = [:package_id, :summary, :events]
12  
-      valid = required + [:weight, :via, :estimated_delivery_date, :origin, :destination]
  12
+      valid = required + [:weight, :service, :service_code, :estimated_delivery_date, :origin, :destination, :delivered_at]
13 13
       validate_options(details, required, valid)
14  
-      @package_id = details[:package_id]
15  
-      @summary = details[:summary]
16  
-      @origin = details[:origin]
17  
-      @destination = details[:destination]
18  
-      @events = details[:events]
19  
-      @weight = details[:weight] || nil
  14
+      
  15
+      @package_id              = details[:package_id]
  16
+      @summary                 = details[:summary]
  17
+      @origin                  = details[:origin]
  18
+      @destination             = details[:destination]
  19
+      @events                  = details[:events]
  20
+      @weight                  = details[:weight] || nil
  21
+      @delivered_at            = details[:delivered_at]
20 22
       @estimated_delivery_date = details[:estimated_delivery_date] || nil
21  
-      @via = details[:via] || nil
  23
+      @service                 = details[:service] || nil
  24
+      @service_code            = details[:service_code] || nil
22 25
     end
23 26
     
24 27
     # The package identifier
@@ -72,6 +75,14 @@ def weight
72 75
     def estimated_delivery_date
73 76
       @estimated_delivery_date
74 77
     end
  78
+
  79
+    # when it was delivered
  80
+    # @example Get the delivery date of package
  81
+    # details.delivered_at # => delivery time
  82
+    # @return [Time]
  83
+    def delivered_at
  84
+      @delivered_at
  85
+    end
75 86
     
76 87
     # Example: UPS 2ND DAY AIR. May not be supported by all services
77 88
     # @example Get how the package was shipped
@@ -80,8 +91,12 @@ def estimated_delivery_date
80 91
     #   details.via # => "UPS 2ND DAY AIR"
81 92
     # @return [String] The service used to ship the package
82 93
     # @api public
83  
-    def via
84  
-      @via
  94
+    def service
  95
+      @service
  96
+    end
  97
+    
  98
+    def service_code
  99
+      @service_code
85 100
     end
86 101
     
87 102
     # Location object of origin    
9  lib/trackerific/event.rb
@@ -8,10 +8,12 @@ class Event
8 8
     # @api private
9 9
     def initialize(details = {})
10 10
       required_details = [:date, :description, :location]
11  
-      validate_options details, required_details
  11
+      valid = required_details + [:code]
  12
+      validate_options(details, required_details, valid)
12 13
       @date         = details[:date]
13 14
       @description  = details[:description]
14 15
       @location     = details[:location]
  16
+      @code         = details[:code]
15 17
     end
16 18
     
17 19
     # The date and time of the event
@@ -23,6 +25,11 @@ def date
23 25
       @date
24 26
     end
25 27
     
  28
+    # Shortcode for what the event actually is
  29
+    def code
  30
+      @code
  31
+    end
  32
+    
26 33
     # The event's description
27 34
     # @example Get the description of an event
28 35
     #   description = details.events.first.description
18  lib/trackerific/services/fedex.rb
@@ -53,8 +53,10 @@ def track_package(package_id)
53 53
         date = Time.parse("#{e["Date"]} #{e["Time"]}")
54 54
         desc = e["Description"]
55 55
         addr = e["Address"]
  56
+        code = e["Type"]
56 57
         events << Trackerific::Event.new(
57 58
           :date         => date,
  59
+          :code         => code,
58 60
           :description  => desc,
59 61
           :location     => Trackerific::Location.new(:city => addr["City"], :state => addr["StateOrProvinceCode"], :country => addr["CountryCode"], :postal_code =>  addr["PostalCode"])
60 62
         )
@@ -70,15 +72,19 @@ def track_package(package_id)
70 72
                         :city => details["DestinationAddress"]["City"], 
71 73
                         :state => details["DestinationAddress"]["StateOrProvinceCode"], 
72 74
                         :country => details["DestinationAddress"]["CountryCode"]) if details["DestinationAddress"]
73  
-      
  75
+                        
  76
+      delivered_at = Time.parse("#{details["DeliveredDate"]} #{details["DeliveredTime"]}") if details["DeliveredDate"]
74 77
       
75 78
       # Return a Trackerific::Details containing all the events
76 79
       Trackerific::Details.new(
77  
-        :package_id  => details["TrackingNumber"],
78  
-        :origin      => origin,
79  
-        :destination => destination,
80  
-        :summary     => details["StatusDescription"],
81  
-        :events      => events
  80
+        :package_id   => details["TrackingNumber"],
  81
+        :origin       => origin,
  82
+        :service_code => details["CarrierCode"],
  83
+        :service      => details["Service"],
  84
+        :destination  => destination,
  85
+        :delivered_at => delivered_at,
  86
+        :summary      => details["StatusDescription"],
  87
+        :events       => events
82 88
       )
83 89
     end
84 90
     
6  lib/trackerific/services/ups.rb
@@ -86,11 +86,13 @@ def parse_success_response(http_response)
86 86
         date    = Date.parse(a['Date'])
87 87
         date    = DateTime.parse("#{date} #{hours}:#{minutes}:#{seconds}")
88 88
         desc    = a['Status']['StatusType']['Description'].titleize
  89
+        code    = a['Status']['StatusType']['Code']
89 90
         loc     = a['ActivityLocation']['Address'].map {|k,v| v}.join(" ")
90 91
         address = a['ActivityLocation']['Address']
91  
-
  92
+        
92 93
         events << Trackerific::Event.new(
93 94
           :date         => date,
  95
+          :code         => code,
94 96
           :description  => desc,
95 97
           :location     => Trackerific::Location.new(:city => address["City"], :state => address["StateProvinceCode"], :country => address["CountryCode"]),
96 98
         )
@@ -105,6 +107,8 @@ def parse_success_response(http_response)
105 107
         :origin       => Trackerific::Location.new(:address => origin["AddressLine1"], :city => origin["City"], :state => origin["StateProvinceCode"], :country => origin["CountryCode"]),
106 108
         :destination  => Trackerific::Location.new(:address => destination["AddressLine1"], :city => destination["City"], :state => destination["StateProvinceCode"], :country => destination["CountryCode"]),
107 109
         :events       => events,
  110
+        :service_code => http_response["TrackResponse"]["Shipment"]["Service"]["Code"],
  111
+        :service      => http_response["TrackResponse"]["Shipment"]["Service"]["Description"],
108 112
         :estimated_delivery_date => deliveryDate
109 113
       )
110 114
     end
23  spec/lib/trackerific/details_spec.rb
@@ -9,8 +9,9 @@
9 9
       :events     => Array.new
10 10
     }
11 11
     @valid_options = {
12  
-      :weight     => Hash.new,
13  
-      :via        => String.new
  12
+      :weight       => Hash.new,
  13
+      :service      => String.new,
  14
+      :service_code => String.new
14 15
     }
15 16
   end
16 17
   
@@ -37,8 +38,13 @@
37 38
       it { should be_nil }
38 39
     end
39 40
     
40  
-    describe :via do
41  
-      subject { @details.via }
  41
+    describe :service do
  42
+      subject { @details.service }
  43
+      it { should be_nil }
  44
+    end
  45
+    
  46
+    describe :service_code do
  47
+      subject { @details.service_code }
42 48
       it { should be_nil }
43 49
     end
44 50
   end
@@ -66,8 +72,13 @@
66 72
       it { should be_a Hash }
67 73
     end
68 74
     
69  
-    describe :via do
70  
-      subject { @details.via }
  75
+    describe :service do
  76
+      subject { @details.service }
  77
+      it { should be_a String }
  78
+    end
  79
+    
  80
+    describe :service_code do
  81
+      subject { @details.service_code }
71 82
       it { should be_a String }
72 83
     end
73 84
   end
10  spec/lib/trackerific/services/fedex_spec.rb
@@ -58,6 +58,16 @@
58 58
         subject { @tracking.destination }
59 59
         it("should return a Trackerific::Location") { should be_a Trackerific::Location }
60 60
       end
  61
+      
  62
+      describe "service" do
  63
+        subject { @tracking.service_code }
  64
+        it {should == "FDXG"}
  65
+      end
  66
+      
  67
+      describe "service" do
  68
+        subject { @tracking.service }
  69
+        it {should == "FedEx Home Delivery"}
  70
+      end
61 71
 
62 72
       describe "events.length" do
63 73
         subject { @tracking.events.length }
10  spec/lib/trackerific/services/ups_spec.rb
@@ -64,6 +64,16 @@
64 64
         it { should >= 1 }
65 65
       end
66 66
       
  67
+      describe "service code" do
  68
+        subject { @tracking.service_code }
  69
+        it {should == "02"}
  70
+      end
  71
+      
  72
+      describe "service" do
  73
+        subject { @tracking.service }
  74
+        it {should == "2ND DAY AIR"}
  75
+      end
  76
+      
67 77
       describe :summary do
68 78
         subject { @tracking.summary }
69 79
         it { should_not be_empty }

0 notes on commit 0a8395e

Please sign in to comment.
Something went wrong with that request. Please try again.