aftership_agent.rb 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. require 'uri'
  2. module Agents
  3. class AftershipAgent < Agent
  4. API_URL = 'https://api.aftership.com/v4'
  5. description <<-MD
  6. The Aftership agent allows you to track your shipment from aftership and emit them into events.
  7. 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.
  8. You can use this agent to retrieve tracking data. You have to provide a specific `get` request and its associated option.
  9. To get all trackings for your packages please enter `get` for key and `/trackings` for the option.
  10. To get tracking for a specific tracking number, add the extra options `slug`, `tracking_number` and set `single_tracking_request` to true.
  11. To get all tracking results for backup purpose set key to `get` and option to `/trackings/export`.
  12. To get the last checkpoint of a package set key to `get` and option to `/last_checkpoint` plus provide `slug` and `tracking_number`
  13. `slug` is a unique courier code.
  14. You can get a list of courier information here `https://www.aftership.com/courier`
  15. Required Options:
  16. * `Content-Type` application/json
  17. * `api_key` - YOUR_API_KEY.
  18. * `get/delete and its associated options`
  19. MD
  20. event_description <<-MD
  21. A typical tracking event has 3 objects (attributes, tracking, and checkpoint) and it looks like this
  22. {
  23. "meta": {
  24. "code": 200
  25. },
  26. "data": {
  27. "page": 1,
  28. "limit": 100,
  29. "count": 3,
  30. "keyword": "",
  31. "slug": "",
  32. "origin": [],
  33. "destination": [],
  34. "tag": "",
  35. "fields": "",
  36. "created_at_min": "2014-03-27T07:36:14+00:00",
  37. "created_at_max": "2014-06-25T07:36:14+00:00",
  38. "trackings": [
  39. {
  40. "id": "53aa7b5c415a670000000021",
  41. "created_at": "2014-06-25T07:33:48+00:00",
  42. "updated_at": "2014-06-25T07:33:55+00:00",
  43. "tracking_number": "123456789",
  44. "tracking_account_number": null,
  45. "tracking_postal_code": null,
  46. "tracking_ship_date": null,
  47. "slug": "dhl",
  48. "active": false,
  49. "custom_fields": {
  50. "product_price": "USD19.99",
  51. "product_name": "iPhone Case"
  52. },
  53. "customer_name": null,
  54. "destination_country_iso3": null,
  55. "emails": [
  56. "email@yourdomain.com",
  57. "another_email@yourdomain.com"
  58. ],
  59. "expected_delivery": null,
  60. "note": null,
  61. "order_id": "ID 1234",
  62. "order_id_path": "http://www.aftership.com/order_id=1234",
  63. "origin_country_iso3": null,
  64. "shipment_package_count": 0,
  65. "shipment_type": null,
  66. "signed_by": "raul",
  67. "smses": [],
  68. "source": "api",
  69. "tag": "Delivered",
  70. "title": "Title Name",
  71. "tracked_count": 1,
  72. "unique_token": "xy_fej9Llg",
  73. "checkpoints": [
  74. {
  75. "slug": "dhl",
  76. "city": null,
  77. "created_at": "2014-06-25T07:33:53+00:00",
  78. "country_name": "VALENCIA - SPAIN",
  79. "message": "Awaiting collection by recipient as requested",
  80. "country_iso3": null,
  81. "tag": "InTransit",
  82. "checkpoint_time": "2014-05-12T12:02:00",
  83. "coordinates": [],
  84. "state": null,
  85. "zip": null
  86. },
  87. ...
  88. ]
  89. },
  90. ...
  91. ]
  92. }
  93. }
  94. MD
  95. def default_options
  96. { 'api_key' => 'YOUR_API_KEY',
  97. 'Content_Type' => 'application/json',
  98. 'get' => '/trackings'
  99. }
  100. end
  101. def single_tracking_request?
  102. boolify(interpolated[:single_tracking_request])
  103. end
  104. def last_checkpoint?
  105. boolify(interpolated[:last_checkpoint_request])
  106. end
  107. def working?
  108. !recent_error_logs?
  109. end
  110. def validate_options
  111. errors.add(:base, "You need to specify a api key") unless options['api_key'].present?
  112. errors.add(:base, "Content-Type must be set to application/json") unless options['Content_Type'].present? && options['Content_Type'] == 'application/json'
  113. end
  114. def check
  115. if single_tracking_request? || last_checkpoint?
  116. response = HTTParty.get(single_or_checkpoint_tracking_url, request_options)
  117. else
  118. response = HTTParty.get(event_url, request_options)
  119. end
  120. events = JSON.parse response.body
  121. create_event :payload => events
  122. end
  123. private
  124. def base_url
  125. "https://api.aftership.com/v4/"
  126. end
  127. def event_url
  128. base_url + "#{URI.encode(interpolated[:get].to_s)}"
  129. end
  130. def single_or_checkpoint_tracking_url
  131. base_url + "#{URI.encode(interpolated[:get].to_s)}/#{URI.encode(interpolated[:slug].to_s)}/#{URI.encode(interpolated[:tracking_number].to_s)}"
  132. end
  133. def request_options
  134. {:headers => {"aftership-api-key" => interpolated['api_key'], "Content-Type"=>"application/json"} }
  135. end
  136. end
  137. end