Browse Source

lots of clarifications and providing full api path

Judy Ngai 9 years ago
parent
commit
0ead3673b4
2 changed files with 20 additions and 35 deletions
  1. 7 25
      app/models/agents/aftership_agent.rb
  2. 13 10
      spec/models/agents/aftership_agent_spec.rb

+ 7 - 25
app/models/agents/aftership_agent.rb

@@ -10,21 +10,19 @@ module Agents
 
       To be able to use the Aftership API, you need to generate an `API Key`. You need a paying plan to use their tracking feature.
 
-      You can use this agent to retrieve tracking data. You have to provide a specific `path` request and its associated option.
+      You can use this agent to retrieve tracking data.
  
-      To get all trackings for your packages please enter `path` for key and `trackings` for the option.
-      To get tracking for a specific tracking number, add the extra keys `slug`, `tracking_number` and their associated values. Set `single_tracking_request` to true.
-
-      To get the last checkpoint of a package set key to `path` and option to `last_checkpoint`. Please provide `slug` and `tracking_number`. Set `last_checkpoint_request` to true.
-
-      `slug` is a unique courier code. 
+      Provide the `path` for the API endpoint that you'd like to hit. For example, for all active packages, enter `trackings` 
+      (see https://www.aftership.com/docs/api/4/trackings), for a specific package, use `trackings/SLUG/TRACKING_NUMBER` 
+      and replace `SLUG` with a courier code and `TRACKING_NUMBER` with the tracking number. You can request last checkpoint of a package 
+      by providing `last_checkpoint/SLUG/TRACKING_NUMBER` instead.
 
       You can get a list of courier information here `https://www.aftership.com/courier`
 
       Required Options:
 
       * `api_key` - YOUR_API_KEY.
-      * `path and its associated options`
+      * `path request and its full path`
     MD
 
     event_description <<-MD
@@ -92,14 +90,6 @@ module Agents
       }
     end
 
-    def single_tracking_request?
-      boolify(interpolated[:single_tracking_request])
-    end
-
-    def last_checkpoint?
-      boolify(interpolated[:last_checkpoint_request])
-    end
-
     def working?
       !recent_error_logs?
     end
@@ -110,11 +100,7 @@ module Agents
     end
 
     def check
-      if single_tracking_request? || last_checkpoint?
-        response = HTTParty.get(single_or_checkpoint_tracking_url, request_options)
-      else
-        response = HTTParty.get(event_url, request_options)
-      end
+      response = HTTParty.get(event_url, request_options)
       events = JSON.parse response.body
       create_event :payload => events
     end
@@ -128,10 +114,6 @@ module Agents
       base_url + "#{URI.encode(interpolated[:path].to_s)}"
     end
 
-    def single_or_checkpoint_tracking_url
-      base_url + "#{URI.encode(interpolated[:path].to_s)}/#{URI.encode(interpolated[:slug].to_s)}/#{URI.encode(interpolated[:tracking_number].to_s)}"
-    end
-
     def request_options
       {:headers => {"aftership-api-key" => interpolated['api_key'], "Content-Type"=>"application/json"} }
     end

+ 13 - 10
spec/models/agents/aftership_agent_spec.rb

@@ -9,11 +9,15 @@ describe Agents::AftershipAgent do
       :headers => {"Content-Type" => "text/json"}
     )
 
+    stub_request(:get, "trackings/usps/9361289878905919630610").to_return(
+      :body => File.read(Rails.root.join("spec/data_fixtures/aftership.json")),
+      :status => 200,
+      :headers => {"Content-Type" => "text/json"}
+    )
+
     @opts = {
       "api_key" => '800deeaf-e285-9d62-bc90-j999c1973cc9',
-      "path" => 'trackings',
-      "slug" => 'usps',
-      "tracking_number" => "9361289684090010005054"
+      "path" => 'trackings'
     }
 
     @checker = Agents::AftershipAgent.new(:name => "tectonic", :options => @opts)
@@ -30,15 +34,14 @@ describe Agents::AftershipAgent do
       expect(@checker.send(:event_url)).to eq("https://api.aftership.com/v4/trackings")
     end
 
-    it "should generate the correct single tracking url" do
-      @checker.options['single_tracking_request'] = true
-      expect(@checker.send(:single_or_checkpoint_tracking_url)).to eq("https://api.aftership.com/v4/trackings/usps/9361289684090010005054")
+    it "should generate the correct specific tracking url" do
+      @checker.options['path'] = "trackings/usps/9361289878905919630610"
+      expect(@checker.send(:event_url)).to eq("https://api.aftership.com/v4/trackings/usps/9361289878905919630610")
     end
 
-    it "should generate the correct checkpoint tracking url" do
-      @checker.options['path'] = 'last_checkpoint'
-      @checker.options['last_checkpoint_request'] = true
-      expect(@checker.send(:single_or_checkpoint_tracking_url)).to eq("https://api.aftership.com/v4/last_checkpoint/usps/9361289684090010005054")
+    it "should generate the correct last checkpoint url" do
+      @checker.options['path'] = "last_checkpoint/usps/9361289878905919630610"
+      expect(@checker.send(:event_url)).to eq("https://api.aftership.com/v4/last_checkpoint/usps/9361289878905919630610")
     end
   end