Class: Trip

Inherits:
Object
  • Object
show all
Defined in:
lib/caltrain/trip.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*data) ⇒ Trip

Returns a new instance of Trip.



49
50
51
52
53
54
55
56
57
58
# File 'lib/caltrain/trip.rb', line 49

def initialize(*data)
  @trip_id = data[0]
  @route_id = data[1]
  @service_id = data[2]
  @headsign = data[3]
  @direction = data[4] == '0' ? :north : :south
  @shape_id = data[5]

  Trip << self
end

Instance Attribute Details

#directionObject (readonly)

Returns the value of attribute direction.



2
3
4
# File 'lib/caltrain/trip.rb', line 2

def direction
  @direction
end

#headsignObject (readonly)

Returns the value of attribute headsign.



2
3
4
# File 'lib/caltrain/trip.rb', line 2

def headsign
  @headsign
end

#route_idObject (readonly)

Returns the value of attribute route_id.



2
3
4
# File 'lib/caltrain/trip.rb', line 2

def route_id
  @route_id
end

#service_idObject (readonly)

Returns the value of attribute service_id.



2
3
4
# File 'lib/caltrain/trip.rb', line 2

def service_id
  @service_id
end

#shape_idObject (readonly)

Returns the value of attribute shape_id.



2
3
4
# File 'lib/caltrain/trip.rb', line 2

def shape_id
  @shape_id
end

#trip_idObject (readonly)

Returns the value of attribute trip_id.



2
3
4
# File 'lib/caltrain/trip.rb', line 2

def trip_id
  @trip_id
end

Class Method Details

.<<(trip) ⇒ Object



44
45
46
# File 'lib/caltrain/trip.rb', line 44

def <<(trip)
  @trips << trip unless trips.map(&:trip_id).include?(trip.trip_id)
end

.dataObject



9
10
11
# File 'lib/caltrain/trip.rb', line 9

def data
  DataParser.parse(trips_path)
end

.new_from_data(data) ⇒ Object



13
14
15
# File 'lib/caltrain/trip.rb', line 13

def new_from_data(data)
  new(*data)
end

.saturday_only(dir = nil) ⇒ Object



30
31
32
# File 'lib/caltrain/trip.rb', line 30

def saturday_only(dir=nil)
  trips(dir).select { |trip| trip.service_id =~ /^ST/ }
end

.today(dir = nil) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/caltrain/trip.rb', line 34

def today(dir=nil)
  if Schedule.saturday?
    weekend(dir) + saturday_only(dir)
  elsif Schedule.weekend?
    weekend(dir)
  else
    weekday(dir)
  end
end

.trips(dir = nil) ⇒ Object



17
18
19
20
# File 'lib/caltrain/trip.rb', line 17

def trips(dir=nil)
  @trips ||= []
  [:north, :south].include?(dir) ? @trips.select { |trip| trip.direction == dir } : @trips
end

.trips_pathObject



5
6
7
# File 'lib/caltrain/trip.rb', line 5

def trips_path
  "#{Caltrain.base_dir}/data/google_transit/trips.txt"
end

.weekday(dir = nil) ⇒ Object



26
27
28
# File 'lib/caltrain/trip.rb', line 26

def weekday(dir=nil)
  trips(dir).select { |trip| trip.service_id =~ /^WD/ }
end

.weekend(dir = nil) ⇒ Object



22
23
24
# File 'lib/caltrain/trip.rb', line 22

def weekend(dir=nil)
  trips(dir).select { |trip| trip.service_id =~ /^WE/ }
end

Instance Method Details

#stops(dir = @direction) ⇒ Object



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

def stops(dir=@direction)
  @stops ||= Schedule.stop_order.select { |stop| time_data.map_nth(3).any? { |d| d =~ /^#{Schedule.abbrevs[stop]}/ }  }
  dir == :north ? @stops : @stops.reverse
end

#time_at_location(loc) ⇒ Object



76
77
78
79
80
# File 'lib/caltrain/trip.rb', line 76

def time_at_location(loc)
  return nil if loc == stops.last
  loc_str = Schedule.abbrevs[loc]
  time_data.find { |row| row[3] =~ /^#{loc_str}/ }[1] rescue nil
end

#time_dataObject



68
69
70
# File 'lib/caltrain/trip.rb', line 68

def time_data
  @time_data ||= Schedule.data.select { |row| row[0] == @trip_id }
end

#timesObject



72
73
74
# File 'lib/caltrain/trip.rb', line 72

def times
  @times ||= time_data.map_nth(1)
end

#train_noObject



60
61
62
# File 'lib/caltrain/trip.rb', line 60

def train_no
  @trip_id =~ /^(\d+)_.+$/; $1.to_i
end

#typeObject



64
65
66
# File 'lib/caltrain/trip.rb', line 64

def type
  @route_id =~ /^ct_(\w+)_\d+/; $1
end