qpx_agent.rb 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 the minimum 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. 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.
  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. }
  47. end
  48. def validate_options
  49. errors.add(:base, "You need a qpx api key") unless options['qpx_api_key'].present?
  50. errors.add(:base, "Adult Count must exist") unless options['adultCount'].present?
  51. errors.add(:base, "Origin must exist") unless options['origin'].present?
  52. errors.add(:base, "Destination must exist") unless options['destination'].present?
  53. errors.add(:base, "Date must exist") unless options['date'].present?
  54. errors.add(:base, "Child Count") unless options['childCount'].present?
  55. errors.add(:base, "Infant In Seat Count must exist") unless options['infantInSeatCount'].present?
  56. errors.add(:base, "Infant In Lap Count") unless options['infantInLapCount'].present?
  57. errors.add(:base, "Senior Count must exist") unless options['seniorCount'].present?
  58. errors.add(:base, "Solutions must exist") unless options['solutions'].present?
  59. end
  60. def working?
  61. !recent_error_logs?
  62. end
  63. def check
  64. 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 }], :solutions=> interpolated["solutions"]}}
  65. body = JSON.generate(post_params)
  66. request = HTTParty.post(event_url, :body => body, :headers => {"Content-Type" => "application/json"})
  67. events = JSON.parse request.body
  68. create_event :payload => events
  69. end
  70. def event_url
  71. endpoint = 'https://www.googleapis.com/qpxExpress/v1/trips/search?key=' + "#{URI.encode(interpolated[:qpx_api_key].to_s)}"
  72. end
  73. end
  74. end