@@ -0,0 +1,73 @@ |
||
| 1 |
+module Agents |
|
| 2 |
+ class AdiosoAgent < Agent |
|
| 3 |
+ cannot_receive_events! |
|
| 4 |
+ |
|
| 5 |
+ default_schedule "every_1d" |
|
| 6 |
+ |
|
| 7 |
+ description <<-MD |
|
| 8 |
+ The Adioso Agent will tell you the minimum airline prices between a pair of cities, and within a certain period of time. |
|
| 9 |
+ |
|
| 10 |
+ The currency is USD. Please make sure that the difference between `start_date` and `end_date` is less than 150 days. You will need to contact [Adioso](http://adioso.com/) |
|
| 11 |
+ for a `username` and `password`. |
|
| 12 |
+ MD |
|
| 13 |
+ |
|
| 14 |
+ event_description <<-MD |
|
| 15 |
+ If flights are present then events look like: |
|
| 16 |
+ |
|
| 17 |
+ {
|
|
| 18 |
+ "cost": 75.23, |
|
| 19 |
+ "date": "June 25, 2013", |
|
| 20 |
+ "route": "New York to Chicago" |
|
| 21 |
+ } |
|
| 22 |
+ |
|
| 23 |
+ otherwise |
|
| 24 |
+ |
|
| 25 |
+ {
|
|
| 26 |
+ "nonetodest": "No flights found to the specified destination" |
|
| 27 |
+ } |
|
| 28 |
+ MD |
|
| 29 |
+ |
|
| 30 |
+ def default_options |
|
| 31 |
+ {
|
|
| 32 |
+ 'start_date' => Date.today.httpdate[0..15], |
|
| 33 |
+ 'end_date' => Date.today.plus_with_duration(100).httpdate[0..15], |
|
| 34 |
+ 'from' => "New York", |
|
| 35 |
+ 'to' => "Chicago", |
|
| 36 |
+ 'username' => "xx", |
|
| 37 |
+ 'password' => "xx", |
|
| 38 |
+ 'expected_update_period_in_days' => "1" |
|
| 39 |
+ } |
|
| 40 |
+ end |
|
| 41 |
+ |
|
| 42 |
+ def working? |
|
| 43 |
+ event_created_within?(options['expected_update_period_in_days']) && !recent_error_logs? |
|
| 44 |
+ end |
|
| 45 |
+ |
|
| 46 |
+ def validate_options |
|
| 47 |
+ unless %w[start_date end_date from to username password expected_update_period_in_days].all? { |field| options[field].present? }
|
|
| 48 |
+ errors.add(:base, "All fields are required") |
|
| 49 |
+ end |
|
| 50 |
+ end |
|
| 51 |
+ |
|
| 52 |
+ def date_to_unix_epoch(date) |
|
| 53 |
+ date.to_time.to_i |
|
| 54 |
+ end |
|
| 55 |
+ |
|
| 56 |
+ def check |
|
| 57 |
+ auth_options = {:basic_auth => {:username =>interpolated[:username], :password=>interpolated['password']}}
|
|
| 58 |
+ parse_response = HTTParty.get "http://api.adioso.com/v2/search/parse?q=#{URI.encode(interpolated['from'])}+to+#{URI.encode(interpolated['to'])}", auth_options
|
|
| 59 |
+ fare_request = parse_response["search_url"].gsub /(end=)(\d*)([^\d]*)(\d*)/, "\\1#{date_to_unix_epoch(interpolated['end_date'])}\\3#{date_to_unix_epoch(interpolated['start_date'])}"
|
|
| 60 |
+ fare = HTTParty.get fare_request, auth_options |
|
| 61 |
+ |
|
| 62 |
+ if fare["warnings"] |
|
| 63 |
+ create_event :payload => fare["warnings"] |
|
| 64 |
+ else |
|
| 65 |
+ event = fare["results"].min {|a,b| a["cost"] <=> b["cost"]}
|
|
| 66 |
+ event["date"] = Time.at(event["date"]).to_date.httpdate[0..15] |
|
| 67 |
+ event["route"] = "#{interpolated['from']} to #{interpolated['to']}"
|
|
| 68 |
+ create_event :payload => event |
|
| 69 |
+ end |
|
| 70 |
+ end |
|
| 71 |
+ end |
|
| 72 |
+end |
|
| 73 |
+ |