translink 0.0.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. data/CHANGELOG.md +12 -0
  2. data/README.md +44 -8
  3. data/doc/schema.graffle +396 -82
  4. data/doc/schema.png +0 -0
  5. data/lib/translink/cli.rb +10 -16
  6. data/lib/translink/crawler.rb +5 -6
  7. data/lib/translink/db.rb +6 -14
  8. data/lib/translink/model/route.rb +29 -18
  9. data/lib/translink/model/stop.rb +17 -19
  10. data/lib/translink/model/stop_time.rb +26 -0
  11. data/lib/translink/model/trip.rb +48 -0
  12. data/lib/translink/page/route.rb +42 -18
  13. data/lib/translink/page/timetable.rb +15 -18
  14. data/lib/translink/page/trip.rb +90 -17
  15. data/lib/translink/page.rb +1 -1
  16. data/lib/translink/version.rb +1 -1
  17. data/lib/translink.rb +2 -3
  18. data/test/fixtures/sample/route.html +401 -1049
  19. data/test/fixtures/sample/timetable.html +170 -216
  20. data/test/fixtures/verbatim/route.html +1976 -7178
  21. data/test/fixtures/verbatim/timetable.html +1501 -6165
  22. data/test/fixtures/verbatim/trip.html +311 -508
  23. data/test/unit/cli_test.rb +4 -20
  24. data/test/unit/crawler_test.rb +16 -36
  25. data/test/unit/model/route_test.rb +14 -25
  26. data/test/unit/model/stop_test.rb +6 -31
  27. data/test/unit/model/stop_time_test.rb +11 -0
  28. data/test/unit/model/trip_test.rb +28 -0
  29. data/test/unit/page/route_test.rb +38 -28
  30. data/test/unit/page/timetable_test.rb +12 -10
  31. data/test/unit/page/trip_test.rb +38 -22
  32. data/test/unit/page_test.rb +1 -1
  33. data/translink.gemspec +2 -2
  34. metadata +24 -27
  35. data/lib/translink/code.rb +0 -9
  36. data/lib/translink/model/service.rb +0 -20
  37. data/lib/translink/model/stop/extractor.rb +0 -67
  38. data/test/unit/code_test.rb +0 -12
  39. data/test/unit/model/service_test.rb +0 -23
  40. data/test/unit/model/stop/extractor_test.rb +0 -112
@@ -28,33 +28,17 @@ class CLITtest < MiniTest::Unit::TestCase
28
28
 
29
29
  def test_blank_executes_help_command
30
30
  @cli.run ''
31
- assert_match /help/, @out.string
31
+ assert_includes @out.string, 'Usage'
32
32
  end
33
33
 
34
34
  def test_unrecognised_command_executes_help_command
35
35
  @cli.run 'unknown'
36
- assert_match /help/, @out.string
37
- end
38
-
39
- def test_execute_extract_command_with_invalid_uri
40
- @cli.run 'extract invalid'
41
- assert_match /help/, @out.string
42
- end
43
-
44
- def test_execute_extract_command_with_valid_uri
45
- file = File.join TMPDIR, "test_extract_#{Time.now.to_i}.sqlite3"
46
- stop_instance = MiniTest::Mock.new.expect(:extract!, nil).expect(:save!, true)
47
- stop_class = MiniTest::Mock.new.expect :all, [stop_instance]
48
- @cli.__stop__ = stop_class
49
- @cli.run "extract sqlite://#{file}"
50
- assert stop_instance.verify
51
- assert stop_class.verify
52
- assert File.exists?(file), 'Expected file to exist.'
36
+ assert_includes @out.string, 'Usage'
53
37
  end
54
38
 
55
39
  def test_execute_help_command
56
40
  @cli.run 'help'
57
- assert_match /help/, @out.string
41
+ assert_includes @out.string, 'Usage'
58
42
  end
59
43
 
60
44
  def test_execute_scrape_command_with_uri
@@ -73,6 +57,6 @@ class CLITtest < MiniTest::Unit::TestCase
73
57
 
74
58
  def test_execute_scrape_command_without_date_executes_help_command
75
59
  @cli.run 'scrape'
76
- assert_match /help/, @out.string
60
+ assert_includes @out.string, 'Usage'
77
61
  end
78
62
  end
@@ -1,42 +1,22 @@
1
1
  require 'helper'
2
2
 
3
3
  class CrawlerTest < MiniTest::Unit::TestCase
4
- class Model
5
- attr_reader :trip_pages
6
-
7
- def initialize
8
- @trip_pages = []
9
- end
10
-
11
- def add_service_from_trip_page trip_pages
12
- @trip_pages << trip_pages
13
- end
14
-
15
- def self.find_or_add_from_route_page route_page
16
- instance
17
- end
18
-
19
- def self.instance
20
- @instance ||= new
21
- end
22
- end
23
-
24
- def test_initialize
25
- assert_equal 'http://localhost', Crawler.new('http://localhost').url.to_s
26
- end
27
-
28
4
  def test_crawl
29
- stub_request(:get, 'http://jp.translink.com.au/travel-information/services-and-timetables/buses/all-bus-timetables').
30
- to_return(:status => 200, :body => fixture('sample/timetable.html'), :headers => {'Content-Type' => 'text/html'})
31
- stub_request(:get, /http:\/\/jp.translink.com.au\/travel-information\/services-and-timetables\/buses\/view-bus-timetable\/\d+\?direction=(In|Out)bound&routeCode=[0-9A-Z]+&timetableDate=\d{4}-\d{2}-\d{2}/).
32
- to_return(:status => 200, :body => fixture('sample/route.html'), :headers => {'Content-Type' => 'text/html'})
33
- stub_request(:get, /http:\/\/jp.translink.com.au\/travel-information\/services-and-timetables\/trip-details\/\d+\?timetableDate=\d{4}-\d{2}-\d{2}/).
34
- to_return(:status => 200, :body => fixture('verbatim/trip.html'), :headers => {'Content-Type' => 'text/html'})
35
- stub_request(:post, 'http://jp.translink.com.au/travel-information/services-and-timetables/buses/bus-timetables').
36
- to_return(:status => 200, :body => fixture('sample/timetable.html'), :headers => {'Content-Type' => 'text/html'})
37
- crawler = Crawler.new 'http://jp.translink.com.au/travel-information/services-and-timetables/buses/all-bus-timetables'
38
- crawler.__model__ = Model
39
- crawler.crawl Date.today
40
- assert_equal 6, Model.instance.trip_pages.size
5
+ DB.context 'sqlite::memory:', :migrate => true do
6
+ stub_request(:get, 'http://jp.translink.com.au/travel-information/services-and-timetables/buses/all-bus-timetables').
7
+ to_return(:status => 200, :body => fixture('sample/timetable.html'), :headers => {'Content-Type' => 'text/html'})
8
+ stub_request(:post, 'http://jp.translink.com.au/travel-information/network-information/timetables').
9
+ to_return(:status => 200, :body => fixture('sample/timetable.html'), :headers => {'Content-Type' => 'text/html'})
10
+ stub_request(:get, %r{http://jp.translink.com.au/travel-information/network-information/buses/\w+}).
11
+ to_return(:status => 200, :body => fixture('sample/route.html'), :headers => {'Content-Type' => 'text/html'})
12
+ stub_request(:get, %r{http://jp.translink.com.au/travel-information/network-information/service-information/\w+/\d+/\d+/\d+-\d+-\d+}).
13
+ to_return(:status => 200, :body => fixture('verbatim/trip.html'), :headers => {'Content-Type' => 'text/html'})
14
+ crawler = Crawler.new 'http://jp.translink.com.au/travel-information/services-and-timetables/buses/all-bus-timetables'
15
+ crawler.crawl Date.today
16
+ assert_equal 1, Model::Route.count
17
+ assert_equal 1, Model::Trip.count
18
+ assert_equal 27, Model::Stop.count
19
+ assert_equal 27, Model::StopTime.count
20
+ end
41
21
  end
42
22
  end
@@ -1,35 +1,24 @@
1
1
  require 'helper'
2
2
 
3
3
  class Model::RouteTest < MiniTest::Unit::TestCase
4
- def test_add_services_from_trip_pages
5
- DB.new 'sqlite::memory:' do
6
- trip = Page::Trip::Trip.new.tap do |t|
7
- t.stop = Page::Trip::Stop.new 'Illaweena St (Waterstone)', 'Waterstone, Illaweena St far side of Waterbrooke Crt'
8
- t.time = DateTime.parse '2011-11-24 23:17:00'
9
- end
4
+ def test_find_or_add_route_from_route_page
5
+ DB.context 'sqlite::memory:', :migrate => true do
10
6
  route_page = MiniTest::Mock.new
11
- route_page.expect :code, '130'
12
- route_page.expect :name, 'City Buz 130 Via Sunnybank'
13
- route_page.expect :translink_id, 1
14
- trip_page = MiniTest::Mock.new
15
- trip_page.expect :trips, [trip]
16
- route_model = Model::Route.find_or_add_from_route_page route_page
17
- route_model.add_service_from_trip_page trip_page
18
- route_model.add_service_from_trip_page trip_page
19
- route_model.add_service_from_trip_page trip_page
20
- assert_equal 3, Model::Service.count
21
- assert_equal 1, Model::Stop.count
7
+ route_page.expect :short_name, '130'
8
+ route_page.expect :long_name, 'City, Griffith Uni, Sunnybank Hills, Algester, Parkinson'
9
+ route_page.expect :route_type, 0
10
+ assert_equal 0, Model::Route.count
11
+ assert_equal Model::Route.find_or_add_route_from_route_page(route_page), Model::Route.find_or_add_route_from_route_page(route_page)
12
+ assert_equal 1, Model::Route.count
22
13
  end
23
14
  end
24
15
 
25
- def test_find_or_add_from_route_page
26
- DB.new 'sqlite::memory:' do
27
- route_page = MiniTest::Mock.new
28
- route_page.expect :code, '130'
29
- route_page.expect :name, 'City Buz 130 Via Sunnybank'
30
- route_page.expect :translink_id, 1
31
- assert_equal Model::Route.find_or_add_from_route_page(route_page), Model::Route.find_or_add_from_route_page(route_page)
32
- assert_equal 1, Model::Route.count
16
+ def test_add_trip_from_trip_page
17
+ DB.context 'sqlite::memory:', :migrate => true do
18
+ trip_page = OpenStruct.new :direction => 'outbound', :service_id => 8550, :trip_id => 1712196
19
+ route_model = Model::Route.new
20
+ trip_model = route_model.add_trip_from_trip_page trip_page
21
+ assert trip_model.saved?
33
22
  end
34
23
  end
35
24
  end
@@ -1,37 +1,12 @@
1
1
  require 'helper'
2
2
 
3
3
  class Model::StopTest < MiniTest::Unit::TestCase
4
- def test_find_or_add_from_stop
5
- DB.new 'sqlite::memory:' do
6
- stop = MiniTest::Mock.new
7
- stop.expect :name, 'Illaweena St (Waterstone)'
8
- stop.expect :summary, 'Waterstone, Illaweena St far side of Waterbrooke Crt'
9
- assert_equal Model::Stop.find_or_add_from_stop(stop), Model::Stop.find_or_add_from_stop(stop)
10
- assert_equal 1, Model::Stop.count
4
+ def test_find_or_add_from_stop_page
5
+ DB.context 'sqlite::memory:', :migrate => true do
6
+ stop_page = OpenStruct.new :stop_id => 001002, :stop_name => 'Queen Street station, platform A6', :stop_lat => '-27.473648', :stop_lon => '153.018908'
7
+ assert_equal 0, Model::Stop.count
8
+ assert_equal Model::Stop.find_or_add_from_stop_page(stop_page), Model::Stop.find_or_add_from_stop_page(stop_page)
9
+ assert_equal 1, Model::Stop.count
11
10
  end
12
11
  end
13
-
14
- def test_all
15
- DB.new 'sqlite::memory:' do
16
- Model::Stop.create :name => 'Adelaide St'
17
- Model::Stop.create :name => 'Queen St'
18
- Model::Stop.create :name => 'Elizabeth St'
19
- stops = Model::Stop.all
20
- assert_equal ['Adelaide St', 'Queen St', 'Elizabeth St'], stops.map(&:name)
21
- end
22
- end
23
-
24
- def test_extract!
25
- stop = Model::Stop.new
26
- extractor_instance = MiniTest::Mock.new.expect :extract!, stop
27
- extractor_class = MiniTest::Mock.new.expect :new, extractor_instance, [stop]
28
- stop.__extractor__ = extractor_class
29
- stop.extract!
30
- assert extractor_instance.verify
31
- assert extractor_class.verify
32
- end
33
-
34
- def test__extractor__
35
- assert_equal Model::Stop::Extractor, Model::Stop.new.__extractor__
36
- end
37
12
  end
@@ -0,0 +1,11 @@
1
+ require 'helper'
2
+
3
+ class Model::StopTimeTest < MiniTest::Unit::TestCase
4
+ def test_stop_time_page_with_bang
5
+ stop_time_page = OpenStruct.new :arrival_time => '10:00 A.M.', :stop_page => OpenStruct.new, :stop_sequence => 7
6
+ stop_time_model = Model::StopTime.new
7
+ stop_time_model.stop_time_page! stop_time_page
8
+ assert_equal stop_time_page.arrival_time, stop_time_model.arrival_time
9
+ assert_equal stop_time_page.stop_sequence, stop_time_model.stop_sequence
10
+ end
11
+ end
@@ -0,0 +1,28 @@
1
+ require 'helper'
2
+
3
+ class Model::TripTest < MiniTest::Unit::TestCase
4
+ def test_trip_page_with_bang
5
+ trip_page = OpenStruct.new :direction => 'outbound', :service_id => 8550, :trip_id => 1712196
6
+ trip_model = Model::Trip.new
7
+ trip_model.trip_page! trip_page
8
+ assert_equal trip_model.direction, trip_page.direction
9
+ assert_equal trip_model.service_id, trip_page.service_id
10
+ assert_equal trip_model.trip_id, trip_page.trip_id
11
+ end
12
+
13
+ def test_add_stop_time_from_stop_time_page
14
+ stop_time_page = OpenStruct.new :arrival_time => '10:00 A.M.', :stop_page => OpenStruct.new, :stop_sequence => 7
15
+ trip_model = Model::Trip.new
16
+ stop_time_model = trip_model.add_stop_time_from_stop_time_page stop_time_page
17
+ assert_equal stop_time_model.arrival_time, stop_time_page.arrival_time
18
+ assert_equal stop_time_model.stop_sequence, stop_time_page.stop_sequence
19
+ end
20
+
21
+ def test_add_stop_times_from_stop_time_pages
22
+ stop_time_page1 = OpenStruct.new :arrival_time => '10:00 A.M.', :stop_page => OpenStruct.new, :stop_sequence => 7
23
+ stop_time_page2 = OpenStruct.new :arrival_time => '11:00 A.M.', :stop_page => OpenStruct.new, :stop_sequence => 8
24
+ trip_model = Model::Trip.new
25
+ stop_time_models = trip_model.add_stop_times_from_stop_time_pages [stop_time_page1, stop_time_page2]
26
+ assert_equal 2, stop_time_models.size
27
+ end
28
+ end
@@ -1,41 +1,51 @@
1
1
  require 'helper'
2
2
 
3
3
  class Page::RouteTest < MiniTest::Unit::TestCase
4
- def test_code
5
- stub_request(:get, 'http://jp.translink.com.au/travel-information/services-and-timetables/buses/view-bus-timetable/1925?timetableDate=2011-11-14&direction=Inbound&routeCode=130').
6
- to_return(:status => 200, :body => fixture('verbatim/route.html'), :headers => {'Content-Type' => 'text/html'})
7
- assert_equal '130', Page::Route.new('http://jp.translink.com.au/travel-information/services-and-timetables/buses/view-bus-timetable/1925?timetableDate=2011-11-14&direction=Inbound&routeCode=130').code
8
- end
9
-
10
4
  def test_date
11
- stub_request(:get, 'http://jp.translink.com.au/travel-information/services-and-timetables/buses/view-bus-timetable/1925?timetableDate=2011-11-14&direction=Inbound&routeCode=130').
5
+ stub_request(:get, 'http://jp.translink.com.au/travel-information/network-information/buses/130/2012-06-21').
12
6
  to_return(:status => 200, :body => fixture('verbatim/route.html'), :headers => {'Content-Type' => 'text/html'})
13
- assert_equal Date.parse('Monday 14 November 2011'), Page::Route.new('http://jp.translink.com.au/travel-information/services-and-timetables/buses/view-bus-timetable/1925?timetableDate=2011-11-14&direction=Inbound&routeCode=130').date
7
+ assert_equal DateTime.parse('21/06/2012 12:00:00 AM'), Page::Route.new('http://jp.translink.com.au/travel-information/network-information/buses/130/2012-06-21', 'City, Griffith Uni, Sunnybank Hills, Algester, Parkinson').date
14
8
  end
15
-
16
- def test_direction
17
- stub_request(:get, 'http://jp.translink.com.au/travel-information/services-and-timetables/buses/view-bus-timetable/1925?timetableDate=2011-11-14&direction=Inbound&routeCode=130').
18
- to_return(:status => 200, :body => fixture('verbatim/route.html'), :headers => {'Content-Type' => 'text/html'})
19
- assert_equal 'Inbound', Page::Route.new('http://jp.translink.com.au/travel-information/services-and-timetables/buses/view-bus-timetable/1925?timetableDate=2011-11-14&direction=Inbound&routeCode=130').direction
20
- end
21
-
22
- def test_name
23
- stub_request(:get, 'http://jp.translink.com.au/travel-information/services-and-timetables/buses/view-bus-timetable/1925?timetableDate=2011-11-14&direction=Inbound&routeCode=130').
24
- to_return(:status => 200, :body => fixture('verbatim/route.html'), :headers => {'Content-Type' => 'text/html'})
25
- assert_equal 'City Buz 130 Via Sunnybank', Page::Route.new('http://jp.translink.com.au/travel-information/services-and-timetables/buses/view-bus-timetable/1925?timetableDate=2011-11-14&direction=Inbound&routeCode=130').name
9
+
10
+ def test_long_name
11
+ route = Page::Route.new('http://local', 'City, Griffith Uni, Sunnybank Hills, Algester, Parkinson')
12
+ assert_equal 'City, Griffith Uni, Sunnybank Hills, Algester, Parkinson', route.long_name
26
13
  end
27
-
28
- def test_translink_id
29
- stub_request(:get, 'http://jp.translink.com.au/travel-information/services-and-timetables/buses/view-bus-timetable/1925?timetableDate=2011-11-14&direction=Inbound&routeCode=130').
14
+
15
+ def test_short_name
16
+ stub_request(:get, 'http://jp.translink.com.au/travel-information/network-information/buses/130/2012-06-21').
30
17
  to_return(:status => 200, :body => fixture('verbatim/route.html'), :headers => {'Content-Type' => 'text/html'})
31
- assert_equal '1925', Page::Route.new('http://jp.translink.com.au/travel-information/services-and-timetables/buses/view-bus-timetable/1925?timetableDate=2011-11-14&direction=Inbound&routeCode=130').translink_id
18
+ assert_equal '130', Page::Route.new('http://jp.translink.com.au/travel-information/network-information/buses/130/2012-06-21', 'City, Griffith Uni, Sunnybank Hills, Algester, Parkinson').short_name
32
19
  end
33
-
20
+
34
21
  def test_trip_pages
35
- stub_request(:get, 'http://jp.translink.com.au/travel-information/services-and-timetables/buses/view-bus-timetable/1925?timetableDate=2011-11-14&direction=Inbound&routeCode=130').
22
+ stub_request(:get, 'http://jp.translink.com.au/travel-information/network-information/buses/130/2012-06-21').
36
23
  to_return(:status => 200, :body => fixture('verbatim/route.html'), :headers => {'Content-Type' => 'text/html'})
37
- trip_pages = Page::Route.new('http://jp.translink.com.au/travel-information/services-and-timetables/buses/view-bus-timetable/1925?timetableDate=2011-11-14&direction=Inbound&routeCode=130').trip_pages
38
- assert_equal 'http://jp.translink.com.au/travel-information/services-and-timetables/trip-details/281889?timetableDate=2011-11-14', trip_pages.first.url.to_s
39
- assert_equal 'http://jp.translink.com.au/travel-information/services-and-timetables/trip-details/252074?timetableDate=2011-11-14', trip_pages.last.url.to_s
24
+ route = Page::Route.new('http://jp.translink.com.au/travel-information/network-information/buses/130/2012-06-21', 'City, Griffith Uni, Sunnybank Hills, Algester, Parkinson')
25
+ trip_pages = route.trip_pages
26
+ assert_equal 'http://jp.translink.com.au/travel-information/network-information/service-information/outbound/8904/1891449/2012-06-20', trip_pages.first.url.to_s
27
+ assert_equal route.date.to_date, trip_pages.first.date
28
+ assert_equal 'http://jp.translink.com.au/travel-information/network-information/service-information/inbound/8904/1881543/2012-06-22', trip_pages.last.url.to_s
29
+ assert_equal route.date.to_date, trip_pages.last.date
30
+ end
31
+ end
32
+
33
+ class Page::RouteTypeTest < MiniTest::Unit::TestCase
34
+ def test_bus
35
+ assert_equal 3, Page::Route.new('http://jp.translink.com.au/travel-information/network-information/buses/all-timetables', 'City').route_type
36
+ end
37
+
38
+ def test_ferry
39
+ assert_equal 4, Page::Route.new('http://jp.translink.com.au/travel-information/network-information/ferries/all-timetables', 'City').route_type
40
+ end
41
+
42
+ def test_train
43
+ assert_equal 0, Page::Route.new('http://jp.translink.com.au/travel-information/network-information/trains/richlands', 'City').route_type
44
+ end
45
+
46
+ def test_exception
47
+ assert_raises Page::Route::UnknownRouteTypeError do
48
+ Page::Route.new('http://foo', 'City').route_type
49
+ end
40
50
  end
41
51
  end
@@ -2,25 +2,27 @@ require 'helper'
2
2
 
3
3
  class Page::TimetableTest < MiniTest::Unit::TestCase
4
4
  def test_page
5
- stub_request(:get, 'http://jp.translink.com.au/travel-information/services-and-timetables/buses/bus-timetables').
5
+ stub_request(:get, 'http://jp.translink.com.au/travel-information/network-information/buses/all-timetables').
6
6
  to_return(:status => 200, :body => fixture('verbatim/timetable.html'), :headers => {'Content-Type' => 'text/html'})
7
- assert_instance_of Mechanize::Page, Page::Timetable.new('http://jp.translink.com.au/travel-information/services-and-timetables/buses/bus-timetables').page
7
+ assert_instance_of Mechanize::Page, Page::Timetable.new('http://jp.translink.com.au/travel-information/network-information/buses/all-timetables').page
8
8
  end
9
9
 
10
10
  def test_route_pages
11
- stub_request(:get, 'http://jp.translink.com.au/travel-information/services-and-timetables/buses/bus-timetables').
11
+ stub_request(:get, 'http://jp.translink.com.au/travel-information/network-information/buses/all-timetables').
12
12
  to_return(:status => 200, :body => fixture('verbatim/timetable.html'), :headers => {'Content-Type' => 'text/html'})
13
- route_pages = Page::Timetable.new('http://jp.translink.com.au/travel-information/services-and-timetables/buses/bus-timetables').route_pages
14
- assert_equal 'http://jp.translink.com.au/travel-information/services-and-timetables/buses/view-bus-timetable/2245?timetableDate=2011-11-14&direction=Outbound&routeCode=100', route_pages.first.url.to_s
15
- assert_equal 'http://jp.translink.com.au/travel-information/services-and-timetables/buses/view-bus-timetable/2631?timetableDate=2011-11-14&direction=Inbound&routeCode=LOOP', route_pages.last.url.to_s
13
+ route_pages = Page::Timetable.new('http://jp.translink.com.au/travel-information/network-information/buses/all-timetables').route_pages
14
+ assert_equal 'http://jp.translink.com.au/travel-information/network-information/buses/3', route_pages.first.url.to_s
15
+ assert_equal 'Oxenford, Helensvale, University, Hospital, Southport', route_pages.first.long_name
16
+ assert_equal 'http://jp.translink.com.au/travel-information/network-information/buses/TX5', route_pages.last.url.to_s
17
+ assert_equal 'Coomera stn, Dreamworld, Movieworld, Wet\'N Wild, H\'vale stn', route_pages.last.long_name
16
18
  end
17
19
 
18
20
  def test_timetable_page
19
- stub_request(:get, 'http://jp.translink.com.au/travel-information/services-and-timetables/buses/all-bus-timetables').
21
+ stub_request(:get, 'http://jp.translink.com.au/travel-information/network-information/buses/all-timetables').
20
22
  to_return(:status => 200, :body => fixture('verbatim/timetable.html'), :headers => {'Content-Type' => 'text/html'})
21
- stub_request(:post, 'http://jp.translink.com.au/travel-information/services-and-timetables/buses/bus-timetables').
23
+ stub_request(:post, 'http://jp.translink.com.au/travel-information/network-information/timetables').
22
24
  to_return(:status => 200, :body => fixture('verbatim/timetable.html'), :headers => {'Content-Type' => 'text/html'})
23
- timetable_page = Page::Timetable.new('http://jp.translink.com.au/travel-information/services-and-timetables/buses/all-bus-timetables').timetable_page Date.parse('2011-11-27')
24
- assert_equal 'http://jp.translink.com.au/travel-information/services-and-timetables/buses/bus-timetables', timetable_page.url.to_s
25
+ timetable_page = Page::Timetable.new('http://jp.translink.com.au/travel-information/network-information/buses/all-timetables').timetable_page DateTime.parse('2012-06-04')
26
+ assert_equal 'http://jp.translink.com.au/travel-information/network-information/timetables', timetable_page.url.to_s
25
27
  end
26
28
  end
@@ -2,34 +2,50 @@ require 'helper'
2
2
 
3
3
  class Page::TripTest < MiniTest::Unit::TestCase
4
4
  def test_date
5
- stub_request(:get, 'http://jp.translink.com.au/travel-information/services-and-timetables/trip-details/281889?timetableDate=2011-11-14').
6
- to_return(:status => 200, :body => fixture('verbatim/trip.html'), :headers => {'Content-Type' => 'text/html'})
7
- assert_equal Date.parse('Monday 14 November 2011'), Page::Trip.new('http://jp.translink.com.au/travel-information/services-and-timetables/trip-details/281889?timetableDate=2011-11-14').date
5
+ timestamp = DateTime.now
6
+ assert_equal timestamp.to_date, Page::Trip.new('http://localhost', timestamp).date
7
+ date = Date.today
8
+ assert_equal date, Page::Trip.new('http://localhost', date).date
8
9
  end
9
10
 
10
- def test_stops
11
- stub_request(:get, 'http://jp.translink.com.au/travel-information/services-and-timetables/trip-details/281889?timetableDate=2011-11-14').
12
- to_return(:status => 200, :body => fixture('verbatim/trip.html'), :headers => {'Content-Type' => 'text/html'})
13
- stops = Page::Trip.new('http://jp.translink.com.au/travel-information/services-and-timetables/trip-details/281889?timetableDate=2011-11-14').stops
14
- assert_equal Page::Trip::Stop.new('Illaweena St (Waterstone)', 'Waterstone, Illaweena St far side of Waterbrooke Crt'), stops.first
15
- assert_equal Page::Trip::Stop.new('Queen St Bus Station A6', 'Qsbs Stop A6'), stops.last
11
+ def test_direction
12
+ assert_equal 'inbound', Page::Trip.new('http://jp.translink.com.au/travel-information/network-information/service-information/inbound/8550/1704728', Date.today).direction
13
+ assert_equal 'outbound', Page::Trip.new('http://jp.translink.com.au/travel-information/network-information/service-information/outbound/8550/1712196/2012-05-31', Date.today).direction
14
+ assert_equal 'counterclockwise', Page::Trip.new('http://jp.translink.com.au/travel-information/network-information/service-information/counterclockwise/8904/1882248/2012-06-25', Date.today).direction
15
+ assert_equal 'clockwise', Page::Trip.new('http://jp.translink.com.au/travel-information/network-information/service-information/clockwise/8904/1882152/2012-06-25', Date.today).direction
16
16
  end
17
+ end
17
18
 
18
- def test_times
19
- stub_request(:get, 'http://jp.translink.com.au/travel-information/services-and-timetables/trip-details/281889?timetableDate=2011-11-14').
19
+ class Page::TripRequestTest < MiniTest::Unit::TestCase
20
+ def setup
21
+ stub_request(:get, 'http://jp.translink.com.au/travel-information/network-information/service-information/outbound/8550/1712196/2012-05-31').
20
22
  to_return(:status => 200, :body => fixture('verbatim/trip.html'), :headers => {'Content-Type' => 'text/html'})
21
- times = Page::Trip.new('http://jp.translink.com.au/travel-information/services-and-timetables/trip-details/281889?timetableDate=2011-11-14').times
22
- assert_equal DateTime.parse('2011-11-14 05:00 +1000'), times.first
23
- assert_equal DateTime.parse('2011-11-14 05:50 +1000'), times.last
24
23
  end
25
24
 
26
- def test_trips
27
- stub_request(:get, 'http://jp.translink.com.au/travel-information/services-and-timetables/trip-details/281889?timetableDate=2011-11-14').
28
- to_return(:status => 200, :body => fixture('verbatim/trip.html'), :headers => {'Content-Type' => 'text/html'})
29
- trips = Page::Trip.new('http://jp.translink.com.au/travel-information/services-and-timetables/trip-details/281889?timetableDate=2011-11-14').trips
30
- assert_equal Page::Trip::Stop.new('Illaweena St (Waterstone)', 'Waterstone, Illaweena St far side of Waterbrooke Crt'), trips.first.stop
31
- assert_equal DateTime.parse('2011-11-14 05:00 +1000'), trips.first.time
32
- assert_equal Page::Trip::Stop.new('Queen St Bus Station A6', 'Qsbs Stop A6'), trips.last.stop
33
- assert_equal DateTime.parse('2011-11-14 05:50 +1000'), trips.last.time
25
+ def stub_trip
26
+ Page::Trip.new('http://jp.translink.com.au/travel-information/network-information/service-information/outbound/8550/1712196/2012-05-31', Date.today)
27
+ end
28
+
29
+ def test_service_id
30
+ assert_equal '1712196', stub_trip.service_id
31
+ end
32
+
33
+ def test_stop_times
34
+ stop_page1 = Page::Trip::Stop.new.tap { |s| s.stop_name = 'Queen Street station, platform A6'; s.stop_id = '001002'; s.stop_lat = '-27.470677'; s.stop_lon = '153.024747' }
35
+ stop_page2 = Page::Trip::Stop.new.tap { |s| s.stop_name = 'Illaweena St at Waterstone'; s.stop_id = '010764'; s.stop_lat = '-27.639463'; s.stop_lon = '153.052551' }
36
+ stop_times = stub_trip.stop_times
37
+ stop_time1 = stop_times.first
38
+ stop_time2 = stop_times.last
39
+ assert_equal 27, stop_times.size
40
+ assert_equal 0, stop_time1.stop_sequence
41
+ assert_equal '11:25 P.M.', stop_time1.arrival_time
42
+ assert_equal stop_page1, stop_time1.stop_page
43
+ assert_equal 26, stop_time2.stop_sequence
44
+ assert_equal '12:14 A.M.', stop_time2.arrival_time
45
+ assert_equal stop_page2, stop_time2.stop_page
46
+ end
47
+
48
+ def test_trip_id
49
+ assert_equal '8550', stub_trip.trip_id
34
50
  end
35
51
  end
@@ -6,6 +6,6 @@ class PageTest < MiniTest::Unit::TestCase
6
6
  end
7
7
 
8
8
  def test_agent
9
- assert_equal "Translink/#{VERSION} Ruby/#{RUBY_VERSION} (https://github.com/tatey/translink)", Page.new('http://localhost').agent.user_agent
9
+ assert_equal "Mozilla/5.0 (Translink/#{VERSION} Ruby/#{RUBY_VERSION} (https://github.com/tatey/translink))", Page.new('http://localhost').agent.user_agent
10
10
  end
11
11
  end
data/translink.gemspec CHANGED
@@ -9,8 +9,8 @@ Gem::Specification.new do |s|
9
9
  s.authors = ['Tate Johnson']
10
10
  s.email = ['tate@tatey.com']
11
11
  s.homepage = 'https://github.com/tatey/translink'
12
- s.summary = %q{Scrapes bus stops, routes and service times into a nicely structured database.}
13
- s.description = %q{Scrapes bus stops, routes and service times into a nicely structured database.}
12
+ s.summary = %q{Scrapes bus routes, trips, stops and times into a relational database.}
13
+ s.description = %q{Scrapes bus routes, trips, stops and times into a relational database.}
14
14
 
15
15
  s.rubyforge_project = 'translink'
16
16
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: translink
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-03-15 00:00:00.000000000 Z
12
+ date: 2012-06-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: dm-core
16
- requirement: &70250850818120 !ruby/object:Gem::Requirement
16
+ requirement: &70170271474960 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 1.2.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70250850818120
24
+ version_requirements: *70170271474960
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: dm-migrations
27
- requirement: &70250850817300 !ruby/object:Gem::Requirement
27
+ requirement: &70170271474500 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 1.2.0
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70250850817300
35
+ version_requirements: *70170271474500
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: dm-sqlite-adapter
38
- requirement: &70250850816380 !ruby/object:Gem::Requirement
38
+ requirement: &70170271474040 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 1.2.0
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *70250850816380
46
+ version_requirements: *70170271474040
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: mechanize
49
- requirement: &70250850815300 !ruby/object:Gem::Requirement
49
+ requirement: &70170271473580 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 2.0.1
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *70250850815300
57
+ version_requirements: *70170271473580
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: rake
60
- requirement: &70250850814000 !ruby/object:Gem::Requirement
60
+ requirement: &70170271753220 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ~>
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: 0.9.2
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70250850814000
68
+ version_requirements: *70170271753220
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: webmock
71
- requirement: &70250850812500 !ruby/object:Gem::Requirement
71
+ requirement: &70170271454480 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ~>
@@ -76,9 +76,8 @@ dependencies:
76
76
  version: 1.7.8
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *70250850812500
80
- description: Scrapes bus stops, routes and service times into a nicely structured
81
- database.
79
+ version_requirements: *70170271454480
80
+ description: Scrapes bus routes, trips, stops and times into a relational database.
82
81
  email:
83
82
  - tate@tatey.com
84
83
  executables:
@@ -88,6 +87,7 @@ extra_rdoc_files: []
88
87
  files:
89
88
  - .gitignore
90
89
  - .travis.yml
90
+ - CHANGELOG.md
91
91
  - Gemfile
92
92
  - LICENSE
93
93
  - README.md
@@ -97,13 +97,12 @@ files:
97
97
  - doc/schema.png
98
98
  - lib/translink.rb
99
99
  - lib/translink/cli.rb
100
- - lib/translink/code.rb
101
100
  - lib/translink/crawler.rb
102
101
  - lib/translink/db.rb
103
102
  - lib/translink/model/route.rb
104
- - lib/translink/model/service.rb
105
103
  - lib/translink/model/stop.rb
106
- - lib/translink/model/stop/extractor.rb
104
+ - lib/translink/model/stop_time.rb
105
+ - lib/translink/model/trip.rb
107
106
  - lib/translink/page.rb
108
107
  - lib/translink/page/route.rb
109
108
  - lib/translink/page/timetable.rb
@@ -116,12 +115,11 @@ files:
116
115
  - test/fixtures/verbatim/trip.html
117
116
  - test/helper.rb
118
117
  - test/unit/cli_test.rb
119
- - test/unit/code_test.rb
120
118
  - test/unit/crawler_test.rb
121
119
  - test/unit/model/route_test.rb
122
- - test/unit/model/service_test.rb
123
- - test/unit/model/stop/extractor_test.rb
124
120
  - test/unit/model/stop_test.rb
121
+ - test/unit/model/stop_time_test.rb
122
+ - test/unit/model/trip_test.rb
125
123
  - test/unit/page/route_test.rb
126
124
  - test/unit/page/timetable_test.rb
127
125
  - test/unit/page/trip_test.rb
@@ -148,13 +146,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
148
146
  version: '0'
149
147
  segments:
150
148
  - 0
151
- hash: 840536220578175397
149
+ hash: 3982084261629869304
152
150
  requirements: []
153
151
  rubyforge_project: translink
154
152
  rubygems_version: 1.8.11
155
153
  signing_key:
156
154
  specification_version: 3
157
- summary: Scrapes bus stops, routes and service times into a nicely structured database.
155
+ summary: Scrapes bus routes, trips, stops and times into a relational database.
158
156
  test_files:
159
157
  - test/fixtures/sample/route.html
160
158
  - test/fixtures/sample/timetable.html
@@ -163,12 +161,11 @@ test_files:
163
161
  - test/fixtures/verbatim/trip.html
164
162
  - test/helper.rb
165
163
  - test/unit/cli_test.rb
166
- - test/unit/code_test.rb
167
164
  - test/unit/crawler_test.rb
168
165
  - test/unit/model/route_test.rb
169
- - test/unit/model/service_test.rb
170
- - test/unit/model/stop/extractor_test.rb
171
166
  - test/unit/model/stop_test.rb
167
+ - test/unit/model/stop_time_test.rb
168
+ - test/unit/model/trip_test.rb
172
169
  - test/unit/page/route_test.rb
173
170
  - test/unit/page/timetable_test.rb
174
171
  - test/unit/page/trip_test.rb