Class: Translink::Page::Route
- Inherits:
-
Translink::Page
- Object
- Translink::Page
- Translink::Page::Route
- Defined in:
- lib/translink/page/route.rb
Defined Under Namespace
Classes: UnknownRouteTypeError
Constant Summary collapse
- ROUTE_TYPES =
Maps to Google Transit route type.
{'buses' => 3, 'ferries' => 4, 'trains' => 0}
Constants inherited from Translink::Page
Instance Attribute Summary collapse
-
#long_name ⇒ Object
readonly
- String
-
Usually a list of suburbs.
Attributes inherited from Translink::Page
Instance Method Summary collapse
-
#date ⇒ DateTime
Get the date this route is running.
-
#date_from_anchor(anchor) ⇒ DateTime
Get the date of the trip.
-
#direction_from_anchor(anchor) ⇒ Integer
Get the direction of travel for the given
anchor
. -
#headsigns ⇒ Array<String>
Get headsigns of directions travelled by this route.
-
#initialize(url, long_name) ⇒ Route
constructor
Creates a new route.
-
#route_id ⇒ String
Get the route’s unique ID assigned by Translink.
-
#route_type ⇒ Integer
Get the type of transportation used on the route.
-
#short_name ⇒ String
Gets the route’s code.
-
#trip_pages ⇒ Array<Page::Trip>
Builds an array of trip pages.
Constructor Details
#initialize(url, long_name) ⇒ Route
Creates a new route.
14 15 16 17 |
# File 'lib/translink/page/route.rb', line 14 def initialize url, long_name super url @long_name = long_name end |
Instance Attribute Details
#long_name ⇒ Object (readonly)
- String
-
Usually a list of suburbs.
8 9 10 |
# File 'lib/translink/page/route.rb', line 8 def long_name @long_name end |
Instance Method Details
#date ⇒ DateTime
Get the date this route is running. Trip pages are bound by this date.
47 48 49 |
# File 'lib/translink/page/route.rb', line 47 def date DateTime.parse page.search('select#TimetableDate option[selected]').first['value'] end |
#date_from_anchor(anchor) ⇒ DateTime
Get the date of the trip. If the trip does not have a date, the UNIX epoc is returned.
Examples:
"/travel-information/network-information/service-information/outbound/9792/2173523/2012-09-24"
... becomes
DateTime.new('2012-09-24')
"/travel-information/network-information/service-information/outbound/9792/2173523"
... becomes
DateTime.new('1970-01-01')
81 82 83 84 85 |
# File 'lib/translink/page/route.rb', line 81 def date_from_anchor anchor match = anchor[:href].match /\d{4}-\d{2}-\d{2}$/ date = match ? match[0] : '1970-01-01' DateTime.parse date end |
#direction_from_anchor(anchor) ⇒ Integer
Get the direction of travel for the given anchor
.
63 64 65 |
# File 'lib/translink/page/route.rb', line 63 def direction_from_anchor anchor headsigns.index anchor.ancestors('div.route-timetable').search('h3').first.text.downcase end |
#headsigns ⇒ Array<String>
Get headsigns of directions travelled by this route.
54 55 56 |
# File 'lib/translink/page/route.rb', line 54 def headsigns page.search('h3').map { |node| node.text.downcase }.uniq end |
#route_id ⇒ String
Get the route’s unique ID assigned by Translink. This is the same as the short_name
.
23 24 25 |
# File 'lib/translink/page/route.rb', line 23 def route_id @route_id ||= page.search('div#headingBar h1').first.text.sub('Route ', '') end |
#route_type ⇒ Integer
Get the type of transportation used on the route.
103 104 105 106 |
# File 'lib/translink/page/route.rb', line 103 def route_type url.to_s =~ /(buses|ferries|trains)/ ROUTE_TYPES.fetch($1) { raise UnknownRouteTypeError } end |
#short_name ⇒ String
Gets the route’s code.
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/translink/page/route.rb', line 30 def short_name case route_id when 'CGLD' 'CityGlider' when 'LOOP' 'City Loop' when 'SHLP' 'Spring Hill City Loop' else route_id end end |
#trip_pages ⇒ Array<Page::Trip>
Builds an array of trip pages.
90 91 92 93 94 95 96 |
# File 'lib/translink/page/route.rb', line 90 def trip_pages page.search('a.map-link-top').select do |anchor| date_from_anchor(anchor) == date end.map do |anchor| Trip.new url_from_href(anchor[:href]), date, direction_from_anchor(anchor) end end |