@@ -0,0 +1,49 @@ |
||
| 1 |
+module Agents |
|
| 2 |
+ class QpxAgent < Agent |
|
| 3 |
+ |
|
| 4 |
+ default_schedule "every_10m" |
|
| 5 |
+ |
|
| 6 |
+ description <<-MD |
|
| 7 |
+ The QpxExpressAgent will tell you the minimum airline prices between a pair of cities, and within a certain period of time. |
|
| 8 |
+ |
|
| 9 |
+ Follow their introduction documentation here (https://developers.google.com/qpx-express/v1/prereqs#get-a-google-account) to retrieve an api key. |
|
| 10 |
+ After you get to the google chrome console and enabled qpx express api, you can choose `api key` credential to be created. |
|
| 11 |
+ For round trips please provide a `return_date`. |
|
| 12 |
+ MD |
|
| 13 |
+ |
|
| 14 |
+ def default_options |
|
| 15 |
+ {
|
|
| 16 |
+ 'qpx_api_key' => 'AIzaSyCMwV5ackABmIPX9pUgEPELXB_FiNKmem0', |
|
| 17 |
+ 'date' => "2016-03-18", |
|
| 18 |
+ 'origin' => "origin", |
|
| 19 |
+ 'destination' => "destination", |
|
| 20 |
+ 'return_date' => "2016-03-25" |
|
| 21 |
+ } |
|
| 22 |
+ end |
|
| 23 |
+ |
|
| 24 |
+ def validate_options |
|
| 25 |
+ errors.add(:base, "You need a qpx api key") unless options['qpx_api_key'].present? |
|
| 26 |
+ # errors.add(:base, "A origin must exist") unless options['origin'].present? |
|
| 27 |
+ # errors.add(:base, "A destination must exist") unless options['destination'].present? |
|
| 28 |
+ # errors.add(:base, "A date must exist") unless options['date'].present? |
|
| 29 |
+ end |
|
| 30 |
+ |
|
| 31 |
+ def working? |
|
| 32 |
+ !recent_error_logs? |
|
| 33 |
+ end |
|
| 34 |
+ |
|
| 35 |
+ HEADERS = {"Content-Type" => "application/json"}
|
|
| 36 |
+ |
|
| 37 |
+ def check |
|
| 38 |
+ hash = {:request=>{:passengers=>{:adultCount=>1}, :slice=>[{:origin=>"BOS", :destination=>"LAX", :date=>"2016-03-20"}, {:origin=>"LAX", :destination=>"BOS", :date=>"2016-03-20"}]}}
|
|
| 39 |
+ body = JSON.generate(hash) |
|
| 40 |
+ request = HTTParty.post(event_url, :body => @body, :headers => HEADERS) |
|
| 41 |
+ events = JSON.parse request.body |
|
| 42 |
+ create_event :payload => events |
|
| 43 |
+ end |
|
| 44 |
+ |
|
| 45 |
+ def event_url |
|
| 46 |
+ endpoint = 'https://www.googleapis.com/qpxExpress/v1/trips/search?key' + "#{URI.encode(interpolated[:qpx_api_key].to_s)}"
|
|
| 47 |
+ end |
|
| 48 |
+ end |
|
| 49 |
+end |
@@ -0,0 +1,48 @@ |
||
| 1 |
+require 'rails_helper' |
|
| 2 |
+ |
|
| 3 |
+describe Agents::QpxExpressAgent do |
|
| 4 |
+ before do |
|
| 5 |
+ |
|
| 6 |
+ stub_request(:get, "https://www.googleapis.com/qpxExpress/v1/trips/search").to_return( |
|
| 7 |
+ :body => File.read(Rails.root.join("spec/data_fixtures/qpx_express.json")),
|
|
| 8 |
+ :status => 200, |
|
| 9 |
+ :headers => {"Content-Type" => "application/json"}
|
|
| 10 |
+ ) |
|
| 11 |
+ |
|
| 12 |
+ @opts = {
|
|
| 13 |
+ "qpx_api_key" => '800deeaf-e285-9d62-bc90-j999c1973cc9' |
|
| 14 |
+ } |
|
| 15 |
+ |
|
| 16 |
+ @checker = Agents::QpxExpressAgent.new(:name => "tectonic", :options => @opts) |
|
| 17 |
+ @checker.user = users(:bob) |
|
| 18 |
+ @checker.save! |
|
| 19 |
+ end |
|
| 20 |
+ |
|
| 21 |
+ describe '#helpers' do |
|
| 22 |
+ it "should return the correct request header" do |
|
| 23 |
+ expect(@checker.send(:request_options)).to eq({:headers => {"Content-Type"=>"application/json"}})
|
|
| 24 |
+ end |
|
| 25 |
+ |
|
| 26 |
+ it "should generate the correct events url" do |
|
| 27 |
+ expect(@checker.send(:event_url)).to eq("https://www.googleapis.com/qpxExpress/v1/trips/search?key")
|
|
| 28 |
+ end |
|
| 29 |
+ end |
|
| 30 |
+ |
|
| 31 |
+ describe "#that checker should be valid" do |
|
| 32 |
+ it "should check that the aftership object is valid" do |
|
| 33 |
+ expect(@checker).to be_valid |
|
| 34 |
+ end |
|
| 35 |
+ |
|
| 36 |
+ it "should require credentials" do |
|
| 37 |
+ @checker.options['qpx_api_key'] = nil |
|
| 38 |
+ expect(@checker).not_to be_valid |
|
| 39 |
+ end |
|
| 40 |
+ end |
|
| 41 |
+ |
|
| 42 |
+ describe '#check' do |
|
| 43 |
+ it "should check that initial run creates an event" do |
|
| 44 |
+ @checker.memory[:last_updated_at] = '2016-03-15T14:01:05+00:00' |
|
| 45 |
+ expect { @checker.check }.to change { Event.count }.by(1)
|
|
| 46 |
+ end |
|
| 47 |
+ end |
|
| 48 |
+end |