|
|
@@ -1,22 +1,31 @@
|
|
1
|
+require 'uri'
|
|
2
|
+
|
1
|
3
|
module Agents
|
2
|
4
|
class AftershipAgent < Agent
|
3
|
5
|
|
4
|
|
- API_URL = 'https://api.aftership.com/v4/couriers/all'
|
5
|
|
- HEADERS = {"aftership-api-key"=>"api_key", "Content-Type"=>"application/json"}
|
|
6
|
+ API_URL = 'https://api.aftership.com/v4'
|
|
7
|
+ HEADERS = {"aftership-api-key"=> "apikey", "Content-Type"=>"application/json"}
|
6
|
8
|
|
7
|
9
|
description <<-MD
|
8
|
10
|
|
9
|
|
- The Aftership agent allows you to track your shipment data from aftership.
|
|
11
|
+ The Aftership agent allows you to track your shipment data from aftership and emit them into events.
|
10
|
12
|
|
11
|
13
|
To be able to use the Aftership API, you need to generate an `API Key`.
|
12
|
14
|
You can generate an api key by visiting `apps > app and click add` on aftership website.
|
13
|
15
|
|
14
|
16
|
The agent is limited to 600 reqs/min per account. You do need a paying plan to use their tracking feature.
|
15
|
17
|
|
|
18
|
+ If you are requesting tracking data from aftership. You have to put in a specific url for get_url in default options.
|
|
19
|
+
|
|
20
|
+ The options are `/trackings/export` to get tracking results for backup purposes, `/trackings/:slug/:tracking_number` to get tracking
|
|
21
|
+
|
|
22
|
+ for a single tracking number and `trackings` to get all of your trackings.
|
|
23
|
+
|
16
|
24
|
Required Options:
|
17
|
25
|
|
18
|
26
|
* `Content-Type` application/json
|
19
|
27
|
* `aftership_api_key` - YOUR_API_KEY.
|
|
28
|
+ * `a certain request whether it be get or put or post`
|
20
|
29
|
MD
|
21
|
30
|
|
22
|
31
|
event_description <<-MD
|
|
|
@@ -37,26 +46,36 @@ module Agents
|
37
|
46
|
MD
|
38
|
47
|
|
39
|
48
|
def default_options
|
40
|
|
- { 'aftership_api_key' => 'YOUR_API_KEY',
|
41
|
|
- 'Content_Type' => 'application/json'
|
|
49
|
+ { 'api_key' => 'YOUR_API_KEY',
|
|
50
|
+ 'Content_Type' => 'application/json',
|
|
51
|
+ 'get_url' => '/trackings'
|
42
|
52
|
}
|
43
|
53
|
end
|
44
|
54
|
|
|
55
|
+ def uri
|
|
56
|
+ #there may be an updated version
|
|
57
|
+ uri = URI.parse('https://api.aftership.com/v4')
|
|
58
|
+ #uri.query = [uri.query, '/trackings' ].compact.join()
|
|
59
|
+ uri.query = [uri.query, interpolated['get_url'] ].compact.join()
|
|
60
|
+ uri.to_s.gsub('?','')
|
|
61
|
+ end
|
|
62
|
+
|
45
|
63
|
def working?
|
46
|
64
|
!recent_error_logs?
|
47
|
65
|
end
|
48
|
66
|
|
49
|
67
|
def validate_options
|
50
|
|
- errors.add(:base, "You need to specify a aftership api key") unless options['aftership-api-key'].present?
|
51
|
|
- #errors.add(:base, "Content-Type must be set to application/json") unless options['aftership-api-key'].present? && options['aftership-api-key'] == 'application/json'
|
|
68
|
+ #errors.add(:base, "You need to specify a aftership api key") unless options['aftership-api-key'].present?
|
|
69
|
+ errors.add(:base, "Content-Type must be set to application/json") unless options['Content_Type'].present? && options['Content_Type'] == 'application/json'
|
|
70
|
+ #only one put or request can be requested
|
52
|
71
|
end
|
53
|
72
|
|
54
|
|
- def aftership
|
55
|
|
- HTTParty.get("https://api.aftership.com/v4/couriers/all", :headers => HEADERS)
|
|
73
|
+ def request
|
|
74
|
+ HTTParty.get(uri, :headers => HEADERS)
|
56
|
75
|
end
|
57
|
76
|
|
58
|
77
|
def check
|
59
|
|
- data = {"body" => aftership.body, "message" => aftership.message}
|
|
78
|
+ data = {"body" => request.body, "message" => request.message}
|
60
|
79
|
create_event :payload => data
|
61
|
80
|
end
|
62
|
81
|
end
|