nextbus 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. data/.gitignore +22 -0
  2. data/Gemfile +6 -0
  3. data/Gemfile.lock +29 -0
  4. data/LICENSE +20 -0
  5. data/README.rdoc +17 -0
  6. data/Rakefile +27 -0
  7. data/VERSION +1 -0
  8. data/doc/ERM.graffle +1318 -0
  9. data/doc/MBTA_XML_Feed_Trial_Docs_13Nov09-1.pdf +0 -0
  10. data/lib/attr_with_default.rb +51 -0
  11. data/lib/instantiate_with_attrs.rb +12 -0
  12. data/lib/nextbus.rb +10 -0
  13. data/lib/nextbus/agency.rb +18 -0
  14. data/lib/nextbus/client.rb +70 -0
  15. data/lib/nextbus/direction.rb +18 -0
  16. data/lib/nextbus/mash_extensions.rb +12 -0
  17. data/lib/nextbus/prediction.rb +18 -0
  18. data/lib/nextbus/report.rb +10 -0
  19. data/lib/nextbus/route.rb +18 -0
  20. data/lib/nextbus/stop.rb +18 -0
  21. data/lib/nextbus/string_extensions.rb +16 -0
  22. data/lib/nextbus/vehicle.rb +18 -0
  23. data/nextbus.gemspec +106 -0
  24. data/test/fixtures/agency_list.xml +5 -0
  25. data/test/fixtures/error.xml +6 -0
  26. data/test/fixtures/predictions.xml +12 -0
  27. data/test/fixtures/predictions_for_multi_stops.xml +21 -0
  28. data/test/fixtures/route_config.xml +755 -0
  29. data/test/fixtures/route_list.xml +5 -0
  30. data/test/fixtures/vehicle_locations.xml +11 -0
  31. data/test/helper.rb +78 -0
  32. data/test/unit/test_agency.rb +56 -0
  33. data/test/unit/test_attr_with_default.rb +158 -0
  34. data/test/unit/test_client.rb +107 -0
  35. data/test/unit/test_direction.rb +59 -0
  36. data/test/unit/test_instantiate_with_attrs.rb +31 -0
  37. data/test/unit/test_mash_extensions.rb +45 -0
  38. data/test/unit/test_nextbus.rb +9 -0
  39. data/test/unit/test_prediction.rb +47 -0
  40. data/test/unit/test_report.rb +34 -0
  41. data/test/unit/test_route.rb +51 -0
  42. data/test/unit/test_stop.rb +64 -0
  43. data/test/unit/test_string_extensions.rb +33 -0
  44. data/test/unit/test_vehicle.rb +52 -0
  45. data/vendor/cache/columnize-0.3.1.gem +0 -0
  46. data/vendor/cache/crack-0.1.6.gem +0 -0
  47. data/vendor/cache/hashie-0.1.8.gem +0 -0
  48. data/vendor/cache/httparty-0.5.2.gem +0 -0
  49. data/vendor/cache/linecache-0.43.gem +0 -0
  50. data/vendor/cache/mocha-0.9.8.gem +0 -0
  51. data/vendor/cache/rake-0.8.7.gem +0 -0
  52. data/vendor/cache/ruby-debug-0.10.3.gem +0 -0
  53. data/vendor/cache/ruby-debug-base-0.10.3.gem +0 -0
  54. metadata +121 -0
@@ -0,0 +1,5 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <body copyright="All data copyright MBTA 2010.">
3
+ <route tag="39" title="39"/>
4
+ <route tag="111" title="111"/>
5
+ </body>
@@ -0,0 +1,11 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <body copyright="All data copyright MBTA 2010.">
3
+ <vehicle id="1022" routeTag="39" dirTag="out" lat="42.3476861" lon="-71.0744548" secsSinceReport="7" predictable="true" heading="265"/>
4
+ <vehicle id="1041" routeTag="39" dirTag="out" lat="42.3452617" lon="-71.0821138" secsSinceReport="7" predictable="true" heading="227"/>
5
+ <vehicle id="1039" routeTag="39" dirTag="out" lat="42.3217476" lon="-71.1121441" secsSinceReport="104" predictable="true" heading="175"/>
6
+ <vehicle id="1040" routeTag="39" dirTag="in" lat="42.3332776" lon="-71.1075843" secsSinceReport="39" predictable="true" heading="90"/>
7
+ <vehicle id="1034" routeTag="39" dirTag="in" lat="42.3016762" lon="-71.1142528" secsSinceReport="39" predictable="true" heading="103"/>
8
+ <vehicle id="1033" routeTag="39" dirTag="out" lat="42.3311184" lon="-71.1117508" secsSinceReport="39" predictable="true" heading="161"/>
9
+ <vehicle id="1027" routeTag="39" dirTag="in" lat="42.3387512" lon="-71.0929833" secsSinceReport="7" predictable="true" heading="61"/>
10
+ <lastTime time="1266681129831"/>
11
+ </body>
@@ -0,0 +1,78 @@
1
+ begin
2
+ # Try to require the preresolved locked set of gems.
3
+ require File.expand_path('../.bundle/environment', __FILE__)
4
+ rescue LoadError
5
+ # Fall back on doing an unlocked resolve at runtime.
6
+ require "rubygems"
7
+ require "bundler"
8
+ Bundler.setup
9
+ end
10
+
11
+ require 'test/unit'
12
+ require 'mocha'
13
+ require 'erb'
14
+ require 'ruby-debug'
15
+
16
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
17
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
18
+
19
+ require 'nextbus'
20
+
21
+ class Test::Unit::TestCase
22
+
23
+ def setup
24
+ response = Net::HTTPOK.new("1.1", 200, "Content for you")
25
+ response.stubs(:body).returns("")
26
+ http_request = HTTParty::Request.new(Net::HTTP::Get, 'http://localhost', :format => :xml)
27
+ http_request.stubs(:perform_actual_request).returns(response)
28
+ HTTParty::Request.stubs(:new).returns(http_request)
29
+ end
30
+
31
+ def file_fixture(filename)
32
+ open(File.join(File.dirname(__FILE__), 'fixtures', "#{filename.to_s}")).read
33
+ end
34
+
35
+ def expect_response(filename, path=nil, method=nil)
36
+ format = filename.split('.').last.intern
37
+ data = file_fixture(filename)
38
+
39
+ response = Net::HTTPOK.new("1.1", 200, "Content for you")
40
+ response.stubs(:body).returns(data)
41
+
42
+ http_request = HTTParty::Request.new(Net::HTTP::Get, 'http://localhost', :format => format)
43
+ http_request.stubs(:perform_actual_request).returns(response)
44
+
45
+ HTTParty::Request.expects(:new).with{|*args| (path.nil? || args[1] =~ path) && (method.nil? || args.first == method) }.returns(http_request)
46
+ end
47
+
48
+ def assert_instantiated_with_attrs(klass, attrs={})
49
+ object = klass.new(attrs)
50
+ attrs.each {|name, value| assert_equal value, object.send(name) }
51
+ end
52
+
53
+ def assert_attr_accessor(object, attr_name, expected_value=nil, new_value='some value')
54
+ assert_attr_reader(object, attr_name, expected_value)
55
+ assert_attr_writer(object, attr_name, expected_value)
56
+ setter = setter_from_attr_name(attr_name)
57
+ assert_not_equal new_value, object.send(attr_name), "With #{object}"
58
+ object.send(setter, new_value)
59
+ assert_equal new_value, object.send(attr_name), "With #{object}"
60
+ object.send(setter, expected_value)
61
+ end
62
+
63
+ def assert_attr_reader(object, attr_name, expected_value=nil)
64
+ assert object.respond_to?(attr_name), "Expected #{object} to respond to #{attr_name.to_s}"
65
+ assert_equal expected_value, object.send(attr_name), "With #{object}"
66
+ end
67
+
68
+ def assert_attr_writer(object, attr_name, new_value=nil)
69
+ setter = setter_from_attr_name(attr_name)
70
+ assert object.respond_to?(setter), "Expected #{object} to respond to #{setter.to_s}"
71
+ assert_equal new_value, object.send(setter, new_value), "With #{object}"
72
+ end
73
+
74
+ def setter_from_attr_name(attr_name)
75
+ "#{attr_name.to_s}=".to_sym
76
+ end
77
+
78
+ end
@@ -0,0 +1,56 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'helper')
2
+
3
+ class TestAgency < Test::Unit::TestCase
4
+
5
+ def setup
6
+ super
7
+ @agency1 = Nextbus::Agency.new
8
+ end
9
+
10
+ def test_tag_attr
11
+ assert_attr_accessor @agency1, :tag
12
+ end
13
+ def test_title_attr
14
+ assert_attr_accessor @agency1, :title
15
+ end
16
+ def test_short_title_attr
17
+ assert_attr_accessor @agency1, :short_title
18
+ end
19
+ def test_region_title_attr
20
+ assert_attr_accessor @agency1, :region_title
21
+ end
22
+
23
+ def test_routes_attr
24
+ assert_attr_accessor @agency1, :routes, []
25
+ end
26
+ def test_vehicles_attr
27
+ assert_attr_accessor @agency1, :vehicles, []
28
+ end
29
+
30
+ def test_instantiated_with_attrs
31
+ attrs = {:tag => 'my tag', :title => 'my title', :short_title => 'my short title', :region_title => 'my region title'}
32
+ assert_instantiated_with_attrs Nextbus::Agency, attrs
33
+ end
34
+
35
+ def test_all
36
+ agency_title1 = 'MBTA'
37
+ agency_title2 = 'San Francisco Muni'
38
+ expect_response('agency_list.xml', nil, Net::HTTP::Get)
39
+ all = Nextbus::Agency.all
40
+ assert all.is_a?(Array)
41
+ assert_equal 2, all.length
42
+ assert all[0].is_a?(Nextbus::Agency)
43
+ assert_equal agency_title1, all[0].title
44
+ assert_equal agency_title2, all[1].title
45
+ end
46
+
47
+ def test_find
48
+ agency_title = 'MBTA'
49
+ expect_response('agency_list.xml', nil, Net::HTTP::Get)
50
+ agency_id = 'mbta'
51
+ agency = Nextbus::Agency.find(agency_id)
52
+ assert agency.is_a?(Nextbus::Agency)
53
+ assert_equal agency_title, agency.title
54
+ end
55
+
56
+ end
@@ -0,0 +1,158 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'helper')
2
+
3
+ class TwoStandardAttrReaders < Test::Unit::TestCase
4
+
5
+ include AttrWithDefault
6
+
7
+ attr_reader :foo, :baz
8
+
9
+ def test_should_not_define_first_instance_variable
10
+ assert_equal false, instance_variable_defined?(:@foo)
11
+ end
12
+
13
+ def test_should_return_first_instance_variable_when_sent_first_attr_reader
14
+ @foo = 'bar'
15
+ assert_equal 'bar', foo
16
+ end
17
+
18
+ def test_should_not_define_second_instance_variable
19
+ assert_equal false, instance_variable_defined?(:@baz)
20
+ end
21
+
22
+ def test_should_return_second_instance_variable_when_sent_second_attr_reader
23
+ @baz = 'bat'
24
+ assert_equal 'bat', baz
25
+ end
26
+
27
+ end
28
+
29
+ class TwoStandardAttrAccessors < Test::Unit::TestCase
30
+
31
+ include AttrWithDefault
32
+
33
+ attr_accessor :foo, :baz
34
+
35
+ def test_should_not_define_first_instance_variable
36
+ assert_equal false, instance_variable_defined?(:@foo)
37
+ end
38
+
39
+ def test_should_return_first_instance_variable_when_sent_first_attr_reader
40
+ @foo = 'bar'
41
+ assert_equal 'bar', foo
42
+ end
43
+
44
+ def test_should_set_first_instance_variable_when_sent_first_attr_writer
45
+ self.foo = 'bar'
46
+ assert_equal 'bar', @foo
47
+ end
48
+
49
+ def test_should_not_define_second_instance_variable
50
+ assert_equal false, instance_variable_defined?(:@baz)
51
+ end
52
+
53
+ def test_should_return_second_instance_variable_when_sent_second_attr_reader
54
+ @baz = 'bat'
55
+ assert_equal 'bat', baz
56
+ end
57
+
58
+ def test_should_set_second_instance_variable_when_sent_second_attr_writer
59
+ self.baz = 'bat'
60
+ assert_equal 'bat', @baz
61
+ end
62
+
63
+ end
64
+
65
+ class TwoStandardAttrReadersAndADefault < Test::Unit::TestCase
66
+
67
+ include AttrWithDefault
68
+
69
+ attr_reader :foo, :baz, :blit => 'blat'
70
+
71
+ def test_should_not_define_first_instance_variable
72
+ assert_equal false, instance_variable_defined?(:@foo)
73
+ end
74
+
75
+ def test_should_return_first_instance_variable_when_sent_first_attr_reader
76
+ @foo = 'bar'
77
+ assert_equal 'bar', foo
78
+ end
79
+
80
+ def test_should_not_define_second_instance_variable
81
+ assert_equal false, instance_variable_defined?(:@baz)
82
+ end
83
+
84
+ def test_should_return_second_instance_variable_when_sent_second_attr_reader
85
+ @baz = 'bat'
86
+ assert_equal 'bat', baz
87
+ end
88
+
89
+ def test_should_not_define_third_instance_variable
90
+ assert_equal false, instance_variable_defined?(:@blit)
91
+ end
92
+
93
+ def test_should_set_third_instance_variable_when_sent_third_attr_reader
94
+ blit
95
+ assert_equal 'blat', @blit
96
+ end
97
+
98
+ def test_should_not_set_third_instance_variable_if_already_set_when_sent_third_attr_reader
99
+ @blit = 'splat'
100
+ assert_equal 'splat', blit
101
+ end
102
+
103
+ end
104
+
105
+ class TwoStandardAttrAccessorsAndADefault < Test::Unit::TestCase
106
+
107
+ include AttrWithDefault
108
+
109
+ attr_accessor :foo, :baz, :blit => 'blat'
110
+
111
+ def test_should_not_define_first_instance_variable
112
+ assert_equal false, instance_variable_defined?(:@foo)
113
+ end
114
+
115
+ def test_should_return_first_instance_variable_when_sent_first_attr_reader
116
+ @foo = 'bar'
117
+ assert_equal 'bar', foo
118
+ end
119
+
120
+ def test_should_set_first_instance_variable_when_sent_first_attr_writer
121
+ self.foo = 'bar'
122
+ assert_equal 'bar', @foo
123
+ end
124
+
125
+ def test_should_not_define_second_instance_variable
126
+ assert_equal false, instance_variable_defined?(:@baz)
127
+ end
128
+
129
+ def test_should_return_second_instance_variable_when_sent_second_attr_reader
130
+ @baz = 'bat'
131
+ assert_equal 'bat', baz
132
+ end
133
+
134
+ def test_should_set_second_instance_variable_when_sent_second_attr_writer
135
+ self.baz = 'bat'
136
+ assert_equal 'bat', @baz
137
+ end
138
+
139
+ def test_should_not_define_third_instance_variable
140
+ assert_equal false, instance_variable_defined?(:@blit)
141
+ end
142
+
143
+ def test_should_set_third_instance_variable_when_sent_third_attr_reader
144
+ blit
145
+ assert_equal 'blat', @blit
146
+ end
147
+
148
+ def test_should_not_set_third_instance_variable_if_already_set_before_sent_third_attr_reader
149
+ @blit = 'splat'
150
+ assert_equal 'splat', blit
151
+ end
152
+
153
+ def test_should_set_third_instance_variable_when_sent_third_attr_writer
154
+ self.blit = 'splat'
155
+ assert_equal 'splat', @blit
156
+ end
157
+
158
+ end
@@ -0,0 +1,107 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'helper')
2
+
3
+ class TestClient < Test::Unit::TestCase
4
+
5
+ def setup
6
+ super
7
+ @client = Nextbus::Client.instance
8
+ end
9
+
10
+ def test_client
11
+ assert_equal @client, Nextbus.client
12
+ end
13
+
14
+ def test_agencies
15
+ agency_title1 = 'MBTA'
16
+ agency_title2 = 'San Francisco Muni'
17
+ expect_response('agency_list.xml', nil, Net::HTTP::Get)
18
+ agencies = @client.agencies
19
+ assert agencies.is_a?(Array)
20
+ assert agencies.detect{|agency| agency.title == agency_title1 }
21
+ assert agencies.detect{|agency| agency.title == agency_title2 }
22
+ assert_equal 2, agencies.length
23
+ end
24
+
25
+ def test_routes
26
+ route_title1 = '39'
27
+ route_title2 = '111'
28
+ agency_id = 'abc'
29
+ expect_response('route_list.xml', /#{agency_id}/, Net::HTTP::Get)
30
+ routes = @client.routes(agency_id)
31
+ assert routes.is_a?(Array)
32
+ assert routes.detect{|route| route.title == route_title1 }
33
+ assert routes.detect{|route| route.title == route_title2 }
34
+ assert_equal 2, routes.length
35
+ end
36
+
37
+ def test_route
38
+ route_title = '39'
39
+ stop_tag1 = '8750'
40
+ stop_tag2 = '1935'
41
+ agency_id = 'abc'
42
+ route_id = '123'
43
+ expect_response('route_config.xml', /#{agency_id}.+#{route_id}/, Net::HTTP::Get)
44
+ route = @client.route(agency_id, route_id)
45
+ assert route.is_a?(Hashie::Mash)
46
+ assert_equal route_title, route.title
47
+ assert_equal stop_tag1, route.stop[0].tag
48
+ assert_equal stop_tag2, route.stop[1].tag
49
+ end
50
+
51
+ def test_predictions
52
+ vehicle_id1 = '1041'
53
+ vehicle_id2 = '1022'
54
+ agency_id = 'abc'
55
+ route_id = '123'
56
+ stop_id = '456'
57
+ expect_response('predictions.xml', /#{agency_id}.+#{route_id}.+#{stop_id}/, Net::HTTP::Get)
58
+ predictions = @client.predictions(agency_id, route_id, stop_id)
59
+ assert predictions.is_a?(Array)
60
+ assert_equal vehicle_id1, predictions[0].vehicle
61
+ assert_equal vehicle_id2, predictions[1].vehicle
62
+ assert_equal 5, predictions.length
63
+ end
64
+
65
+ def test_vehicles
66
+ vehicle_heading1 = '265'
67
+ vehicle_heading2 = '227'
68
+ agency_id = 'abc'
69
+ route_id = '123'
70
+ time = Time.parse('2010/01/31 12:30:00EDT')
71
+ epoch_time = time.to_i.to_s
72
+ expect_response('vehicle_locations.xml', /#{agency_id}.+#{route_id}.+#{epoch_time}/, Net::HTTP::Get)
73
+ vehicles = @client.vehicles(agency_id, route_id, time)
74
+ assert vehicles.is_a?(Array)
75
+ assert_equal vehicle_heading1, vehicles[0].heading
76
+ assert_equal vehicle_heading2, vehicles[1].heading
77
+ assert_equal 7, vehicles.length
78
+ end
79
+
80
+ def test_directions
81
+ direction_title1 = 'Outbound'
82
+ direction_title2 = 'Inbound'
83
+ agency_id = 'abc'
84
+ route_id = '123'
85
+ expect_response('route_config.xml', /#{agency_id}.+#{route_id}/, Net::HTTP::Get)
86
+ directions = @client.directions(agency_id, route_id)
87
+ assert directions.is_a?(Array)
88
+ assert_equal direction_title1, directions[0].title
89
+ assert_equal direction_title2, directions[1].title
90
+ assert_equal 2, directions.length
91
+ end
92
+
93
+ def test_stops
94
+ stop_tag1 = '23391'
95
+ stop_tag2 = '173'
96
+ agency_id = 'abc'
97
+ route_id = '123'
98
+ direction_id = 'out'
99
+ expect_response('route_config.xml', /#{agency_id}.+#{route_id}/, Net::HTTP::Get)
100
+ stops = @client.stops(agency_id, route_id, direction_id)
101
+ assert stops.is_a?(Array)
102
+ assert_equal stop_tag1, stops[0].tag
103
+ assert_equal stop_tag2, stops[1].tag
104
+ assert_equal 32, stops.length
105
+ end
106
+
107
+ end
@@ -0,0 +1,59 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'helper')
2
+
3
+ class TestDirection < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @direction1 = Nextbus::Direction.new
7
+ end
8
+
9
+ def test_tag_attr
10
+ assert_attr_accessor @direction1, :tag
11
+ end
12
+ def test_title_attr
13
+ assert_attr_accessor @direction1, :title
14
+ end
15
+ def test_name_attr
16
+ assert_attr_accessor @direction1, :name
17
+ end
18
+
19
+ def test_reports_attr
20
+ assert_attr_accessor @direction1, :reports, []
21
+ end
22
+ def test_stops_attr
23
+ assert_attr_accessor @direction1, :stops, []
24
+ end
25
+ def test_route_attr
26
+ assert_attr_accessor @direction1, :route
27
+ end
28
+
29
+ def test_instantiated_with_attrs
30
+ attrs = {:tag => 'my tag', :title => 'my title', :name => 'my name'}
31
+ assert_instantiated_with_attrs Nextbus::Direction, attrs
32
+ end
33
+
34
+ def test_all
35
+ direction_title1 = 'Outbound'
36
+ direction_title2 = 'Inbound'
37
+ agency_id = 'abc'
38
+ route_id = '321'
39
+ expect_response('route_config.xml', /#{agency_id}.+#{route_id}/, Net::HTTP::Get)
40
+ all = Nextbus::Direction.all(agency_id, route_id)
41
+ assert all.is_a?(Array)
42
+ assert_equal 2, all.length
43
+ assert all[0].is_a?(Nextbus::Direction)
44
+ assert_equal direction_title1, all[0].title
45
+ assert_equal direction_title2, all[1].title
46
+ end
47
+
48
+ def test_find
49
+ direction_title = 'Outbound'
50
+ agency_id = 'abc'
51
+ route_id = '321'
52
+ direction_id = 'out'
53
+ expect_response('route_config.xml', /#{agency_id}.+#{route_id}/, Net::HTTP::Get)
54
+ direction = Nextbus::Direction.find(agency_id, route_id, direction_id)
55
+ assert direction.is_a?(Nextbus::Direction)
56
+ assert_equal direction_title, direction.title
57
+ end
58
+
59
+ end