Class: Translink::Page::Route

Inherits:
Translink::Page show all
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

USER_AGENT

Instance Attribute Summary collapse

Attributes inherited from Translink::Page

#agent, #page, #url

Instance Method Summary collapse

Constructor Details

#initialize(url, long_name) ⇒ Route

Creates a new route.

Parameters:

  • url (String)

    URL to fetch the page from.

  • name (String)

    Route’s long name.



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_nameObject (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

#dateDateTime

Get the date this route is running. Trip pages are bound by this date.

Returns:

  • (DateTime)


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')

Returns:

  • (DateTime)


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.

Parameters:

  • anchor (Nokogiri::XML::Node)

    Anchor to ascend from.

Returns:

  • (Integer)

    0 for one (Regular) direction of travel, 1 for opposite (Goofy) direction of travel.



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

#headsignsArray<String>

Get headsigns of directions travelled by this route.

Returns:

  • (Array<String>)


54
55
56
# File 'lib/translink/page/route.rb', line 54

def headsigns
  page.search('h3').map { |node| node.text.downcase }.uniq
end

#route_idString

Get the route’s unique ID assigned by Translink. This is the same as the short_name.

Returns:

  • (String)


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_typeInteger

Get the type of transportation used on the route.

Returns:

  • (Integer)

Raises:



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_nameString

Gets the route’s code.

Returns:

  • (String)


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_pagesArray<Page::Trip>

Builds an array of trip pages.

Returns:



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