Keine Beschreibung http://j1x-huginn.herokuapp.com

aftership_agent.rb 4.8KB

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