flightcaster 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,44 @@
1
+ FlightCaster
2
+ ============
3
+
4
+ The [FlightCaster](http://flightcaster.com) ruby gem. Get started by [signing up for a free API
5
+ key](http://docs.flightcaster.com/).
6
+
7
+ ## Examples
8
+
9
+ # load your API key
10
+ flightcaster = FlightCaster.new('api_key')
11
+
12
+ # retrieve all flights
13
+ flights = flightcaster.flights
14
+
15
+ # get flights by airline and flight number
16
+ flight = flightcaster.flights_by_airline('VX', 28)
17
+
18
+ See more in the [examples
19
+ directory](http://github.com/jarodluebbert/flightcaster/tree/master/examples/).
20
+
21
+ ## Docs
22
+
23
+ [View the
24
+ documentation](http://rdoc.info/projects/jarodluebbert/flightcaster).
25
+
26
+ ### todo
27
+
28
+ * Load API key from config file
29
+ * Authenticate with user/pass
30
+
31
+ ### Note on Patches/Pull Requests
32
+
33
+ * Fork the project.
34
+ * Make your feature addition or bug fix.
35
+ * Add tests for it. This is important so I don't break it in a
36
+ future version unintentionally.
37
+ * Commit, do not mess with rakefile, version, or history.
38
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
39
+ * Send me a pull request. Bonus points for topic branches.
40
+
41
+ ### Copyright
42
+
43
+ Copyright (c) 2010 Jarod Luebbert. See LICENSE for details.
44
+
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.2.0
@@ -5,31 +5,31 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{flightcaster}
8
- s.version = "0.1.1"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Jarod Luebbert"]
12
- s.date = %q{2010-02-26}
12
+ s.date = %q{2010-03-01}
13
13
  s.description = %q{Simple interaction with the flightcaster API. Look up
14
14
  flight predictions and information.}
15
15
  s.email = %q{jarod.luebbert@gmail.com}
16
16
  s.extra_rdoc_files = [
17
17
  "LICENSE",
18
- "README.rdoc"
18
+ "README.md"
19
19
  ]
20
20
  s.files = [
21
21
  ".document",
22
22
  ".gitignore",
23
23
  "LICENSE",
24
- "README.rdoc",
24
+ "README.md",
25
25
  "Rakefile",
26
26
  "VERSION",
27
27
  "examples/flights.rb",
28
28
  "flightcaster.gemspec",
29
29
  "lib/flightcaster.rb",
30
30
  "lib/flightcaster/base.rb",
31
+ "lib/flightcaster/rash.rb",
31
32
  "lib/flightcaster/request.rb",
32
- "lib/flightcaster/result.rb",
33
33
  "test/fixtures/airline.xml",
34
34
  "test/fixtures/airline_flight.xml",
35
35
  "test/fixtures/airline_flights.xml",
@@ -60,7 +60,8 @@ Gem::Specification.new do |s|
60
60
  "test/helper.rb",
61
61
  "test/test_base.rb",
62
62
  "test/test_flightcaster.rb",
63
- "test/test_request.rb"
63
+ "test/test_request.rb",
64
+ "test/test_result.rb"
64
65
  ]
65
66
  s.homepage = %q{http://github.com/jarodluebbert/flightcaster}
66
67
  s.rdoc_options = ["--charset=UTF-8"]
@@ -72,6 +73,7 @@ Gem::Specification.new do |s|
72
73
  "test/test_base.rb",
73
74
  "test/test_flightcaster.rb",
74
75
  "test/test_request.rb",
76
+ "test/test_result.rb",
75
77
  "examples/flights.rb"
76
78
  ]
77
79
 
@@ -80,14 +82,14 @@ Gem::Specification.new do |s|
80
82
  s.specification_version = 3
81
83
 
82
84
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
83
- s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
85
+ s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 2.10.2"])
84
86
  s.add_development_dependency(%q<shoulda>, [">= 2.10.2"])
85
87
  s.add_development_dependency(%q<mhennemeyer-matchy>, [">= 0.3.3"])
86
88
  s.add_development_dependency(%q<fakeweb>, [">= 1.2.8"])
87
89
  s.add_runtime_dependency(%q<httparty>, [">= 0.5.2"])
88
90
  s.add_runtime_dependency(%q<hashie>, [">= 0.1.8"])
89
91
  else
90
- s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
92
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 2.10.2"])
91
93
  s.add_dependency(%q<shoulda>, [">= 2.10.2"])
92
94
  s.add_dependency(%q<mhennemeyer-matchy>, [">= 0.3.3"])
93
95
  s.add_dependency(%q<fakeweb>, [">= 1.2.8"])
@@ -95,7 +97,7 @@ Gem::Specification.new do |s|
95
97
  s.add_dependency(%q<hashie>, [">= 0.1.8"])
96
98
  end
97
99
  else
98
- s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
100
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 2.10.2"])
99
101
  s.add_dependency(%q<shoulda>, [">= 2.10.2"])
100
102
  s.add_dependency(%q<mhennemeyer-matchy>, [">= 0.3.3"])
101
103
  s.add_dependency(%q<fakeweb>, [">= 1.2.8"])
@@ -1,12 +1,10 @@
1
1
  require 'forwardable'
2
- gem 'httparty', '~> 0.5.2'
3
2
  require 'httparty'
4
- gem 'hashie', '~> 0.1.8'
5
3
  require 'hashie'
6
4
 
7
5
  [ "base",
8
6
  "request",
9
- "result" ].each do |file|
7
+ "rash" ].each do |file|
10
8
  require File.join(File.dirname(__FILE__), 'flightcaster', file)
11
9
  end
12
10
 
@@ -22,7 +22,7 @@ module FlightCaster
22
22
  perform_get("/airlines/#{id}.xml")
23
23
  end
24
24
 
25
- # Params
25
+ # params
26
26
  # :per_page => The number of results per page. Defaults to 30.
27
27
  # :page => The page of results displayed.
28
28
  def airports(params={})
@@ -61,87 +61,135 @@ module FlightCaster
61
61
  perform_get("/flights/#{id}.xml")
62
62
  end
63
63
 
64
- # Get all flights for a sepcific airline on a given date.
65
- def flights_by_airline_on(airline_id, flight_number, date, params={})
66
- t = convert_date(date)
67
- perform_get("/airlines/#{airline_id}/flights/#{flight_number}/#{t}.xml", params)
68
- end
69
-
70
- # Get all flights from one airport to another.
71
- def flight_path(from, to, params={})
72
- perform_get("/airports/#{from}/departures/#{to}.xml", params)
73
- end
74
-
75
- # Get all flights from one airport to another on a given date.
76
- def flight_path_on(from, to, date, params={})
77
- t = convert_date(date)
78
- perform_get("/airports/#{from}/departures/#{to}/#{t}.xml", params)
79
- end
80
-
64
+ # params
65
+ # :per_page => The number of results per page. Defaults to 30.
66
+ # :page => The page of results displayed.
81
67
  def flights(params={})
82
68
  perform_get('/flights.xml', params)
83
69
  end
84
70
 
85
- def flights_by_airline(id, params={})
86
- perform_get("/airlines/#{id}/flights.xml", params)
87
- end
88
-
89
- def flight_by_airline(airline_id, flight_number, params={})
90
- perform_get("/airlines/#{airline_id}/flights/#{flight_number}.xml", params)
91
- end
92
-
71
+ # airline_id
72
+ # flight_number
73
+ # date
74
+ # A Ruby Time object
75
+ # or a date in the format 'yearmonthday' (ex: 20100226)
76
+ # params
77
+ # :per_page => The number of results per page. Defaults to 30.
78
+ # :page => The page of results displayed.
79
+ def flights_by_airline(airline_id, flight_number=nil, date=nil, params={})
80
+ args = [airline_id, 'flights', flight_number, format(date)].compact.join('/')
81
+ perform_get("/airlines/#{args}.xml", params)
82
+ end
83
+
84
+ # origin
85
+ # The FlightCaster id of the airport
86
+ # The 3 character airport IATA id
87
+ # The 4 character airport ICAO id
88
+ # destination
89
+ # The FlightCaster id of the airport
90
+ # The 3 character airport IATA id
91
+ # The 4 character airport ICAO id
92
+ # date
93
+ # A Ruby Time object
94
+ # A date in the format 'yearmonthday' (ex: 20100226)
95
+ # Returns all flights going from one airport to another
96
+ def flight_route(origin, destination, date=nil, params={})
97
+ args = [origin, 'departures', destination, format(date)].compact.join('/')
98
+ perform_get("/airports/#{args}.xml", params)
99
+ end
100
+
101
+ # params
102
+ # :per_page => The number of results per page. Defaults to 30.
103
+ # :page => The page of results displayed.
104
+ # Returns all metars
93
105
  def metars(params={})
94
106
  perform_get('/metars.xml', params)
95
107
  end
96
108
 
109
+ # id
110
+ # The FlightCaster id
111
+ # The 4 character metar ICAO id. If given the metar that is returned
112
+ # is the last one reported at that station.
113
+ # Returns metar specified by the given id.
97
114
  def metar(id)
98
115
  perform_get("/metars/#{id}.xml")
99
116
  end
100
117
 
118
+ # params
119
+ # :per_page => The number of results per page. Defaults to 30.
120
+ # :page => The page of results displayed.
121
+ # Returns all tafs
101
122
  def tafs(params={})
102
123
  perform_get('/tafs.xml', params)
103
124
  end
104
125
 
126
+ # id
127
+ # The FlightCaster id
128
+ # The 4 character taf ICAO id. If given the taf that is returned
129
+ # is the last one reported at that station.
130
+ # Returns taf specified by the given id.
105
131
  def taf(id)
106
132
  perform_get("/tafs/#{id}.xml")
107
133
  end
108
134
 
135
+ # params
136
+ # :per_page => The number of results per page. Defaults to 30.
137
+ # :page => The page of results displayed.
138
+ # Returns a list of ground delays
109
139
  def ground_delays(params={})
110
140
  perform_get('/ground_delays.xml', params)
111
141
  end
112
142
 
143
+ # id
144
+ # The FlightCaster id
145
+ # The 4 character delay ICAO id. If given, the delay that is returned
146
+ # is the last one reported at that station.
147
+ # Returns a single ground delay.
113
148
  def ground_delay(id)
114
149
  perform_get("/ground_delays/#{id}.xml")
115
150
  end
116
151
 
152
+ # Returns all ground stops, limited to 50 results.
117
153
  def ground_stops(params={})
118
154
  perform_get('/ground_stops.xml', params)
119
155
  end
120
156
 
157
+ # id
158
+ # The FlightCaster id
159
+ # The 4 character stop ICAO id. If given, the stop that is returned
160
+ # is the last one reported at that station.
161
+ # Returns a single ground stop.
121
162
  def ground_stop(id)
122
163
  perform_get("/ground_stops/#{id}.xml")
123
164
  end
124
165
 
166
+ # params
167
+ # :per_page => The number of results per page. Defaults to 30.
168
+ # :page => The page of results displayed.
169
+ # Returns all delays.
125
170
  def delays(params={})
126
171
  perform_get('/delays.xml', params)
127
172
  end
128
173
 
174
+ # id
175
+ # The FlightCaster id
176
+ # The 4 character delay ICAO id. If given, the delay that is returned
177
+ # is the last one reported at that station.
178
+ # Returns a single delay.
129
179
  def delay(id)
130
180
  perform_get("/delays/#{id}.xml")
131
181
  end
132
182
 
133
183
  private
134
184
 
185
+ # :nodoc
135
186
  def perform_get(path, params={})
136
187
  FlightCaster::Request.get(path, params)
137
188
  end
138
189
 
139
- def convert_date(date)
140
- if date.class == Time
141
- date.strftime("%Y%m%d")
142
- else
143
- date
144
- end
190
+ # :nodoc
191
+ def format(date)
192
+ (date.is_a?(Time)) ? date.strftime("%Y%m%d") : date
145
193
  end
146
194
 
147
195
  end
@@ -0,0 +1,31 @@
1
+ module FlightCaster
2
+ class Rash
3
+ extend Forwardable
4
+
5
+ attr_accessor :hash, :array
6
+
7
+ def initialize(h)
8
+ @array = []
9
+ @hash = h
10
+ split_into_rash
11
+ end
12
+
13
+ def_delegators :@hash, :id, :replace, :each_key, :keys, :delete,
14
+ :merge!, :method_missing
15
+ def_delegators :@array, :each, :size, :[]
16
+
17
+ private
18
+
19
+ def split_into_rash
20
+ @hash.each_key do |key|
21
+ if @hash[key].class == Array
22
+ @hash[key].each {|x| @array << x}
23
+ @hash.delete(key)
24
+ elsif @hash[key].class == Hashie::Mash
25
+ @hash[key] = Rash.new(@hash[key])
26
+ end
27
+ end
28
+ end
29
+
30
+ end
31
+ end
@@ -22,7 +22,9 @@ module FlightCaster
22
22
  begin
23
23
  raise_errors(response)
24
24
  data = parse(response)
25
- FlightCaster::Result.new(data)
25
+ h = Hashie::Mash.new(data)
26
+ h = h[h.keys[0]]
27
+ FlightCaster::Rash.new(h)
26
28
  rescue
27
29
  end
28
30
  end
@@ -13,7 +13,7 @@ Age: 0
13
13
  Via: 1.1 varnish
14
14
 
15
15
  <?xml version="1.0" encoding="utf-8"?>
16
- <flights>
16
+ <flights current-page="1" total-entries="504" total-pages="17">
17
17
  <flight>
18
18
  <actual-air-time nil="true" type="integer"/>
19
19
  <actual-aircraft-type nil="true"/>
@@ -107,27 +107,28 @@ class TestFlightCaster < Test::Unit::TestCase
107
107
 
108
108
  should "get flights by airline and flight number" do
109
109
  stub_get('/airlines/VX/flights/28.xml', 'airline_flight.xml')
110
- flights = @flightcaster.flight_by_airline('VX', 28)
110
+ flights = @flightcaster.flights_by_airline('VX', 28)
111
111
  flights.total_entries.should == '5'
112
+ flights.size.should == 5
112
113
  flights.total_pages.should == '1'
113
114
  end
114
115
 
115
116
  should "get flights by airline, flight number, and date" do
116
117
  stub_get('/airlines/VX/flights/28/20100226.xml', 'flight_date.xml')
117
- flights = @flightcaster.flights_by_airline_on('VX', 28, '20100226')
118
+ flights = @flightcaster.flights_by_airline('VX', 28, '20100226')
118
119
  flights.total_entries.should == '1'
119
120
  end
120
121
 
121
122
  should "find a flight from one airport to another" do
122
123
  stub_get('/airports/PDX/departures/DFW.xml', 'flight_path.xml')
123
- flights = @flightcaster.flight_path('PDX', 'DFW')
124
+ flights = @flightcaster.flight_route('PDX', 'DFW')
124
125
  flights.total_entries.should == '6'
125
126
  flights.total_pages.should == '3'
126
127
  end
127
128
 
128
129
  should "find a flight from one airport to another on a certain day" do
129
130
  stub_get('/airports/PDX/departures/DFW/20090911.xml', 'flight_path_date.xml')
130
- flights = @flightcaster.flight_path_on('PDX', 'DFW', '20090911')
131
+ flights = @flightcaster.flight_route('PDX', 'DFW', '20090911')
131
132
  flights[0].id.should == 2877686
132
133
  flights.size.should == 22
133
134
  end
@@ -143,9 +144,9 @@ class TestFlightCaster < Test::Unit::TestCase
143
144
  end
144
145
 
145
146
  should "get predictions for one flight" do
146
- #stub_get('/flights/2858102.xml', 'flight.xml')
147
- #flight = @flightcaster.flight(2858102)
148
- #flight.predictions[0].state.should == 'delayed'
147
+ stub_get('/flights/2858102.xml', 'flight.xml')
148
+ flight = @flightcaster.flight(2858102)
149
+ flight.predictions[0].state.should == 'delayed'
149
150
  end
150
151
  end
151
152
 
@@ -1,7 +1,7 @@
1
1
  require 'helper'
2
2
 
3
3
  class TestRequest < Test::Unit::TestCase
4
- context "Requesting" do
4
+ context "Request" do
5
5
  should "add API key to base_uri" do
6
6
  FlightCaster::Request.set_api_key("foo")
7
7
  FlightCaster::Request.full_uri('/').should == "http://api.flightcaster.com/?api_key=foo"
@@ -0,0 +1,49 @@
1
+ require 'helper'
2
+
3
+ class TestResult < Test::Unit::TestCase
4
+ def setup
5
+ stub_get('/flights.xml', 'flights.xml')
6
+ FlightCaster::Request.set_api_key('foo')
7
+ @flights = FlightCaster::Request.get('/flights.xml')
8
+ end
9
+
10
+ context "Initialization" do
11
+ should "require data" do
12
+ lambda { FlightCaster::Result.new }.should raise_error
13
+ end
14
+ end
15
+
16
+ context "After initialization, Result" do
17
+ should "get rid of the outside hash" do
18
+ @flights.flights.should == nil
19
+ end
20
+
21
+ should "extract arrays from hashes" do
22
+ @flights[0].id.should == 2858102
23
+ @flights[1].id.should == 2856793
24
+ end
25
+
26
+ should "not create duplicates" do
27
+ @flights.flight.should == nil
28
+ end
29
+
30
+ should "not remove current page number" do
31
+ @flights.current_page.should == '1'
32
+ end
33
+
34
+ should "not remove number of entries" do
35
+ @flights.total_entries.should == '504'
36
+ end
37
+
38
+ should "not remove number of pages" do
39
+ @flights.total_pages.should == '17'
40
+ end
41
+
42
+ should "return the size of the array" do
43
+ @flights.size.should == 30
44
+ end
45
+
46
+ should "not duplicate inner arrays" do
47
+ end
48
+ end
49
+ end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
8
- - 1
9
- version: 0.1.1
7
+ - 2
8
+ - 0
9
+ version: 0.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Jarod Luebbert
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-02-26 00:00:00 -06:00
17
+ date: 2010-03-01 00:00:00 -06:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -25,8 +25,10 @@ dependencies:
25
25
  - - ">="
26
26
  - !ruby/object:Gem::Version
27
27
  segments:
28
- - 0
29
- version: "0"
28
+ - 2
29
+ - 10
30
+ - 2
31
+ version: 2.10.2
30
32
  type: :development
31
33
  version_requirements: *id001
32
34
  - !ruby/object:Gem::Dependency
@@ -109,20 +111,20 @@ extensions: []
109
111
 
110
112
  extra_rdoc_files:
111
113
  - LICENSE
112
- - README.rdoc
114
+ - README.md
113
115
  files:
114
116
  - .document
115
117
  - .gitignore
116
118
  - LICENSE
117
- - README.rdoc
119
+ - README.md
118
120
  - Rakefile
119
121
  - VERSION
120
122
  - examples/flights.rb
121
123
  - flightcaster.gemspec
122
124
  - lib/flightcaster.rb
123
125
  - lib/flightcaster/base.rb
126
+ - lib/flightcaster/rash.rb
124
127
  - lib/flightcaster/request.rb
125
- - lib/flightcaster/result.rb
126
128
  - test/fixtures/airline.xml
127
129
  - test/fixtures/airline_flight.xml
128
130
  - test/fixtures/airline_flights.xml
@@ -154,6 +156,7 @@ files:
154
156
  - test/test_base.rb
155
157
  - test/test_flightcaster.rb
156
158
  - test/test_request.rb
159
+ - test/test_result.rb
157
160
  has_rdoc: true
158
161
  homepage: http://github.com/jarodluebbert/flightcaster
159
162
  licenses: []
@@ -189,4 +192,5 @@ test_files:
189
192
  - test/test_base.rb
190
193
  - test/test_flightcaster.rb
191
194
  - test/test_request.rb
195
+ - test/test_result.rb
192
196
  - examples/flights.rb
@@ -1,26 +0,0 @@
1
- = flightcaster
2
-
3
- The flightcaster ruby gem. Get started by going to http://docs.flightcaster.com/
4
- and getting an API key.
5
-
6
- == examples
7
-
8
- See the examples directory.
9
-
10
- == docs
11
-
12
- http://rdoc.info/projects/jarodluebbert/flightcaster
13
-
14
- == Note on Patches/Pull Requests
15
-
16
- * Fork the project.
17
- * Make your feature addition or bug fix.
18
- * Add tests for it. This is important so I don't break it in a
19
- future version unintentionally.
20
- * Commit, do not mess with rakefile, version, or history.
21
- (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
22
- * Send me a pull request. Bonus points for topic branches.
23
-
24
- == Copyright
25
-
26
- Copyright (c) 2010 Jarod Luebbert. See LICENSE for details.
@@ -1,51 +0,0 @@
1
- module FlightCaster
2
- class Result
3
- extend Forwardable
4
-
5
- def initialize(data)
6
- @h = generate_hash(data)
7
- end
8
-
9
- def_delegators :@h, :id, :replace, :each_key, :keys, :delete,
10
- :[], :[]=, :merge!, :method_missing
11
-
12
- # When enumerating a hash, look for an array with the
13
- # response results and pass it the block.
14
- def each(&blk)
15
- @h.results.each(&blk)
16
- end
17
-
18
- def [](key)
19
- @h.results[key]
20
- end
21
-
22
- def size
23
- @h.results.size
24
- end
25
-
26
- private
27
-
28
- def generate_hash(data)
29
- h = Hashie::Mash.new(data)
30
- # since the hash looks like { :airlines => { stuff we want } },
31
- # we just grab the value from the first key
32
- h = h[h.keys[0]]
33
- # now we have:
34
- # { :total_entries => 1, ... , :data_requested => [array of hashes], }
35
- # for example if the user requests `.flights` then in order to
36
- # iterate over all flights `.flights.flight.each` would be needed.
37
- # To get around this, we need to grab out that array of hashes
38
- # and store it in a general location so we can overload `.each`
39
- h.each_key do |key|
40
- if h[key].class == Array
41
- results = []
42
- h[key].each do |item|
43
- results << item
44
- end
45
- h.delete(key)
46
- h.merge!({ :results => results })
47
- end
48
- end
49
- end
50
- end
51
- end