aftership_agent.rb 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. require 'uri'
  2. module Agents
  3. class AftershipAgent < Agent
  4. cannot_receive_events!
  5. default_schedule "every_10m"
  6. description <<~MD
  7. The Aftership agent allows you to track your shipment from aftership and emit them into events.
  8. 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.
  9. You can use this agent to retrieve tracking data.
  10. Provide the `path` for the API endpoint that you'd like to hit. For example, for all active packages, enter `trackings`
  11. (see https://www.aftership.com/docs/api/4/trackings), for a specific package, use `trackings/SLUG/TRACKING_NUMBER`
  12. and replace `SLUG` with a courier code and `TRACKING_NUMBER` with the tracking number. You can request last checkpoint of a package
  13. by providing `last_checkpoint/SLUG/TRACKING_NUMBER` instead.
  14. You can get a list of courier information here `https://www.aftership.com/courier`
  15. Required Options:
  16. * `api_key` - YOUR_API_KEY.
  17. * `path request and its full path`
  18. MD
  19. event_description <<~MD
  20. A typical tracking event have 2 important objects (tracking, and checkpoint) and the tracking/checkpoint looks like this.
  21. "trackings": [
  22. {
  23. "id": "53aa7b5c415a670000000021",
  24. "created_at": "2014-06-25T07:33:48+00:00",
  25. "updated_at": "2014-06-25T07:33:55+00:00",
  26. "tracking_number": "123456789",
  27. "tracking_account_number": null,
  28. "tracking_postal_code": null,
  29. "tracking_ship_date": null,
  30. "slug": "dhl",
  31. "active": false,
  32. "custom_fields": {
  33. "product_price": "USD19.99",
  34. "product_name": "iPhone Case"
  35. },
  36. "customer_name": null,
  37. "destination_country_iso3": null,
  38. "emails": [
  39. "email@yourdomain.com",
  40. "another_email@yourdomain.com"
  41. ],
  42. "expected_delivery": null,
  43. "note": null,
  44. "order_id": "ID 1234",
  45. "order_id_path": "http://www.aftership.com/order_id=1234",
  46. "origin_country_iso3": null,
  47. "shipment_package_count": 0,
  48. "shipment_type": null,
  49. "signed_by": "raul",
  50. "smses": [],
  51. "source": "api",
  52. "tag": "Delivered",
  53. "title": "Title Name",
  54. "tracked_count": 1,
  55. "unique_token": "xy_fej9Llg",
  56. "checkpoints": [
  57. {
  58. "slug": "dhl",
  59. "city": null,
  60. "created_at": "2014-06-25T07:33:53+00:00",
  61. "country_name": "VALENCIA - SPAIN",
  62. "message": "Awaiting collection by recipient as requested",
  63. "country_iso3": null,
  64. "tag": "InTransit",
  65. "checkpoint_time": "2014-05-12T12:02:00",
  66. "coordinates": [],
  67. "state": null,
  68. "zip": null
  69. },
  70. ...
  71. ]
  72. },
  73. ...
  74. ]
  75. MD
  76. def default_options
  77. {
  78. 'api_key' => 'YOUR_API_KEY',
  79. 'path' => 'trackings',
  80. }
  81. end
  82. def working?
  83. !recent_error_logs?
  84. end
  85. def validate_options
  86. errors.add(:base, "You need to specify a api key") unless options['api_key'].present?
  87. errors.add(:base, "You need to specify a path request") unless options['path'].present?
  88. end
  89. def check
  90. response = HTTParty.get(event_url, request_options)
  91. events = JSON.parse response.body
  92. create_event payload: events
  93. end
  94. private
  95. def base_url
  96. "https://api.aftership.com/v4/"
  97. end
  98. def event_url
  99. Utils.normalize_uri(base_url + interpolated[:path].to_s).to_s
  100. end
  101. def request_options
  102. {
  103. headers: {
  104. "aftership-api-key" => interpolated['api_key'],
  105. "Content-Type" => "application/json",
  106. }
  107. }
  108. end
  109. end
  110. end