Class: Translink::Page::Trip

Inherits:
Translink::Page show all
Defined in:
lib/translink/page/trip.rb

Defined Under Namespace

Classes: Stop, StopTime

Constant Summary

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, date, direction) ⇒ Trip

Creates a new trip.

Parameters:

  • url (String)

    URL to fetch the page from.

  • date (Date)

    Date the trip runs on.



65
66
67
68
69
# File 'lib/translink/page/trip.rb', line 65

def initialize url, date, direction
  super url
  @date = date.to_date
  @direction = direction
end

Instance Attribute Details

#dateObject

Date

Date the trip runs on.



57
58
59
# File 'lib/translink/page/trip.rb', line 57

def date
  @date
end

#directionObject

Returns the value of attribute direction.



58
59
60
# File 'lib/translink/page/trip.rb', line 58

def direction
  @direction
end

Instance Method Details

#headsignString

Get the trip’s headsign.

Returns:

  • (String)

    “inbound” or “outbound”.



74
75
76
77
# File 'lib/translink/page/trip.rb', line 74

def headsign
  url.to_s =~ /information\/([a-z]+)\//
  $1
end

#stop_timesArray<Page::Trip::StopTime>

Builds an unique array of stop times.

Returns:



90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/translink/page/trip.rb', line 90

def stop_times
  page.search('table#trip-details tbody tr').reduce(Array.new) do |stop_times, table_row|
    stop_time = StopTime.new.html! table_row
    duplicate = stop_times.find do |duplicate|
      duplicate.stop_page.stop_id == stop_time.stop_page.stop_id &&
      duplicate.arrival_time == stop_time.arrival_time
    end
    stop_times << stop_time unless duplicate
    stop_times
  end.each_with_index.map do |stop_time, index|
    stop_time.stop_sequence = index
    stop_time
  end
end

#trip_idString

Get the trip’s unique ID.

Returns:

  • (String)


82
83
84
85
# File 'lib/translink/page/trip.rb', line 82

def trip_id
  url.to_s =~ /information\/[a-z]+\/[^\/]+\/([^\/]+)/
  $1
end