Class: AltFlights::KayakWrapper
- Inherits:
-
Object
- Object
- AltFlights::KayakWrapper
- Defined in:
- lib/altflights/kayak_wrapper.rb
Constant Summary collapse
- HOSTNAME =
'api.kayak.com'- PORT =
80
Instance Attribute Summary collapse
-
#last_count ⇒ Object
Returns the value of attribute last_count.
-
#searchid ⇒ Object
Returns the value of attribute searchid.
-
#sid ⇒ Object
Returns the value of attribute sid.
Class Method Summary collapse
-
.check_results(body) ⇒ Object
Load the xml feed from kayak.com and return the value of the more attribute which indicates whether kayak is still processing the request.
- .handle_results(body) ⇒ Object
- .poll_results(sid, searchid, count) ⇒ Object
Instance Method Summary collapse
- #api_key ⇒ Object
- #get_session ⇒ Object
-
#start_flight_search(oneway, origin, destination, dep_date, dep_time, ret_date, ret_time, travelers, cabin) ⇒ Object
y or n.
- #start_search(url) ⇒ Object
Instance Attribute Details
#last_count ⇒ Object
Returns the value of attribute last_count.
5 6 7 |
# File 'lib/altflights/kayak_wrapper.rb', line 5 def last_count @last_count end |
#searchid ⇒ Object
Returns the value of attribute searchid.
5 6 7 |
# File 'lib/altflights/kayak_wrapper.rb', line 5 def searchid @searchid end |
#sid ⇒ Object
Returns the value of attribute sid.
5 6 7 |
# File 'lib/altflights/kayak_wrapper.rb', line 5 def sid @sid end |
Class Method Details
.check_results(body) ⇒ Object
Load the xml feed from kayak.com and return the value of the more attribute which indicates whether kayak is still processing the request.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/altflights/kayak_wrapper.rb', line 76 def self.check_results(body) parser = LibXML::XML::Parser.string(body) doc = parser.parse more = doc.find('/searchresult/morepending') logger = "count: #{last_count = doc.find('/searchresult/count').first.inner_xml}" if more more = more.first.inner_xml end if more == 'true' return 0 else return last_count end end |
.handle_results(body) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/altflights/kayak_wrapper.rb', line 93 def self.handle_results(body) parser = LibXML::XML::Parser.string(body) doc = parser.parse logger = "alternate flight count: #{doc.find('/searchresult/count').first.inner_xml}" results = [] doc.find("/searchresult/trips/trip").each do |t| results << AltFlights::Booking.new results.last.price_url = t.find('price').first.attributes['url'] results.last.price = t.find('price').first.inner_xml t.find('legs/leg').each do |l| results.last.airline = l.find('airline').first.inner_xml results.last.airline_display = l.find('airline_display').first.inner_xml results.last.origin = l.find("orig").first.inner_xml results.last.destination = l.find("dest").first.inner_xml results.last.departure_time = DateTime.strptime(l.find("depart").first.inner_xml, "%Y/%m/%d %H:%M") results.last.arrival_time = DateTime.strptime(l.find("arrive").first.inner_xml, "%Y/%m/%d %H:%M") results.last.stops = l.find("stops").first.inner_xml results.last.duration = l.find("duration_minutes").first.inner_xml results.last.cabin = l.find("cabin").first.inner_xml l.find('segment').each do |s| results.last.flights << AltFlights::Flight.new results.last.flights.last.airline = s.find("airline").first.inner_xml results.last.flights.last.flight = s.find("flight").first.inner_xml results.last.flights.last.duration = s.find("duration_minutes").first.inner_xml results.last.flights.last.equipment = s.find("equip").first.inner_xml results.last.flights.last.miles = s.find("miles").first.inner_xml results.last.flights.last.departure_time = DateTime.strptime(s.find("dt").first.inner_xml, "%Y/%m/%d %H:%M") results.last.flights.last.origin = s.find("o").first.inner_xml results.last.flights.last.arrival_time = DateTime.strptime(s.find("at").first.inner_xml, "%Y/%m/%d %H:%M") results.last.flights.last.destination = s.find("d").first.inner_xml results.last.flights.last.cabin = s.find("cabin").first.inner_xml end end end return results end |
.poll_results(sid, searchid, count) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/altflights/kayak_wrapper.rb', line 57 def self.poll_results(sid, searchid, count) url = "/s/apibasic/flight?searchid=#{searchid}&apimode=1&_sid_=#{sid}" Net::HTTP.start(HOSTNAME, PORT) do |http| if count url += "&c=#{count}" end response = http.get(url) logger = "http://#{HOSTNAME}#{url}" body = response.body if count return handle_results(body) else return check_results(body) end end end |
Instance Method Details
#api_key ⇒ Object
7 8 9 |
# File 'lib/altflights/kayak_wrapper.rb', line 7 def api_key KAYAK_API_KEY end |
#get_session ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/altflights/kayak_wrapper.rb', line 11 def get_session self.sid = nil Net::HTTP.start(HOSTNAME, PORT) do |http| response = http.get("/k/ident/apisession?token=#{api_key}") logger = "http://#{HOSTNAME}/k/ident/apisession?token=#{api_key}" body = response.body parser = LibXML::XML::Parser.string(body) doc = parser.parse self.sid = doc.find('//sid').first.inner_xml end end |
#start_flight_search(oneway, origin, destination, dep_date, dep_time, ret_date, ret_time, travelers, cabin) ⇒ Object
y or n
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/altflights/kayak_wrapper.rb', line 23 def start_flight_search(oneway, # y or n origin, destination, dep_date, dep_time, # Values:"a" = any time; "r"=early morning; "m"=morning; "12"=noon; "n"=afternoon; "e"=evening; "l"=night ret_date, ret_time, travelers, cabin) # f, b, or e url = "/s/apisearch?basicmode=true&oneway=#{oneway}&origin=#{origin}&destination=#{destination}&destcode=&depart_date=#{dep_date}&depart_time=#{dep_time}&return_date=#{ret_date}&return_time=#{ret_time}&travelers=#{travelers}&cabin=#{cabin}&action=doflights&apimode=1&_sid_=#{sid}" return start_search(url) end |
#start_search(url) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/altflights/kayak_wrapper.rb', line 36 def start_search(url) self.searchid = nil Net::HTTP.start(HOSTNAME, PORT) do |http| response = http.get(url) logger = "http://#{HOSTNAME}#{url}" body = response.body parser = LibXML::XML::Parser.string(body) doc = parser.parse self.searchid = doc.find('//searchid') if searchid.first != nil self.searchid = self.searchid.first.inner_xml logger = "searchid: #{self.searchid}" else puts "search error:" puts body return nil end end end |