google_flights_agent.rb 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. module Agents
  2. class GoogleFlightsAgent < Agent
  3. include FormConfigurable
  4. cannot_receive_events!
  5. default_schedule "every_12h"
  6. description <<-MD
  7. The GoogleFlightsAgent will tell you the minimum airline prices between a pair of cities. The api limit is 50 requests/day.
  8. Follow their documentation here (https://developers.google.com/qpx-express/v1/prereqs#get-a-google-account) to retrieve an api key.
  9. 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.
  10. The `origin` and `destination` options require an [airport code](http://www.expedia.com/daily/airports/AirportCodes.asp).
  11. All the default options must exist. For `infantInSeatCount`, `infantInLapCount`, `seniorCount`, and `childCount`, leave them to the default value of `0` if you do not need them.
  12. Make sure `date` and `return_date` is in this date format: `YYYY-MM-DAY`.
  13. You can choose one way tickets only by setting `roundtrip` to `false`.
  14. You can limit the number of `solutions` returned. The first solution will be the lowest priced ticket.
  15. MD
  16. event_description <<-MD
  17. The event payload will have objects that contains valuable data like this
  18. "carrier": [
  19. {
  20. "code": "B6",
  21. "name": "Jetblue Airways Corporation"
  22. }
  23. ]
  24. "tripOption": [
  25. "saleTotal": "USD49.10"
  26. "slice": [
  27. ...
  28. ...
  29. "flight": {
  30. "carrier": "B6",
  31. "number": "833"
  32. }
  33. ]
  34. ]
  35. MD
  36. def default_options
  37. {
  38. 'qpx_api_key' => '',
  39. 'adultCount'=> 1,
  40. 'origin' => 'BOS',
  41. 'destination' => 'SFO',
  42. 'date' => '2016-04-11',
  43. 'childCount' => 0,
  44. 'infantInSeatCount' => 0,
  45. 'infantInLapCount'=> 0,
  46. 'seniorCount'=> 0,
  47. 'return_date' => '2016-04-18',
  48. 'roundtrip' => true,
  49. 'solutions'=> 3
  50. }
  51. end
  52. form_configurable :qpx_api_key, type: :string
  53. form_configurable :adultCount
  54. form_configurable :origin, type: :string
  55. form_configurable :destination, type: :string
  56. form_configurable :date, type: :string
  57. form_configurable :childCount
  58. form_configurable :infantInSeatCount
  59. form_configurable :infantInLapCount
  60. form_configurable :seniorCount
  61. form_configurable :roundtrip, type: :boolean
  62. form_configurable :return_date, type: :string
  63. form_configurable :solutions
  64. def validate_options
  65. errors.add(:base, "You need a qpx api key") unless options['qpx_api_key'].present?
  66. errors.add(:base, "Adult Count must exist") unless options['adultCount'].present?
  67. errors.add(:base, "Origin must exist") unless options['origin'].present?
  68. errors.add(:base, "Destination must exist") unless options['destination'].present?
  69. errors.add(:base, "Date must exist") unless options['date'].present?
  70. errors.add(:base, "Child Count") unless options['childCount'].present?
  71. errors.add(:base, "Infant In Seat Count must exist") unless options['infantInSeatCount'].present?
  72. errors.add(:base, "Infant In Lap Count") unless options['infantInLapCount'].present?
  73. errors.add(:base, "Senior Count must exist") unless options['seniorCount'].present?
  74. errors.add(:base, "Solutions must exist") unless options['solutions'].present?
  75. errors.add(:base, "Return Date must exist") if options["return_date"].blank? && boolify(options['roundtrip'])
  76. end
  77. def working?
  78. !recent_error_logs?
  79. end
  80. def round_trip?
  81. boolify(interpolated['roundtrip'])
  82. end
  83. def post_params
  84. if round_trip?
  85. post_params = {:request=>{:passengers=>{:kind=>"qpxexpress#passengerCounts", :adultCount=> interpolated["adultCount"], :childCount=> interpolated["childCount"], :infantInLapCount=>interpolated["infantInLapCount"], :infantInSeatCount=>interpolated['infantInSeatCount'], :seniorCount=>interpolated["seniorCount"]}, :slice=>[ {:origin=> interpolated["origin"].to_s , :destination=> interpolated["destination"].to_s , :date=> interpolated["date"].to_s }, {:origin=> interpolated["destination"].to_s , :destination=> interpolated["origin"].to_s , :date=> interpolated["return_date"].to_s } ], :solutions=> interpolated["solutions"]}}
  86. else
  87. 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"]}}
  88. end
  89. end
  90. def check
  91. body = JSON.generate(post_params)
  92. request = HTTParty.post(event_url, :body => body, :headers => {"Content-Type" => "application/json"})
  93. events = JSON.parse request.body
  94. create_event :payload => events
  95. end
  96. def event_url
  97. endpoint = 'https://www.googleapis.com/qpxExpress/v1/trips/search?key=' + "#{URI.encode(interpolated[:qpx_api_key].to_s)}"
  98. end
  99. end
  100. end