diff --git a/app/models/agents/qpx_agent.rb b/app/models/agents/qpx_agent.rb new file mode 100644 index 00000000..e68ae49e --- /dev/null +++ b/app/models/agents/qpx_agent.rb @@ -0,0 +1,49 @@ +module Agents + class QpxAgent < Agent + + default_schedule "every_10m" + + description <<-MD + The QpxExpressAgent will tell you the minimum airline prices between a pair of cities, and within a certain period of time. + + Follow their introduction documentation here (https://developers.google.com/qpx-express/v1/prereqs#get-a-google-account) to retrieve an api key. + After you get to the google chrome console and enabled qpx express api, you can choose `api key` credential to be created. + For round trips please provide a `return_date`. + MD + + def default_options + { + 'qpx_api_key' => 'AIzaSyCMwV5ackABmIPX9pUgEPELXB_FiNKmem0', + 'date' => "2016-03-18", + 'origin' => "origin", + 'destination' => "destination", + 'return_date' => "2016-03-25" + } + end + + def validate_options + errors.add(:base, "You need a qpx api key") unless options['qpx_api_key'].present? + # errors.add(:base, "A origin must exist") unless options['origin'].present? + # errors.add(:base, "A destination must exist") unless options['destination'].present? + # errors.add(:base, "A date must exist") unless options['date'].present? + end + + def working? + !recent_error_logs? + end + + HEADERS = {"Content-Type" => "application/json"} + + def check + hash = {:request=>{:passengers=>{:adultCount=>1}, :slice=>[{:origin=>"BOS", :destination=>"LAX", :date=>"2016-03-20"}, {:origin=>"LAX", :destination=>"BOS", :date=>"2016-03-20"}]}} + body = JSON.generate(hash) + request = HTTParty.post(event_url, :body => @body, :headers => HEADERS) + events = JSON.parse request.body + create_event :payload => events + end + + def event_url + endpoint = 'https://www.googleapis.com/qpxExpress/v1/trips/search?key' + "#{URI.encode(interpolated[:qpx_api_key].to_s)}" + end + end +end diff --git a/spec/models/agents/qpx_spec.rb b/spec/models/agents/qpx_spec.rb new file mode 100644 index 00000000..22f7fa99 --- /dev/null +++ b/spec/models/agents/qpx_spec.rb @@ -0,0 +1,48 @@ +require 'rails_helper' + +describe Agents::QpxExpressAgent do + before do + + stub_request(:get, "https://www.googleapis.com/qpxExpress/v1/trips/search").to_return( + :body => File.read(Rails.root.join("spec/data_fixtures/qpx_express.json")), + :status => 200, + :headers => {"Content-Type" => "application/json"} + ) + + @opts = { + "qpx_api_key" => '800deeaf-e285-9d62-bc90-j999c1973cc9' + } + + @checker = Agents::QpxExpressAgent.new(:name => "tectonic", :options => @opts) + @checker.user = users(:bob) + @checker.save! + end + + describe '#helpers' do + it "should return the correct request header" do + expect(@checker.send(:request_options)).to eq({:headers => {"Content-Type"=>"application/json"}}) + end + + it "should generate the correct events url" do + expect(@checker.send(:event_url)).to eq("https://www.googleapis.com/qpxExpress/v1/trips/search?key") + end + end + + describe "#that checker should be valid" do + it "should check that the aftership object is valid" do + expect(@checker).to be_valid + end + + it "should require credentials" do + @checker.options['qpx_api_key'] = nil + expect(@checker).not_to be_valid + end + end + + describe '#check' do + it "should check that initial run creates an event" do + @checker.memory[:last_updated_at] = '2016-03-15T14:01:05+00:00' + expect { @checker.check }.to change { Event.count }.by(1) + end + end +end