altflights 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/altflights/kayak_wrapper.rb +20 -20
- data/lib/altflights.rb +16 -14
- metadata +2 -2
@@ -2,27 +2,25 @@ class AltFlights::KayakWrapper
|
|
2
2
|
HOSTNAME = 'api.kayak.com'
|
3
3
|
PORT = 80
|
4
4
|
|
5
|
-
attr_accessor :last_count
|
5
|
+
attr_accessor :last_count, :sid, :searchid
|
6
6
|
|
7
7
|
def api_key
|
8
8
|
KAYAK_API_KEY
|
9
9
|
end
|
10
10
|
|
11
11
|
def get_session
|
12
|
-
sid = nil
|
12
|
+
self.sid = nil
|
13
13
|
Net::HTTP.start(HOSTNAME, PORT) do |http|
|
14
14
|
response = http.get("/k/ident/apisession?token=#{api_key}")
|
15
15
|
Rails.logger.info "http://#{HOSTNAME}/k/ident/apisession?token=#{api_key}"
|
16
16
|
body = response.body
|
17
17
|
parser = LibXML::XML::Parser.string(body)
|
18
18
|
doc = parser.parse
|
19
|
-
sid = doc.find('//sid').first.inner_xml
|
19
|
+
self.sid = doc.find('//sid').first.inner_xml
|
20
20
|
end
|
21
|
-
sid
|
22
21
|
end
|
23
22
|
|
24
|
-
def start_flight_search(
|
25
|
-
oneway, # y or n
|
23
|
+
def start_flight_search(oneway, # y or n
|
26
24
|
origin,
|
27
25
|
destination,
|
28
26
|
dep_date,
|
@@ -36,35 +34,33 @@ class AltFlights::KayakWrapper
|
|
36
34
|
end
|
37
35
|
|
38
36
|
def start_search(url)
|
39
|
-
searchid = nil
|
37
|
+
self.searchid = nil
|
40
38
|
Net::HTTP.start(HOSTNAME, PORT) do |http|
|
41
39
|
response = http.get(url)
|
42
|
-
|
40
|
+
Rails.logger.info "http://#{HOSTNAME}#{url}"
|
43
41
|
body = response.body
|
44
42
|
parser = LibXML::XML::Parser.string(body)
|
45
43
|
doc = parser.parse
|
46
|
-
|
44
|
+
self.searchid = doc.find('//searchid')
|
47
45
|
if searchid.first != nil
|
48
|
-
searchid = searchid.first.inner_xml
|
46
|
+
Rails.logger.info "searchid: #{self.searchid = self.searchid.first.inner_xml}"
|
49
47
|
else
|
50
|
-
|
51
|
-
|
48
|
+
Rails.logger.info "search error:"
|
49
|
+
Rails.logger.info body
|
52
50
|
return nil
|
53
51
|
end
|
54
52
|
end
|
55
|
-
return searchid
|
56
53
|
end
|
57
54
|
|
58
55
|
|
59
|
-
def poll_results(sid, searchid, count)
|
56
|
+
def self.poll_results(sid, searchid, count)
|
60
57
|
url = "/s/apibasic/flight?searchid=#{searchid}&apimode=1&_sid_=#{sid}"
|
61
|
-
more = nil
|
62
58
|
Net::HTTP.start(HOSTNAME, PORT) do |http|
|
63
59
|
if count
|
64
60
|
url += "&c=#{count}"
|
65
61
|
end
|
66
62
|
response = http.get(url)
|
67
|
-
|
63
|
+
Rails.logger.info "http://#{HOSTNAME}#{url}"
|
68
64
|
body = response.body
|
69
65
|
if count
|
70
66
|
return handle_results(body)
|
@@ -76,20 +72,24 @@ class AltFlights::KayakWrapper
|
|
76
72
|
|
77
73
|
# Load the xml feed from kayak.com and return the value of the more attribute
|
78
74
|
# which indicates whether kayak is still processing the request.
|
79
|
-
def check_results(body)
|
75
|
+
def self.check_results(body)
|
80
76
|
parser = LibXML::XML::Parser.string(body)
|
81
77
|
doc = parser.parse
|
82
78
|
more = doc.find('/searchresult/morepending')
|
83
79
|
|
84
|
-
Rails.logger.info "count: #{
|
80
|
+
Rails.logger.info "count: #{last_count = doc.find('/searchresult/count').first.inner_xml}"
|
85
81
|
if more
|
86
82
|
more = more.first.inner_xml
|
87
83
|
end
|
88
84
|
|
89
|
-
|
85
|
+
if more == 'false'
|
86
|
+
return 0
|
87
|
+
else
|
88
|
+
return last_count
|
89
|
+
end
|
90
90
|
end
|
91
91
|
|
92
|
-
def handle_results(body)
|
92
|
+
def self.handle_results(body)
|
93
93
|
parser = LibXML::XML::Parser.string(body)
|
94
94
|
doc = parser.parse
|
95
95
|
|
data/lib/altflights.rb
CHANGED
@@ -4,24 +4,26 @@ require 'net/https'
|
|
4
4
|
module AltFlights
|
5
5
|
|
6
6
|
class << self
|
7
|
-
|
8
|
-
def
|
7
|
+
|
8
|
+
def start_search(origin, destination, date_string) # date_string format => '%m/%d/%y'
|
9
9
|
kayak = AltFlights::KayakWrapper.new
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
10
|
+
kayak.get_session()
|
11
|
+
kayak.start_flight_search('y', origin, destination, date_string, 'a', nil, 'a', '1', 'e')
|
12
|
+
|
13
|
+
return kayak.to_json
|
14
|
+
end
|
15
|
+
|
16
|
+
def check_search(searchid, sid)
|
17
|
+
return AltFlights::KayakWrapper.poll_results(searchid, sid, nil)
|
18
|
+
end
|
19
|
+
|
20
|
+
def get_flights(searchid, sid, count) # date_string format => '%m/%d/%y'
|
19
21
|
|
20
|
-
if
|
21
|
-
|
22
|
+
if count.to_i > 50
|
23
|
+
count = 50
|
22
24
|
end
|
23
25
|
|
24
|
-
results =
|
26
|
+
results = AltFlights::KayakWrapper.poll_results(searchid, sid, count)
|
25
27
|
|
26
28
|
return results
|
27
29
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: altflights
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- FlightCaster
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date:
|
13
|
+
date: 2010-01-06 00:00:00 -08:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|