qpx_agent.rb 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. module Agents
  2. class QpxAgent < Agent
  3. cannot_receive_events!
  4. default_schedule "every_12h"
  5. description <<-MD
  6. The QpxExpressAgent will tell you airline prices between a pair of cities. The api limit is 50 requests/day.
  7. Follow their documentation here (https://developers.google.com/qpx-express/v1/prereqs#get-a-google-account) to retrieve an api key.
  8. After you get to the google developer console, created a project, enabled qpx express api then you can choose `api key` credential to be created.
  9. `Origin` and `Destination` requires `airport code`.
  10. All the default options must exist. For `infantInSeatCount`, `infantInLapCount`, `seniorCount`, and `childCount`, leave them to the default value of `0` if its not necessary.
  11. Set `refundable` to `true` if you want ticket to be refundable. Make sure `date` is in this type of date format `YYYY-MO-DAY`.
  12. You can limit the number of `solutions` returned back. The first solution is the lowest priced ticket. Every detailed trip option details starts at `"slice"`.
  13. MD
  14. event_description <<-MD
  15. The event payload will have objects that contains valuable data like this
  16. "carrier": [
  17. {
  18. "code": "B6",
  19. "name": "Jetblue Airways Corporation"
  20. }
  21. ]
  22. "tripOption": [
  23. "saleTotal": "USD49.10"
  24. "slice": [
  25. ...
  26. ...
  27. "flight": {
  28. "carrier": "B6",
  29. "number": "833"
  30. }
  31. ]
  32. ]
  33. MD
  34. def default_options
  35. {
  36. 'qpx_api_key': '',
  37. 'adultCount': 1,
  38. 'origin': 'BOS',
  39. 'destination': 'SFO',
  40. 'date': '2016-04-11',
  41. 'childCount': 0,
  42. 'infantInSeatCount': 0,
  43. 'infantInLapCount': 0,
  44. 'seniorCount': 0,
  45. 'solutions': 3,
  46. 'refundable': false
  47. }
  48. end
  49. def validate_options
  50. errors.add(:base, "You need a qpx api key") unless options['qpx_api_key'].present?
  51. errors.add(:base, "Adult Count must exist") unless options['adultCount'].present?
  52. errors.add(:base, "Origin must exist") unless options['origin'].present?
  53. errors.add(:base, "Destination must exist") unless options['destination'].present?
  54. errors.add(:base, "Date must exist") unless options['date'].present?
  55. errors.add(:base, "Child Count") unless options['childCount'].present?
  56. errors.add(:base, "Infant In Seat Count must exist") unless options['infantInSeatCount'].present?
  57. errors.add(:base, "Infant In Lap Count") unless options['infantInLapCount'].present?
  58. errors.add(:base, "Senior Count must exist") unless options['seniorCount'].present?
  59. errors.add(:base, "Solutions must exist") unless options['solutions'].present?
  60. errors.add(:base, "Refundable must exist") unless options['refundable'].present?
  61. end
  62. def working?
  63. !recent_error_logs?
  64. end
  65. def check
  66. post_params = {:request=>{:passengers=>{:kind=>"qpxexpress#passengerCounts", :adultCount=> interpolated["adultCount"], :childCount=> interpolated["childCount"], :infantInLapCount=>interpolated["infantInLapCount"], :infantInSeatCount=>interpolated['infantInSeatCount'], :seniorCount=>interpolated["seniorCount"]}, :slice=>[{:kind=>"qpxexpress#sliceInput", :origin=> interpolated["origin"].to_s , :destination=> interpolated["destination"].to_s , :date=> interpolated["date"].to_s }], :refundable=> interpolated["refundable"], :solutions=> interpolated["solutions"]}}
  67. body = JSON.generate(post_params)
  68. request = HTTParty.post(event_url, :body => body, :headers => {"Content-Type" => "application/json"})
  69. events = JSON.parse request.body
  70. create_event :payload => events
  71. end
  72. def event_url
  73. endpoint = 'https://www.googleapis.com/qpxExpress/v1/trips/search?key=' + "#{URI.encode(interpolated[:qpx_api_key].to_s)}"
  74. end
  75. end
  76. end