Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added round trip option to google flight agent #1384

Merged
merged 2 commits into from
Mar 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 21 additions & 3 deletions app/models/agents/google_flights_agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ class GoogleFlightsAgent < Agent

All the default options must exist. For `infantInSeatCount`, `infantInLapCount`, `seniorCount`, and `childCount`, leave them to the default value of `0` if its not necessary.

Make sure `date` is in this type of date format `YYYY-MO-DAY`.
Make sure `date` and `return_date` is in this type of date format `YYYY-MM-DAY`.

You can choose one way ticket only by setting `roundtrip` to `false`.

You can limit the number of `solutions` returned back. The first solution is the lowest priced ticket.
MD
Expand Down Expand Up @@ -56,6 +58,8 @@ def default_options
'infantInSeatCount' => 0,
'infantInLapCount'=> 0,
'seniorCount'=> 0,
'return_date' => '2016-04-18',
'roundtrip' => true,
'solutions'=> 3
}
end
Expand All @@ -69,8 +73,10 @@ def default_options
form_configurable :infantInSeatCount
form_configurable :infantInLapCount
form_configurable :seniorCount
form_configurable :roundtrip, type: :boolean
form_configurable :return_date, type: :string
form_configurable :solutions

def validate_options
errors.add(:base, "You need a qpx api key") unless options['qpx_api_key'].present?
errors.add(:base, "Adult Count must exist") unless options['adultCount'].present?
Expand All @@ -82,14 +88,26 @@ def validate_options
errors.add(:base, "Infant In Lap Count") unless options['infantInLapCount'].present?
errors.add(:base, "Senior Count must exist") unless options['seniorCount'].present?
errors.add(:base, "Solutions must exist") unless options['solutions'].present?
errors.add(:base, "Returned Date must exist") if options["return_date"].blank? && boolify(options['roundtrip'])
end

def working?
!recent_error_logs?
end

def round_trip?
boolify(interpolated['roundtrip'])
end

def post_params
if round_trip?
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"]}}
else
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"]}}
end
end

def check
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"]}}
body = JSON.generate(post_params)
request = HTTParty.post(event_url, :body => body, :headers => {"Content-Type" => "application/json"})
events = JSON.parse request.body
Expand Down
6 changes: 6 additions & 0 deletions spec/models/agents/google_flights_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@
@checker.options['solutions'] = nil
expect(@checker).not_to be_valid
end

it "should require Returned Date" do
@checker.options['roundtrip'] = true
@checker.options['return_date'] = nil
expect(@checker).not_to be_valid
end
end

describe '#check' do
Expand Down