Module: Schedule

Extended by:
Printing
Defined in:
lib/caltrain/schedule.rb

Class Method Summary collapse

Methods included from Printing

pretty_hash, print_trip, stop_info, train_info

Class Method Details

.abbrevsObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/caltrain/schedule.rb', line 62

def abbrevs
  @abbrevs ||= {
    :gil => "Gilroy",
    :smt => "San Martin",
    :mrg => "Morgan Hill",
    :bhl => "Blossom Hill",
    :cap => "Capitol",
    :tam => "Tamien",
    :sj  => "San Jose",
    :clp => "College Park",
    :sc  => "Santa Clara",
    :law => "Lawrence",
    :sv  => "Sunnyvale",
    :mv  => "Mountain View",
    :sa  => "San Antonio",
    :cal => "California Ave",
    :pa  => "Palo Alto",
    :men => "Menlo Park",
    :ath => "Atherton",
    :rc  => "Redwood City",
    :scl => "San Carlos",
    :bel => "Belmont",
    :hil => "Hillsdale",
    :hay => "Hayward Park",
    :sm  => "San Mateo",
    :brl => "Burlingame",
    :bdw => "Broadway",
    :mil => "Millbrae",
    :sb  => "San Bruno",
    :ssf => "So. San Francisco",
    :bsh => "Bayshore",
    :tt  => "22nd Street",
    :sf  => "San Francisco"
  }.freeze
end

.dataObject



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

def data
  @data ||= DataParser.parse(times_path)
end

.list(loc, dir, output = $stdout) ⇒ Object



13
14
15
16
17
18
# File 'lib/caltrain/schedule.rb', line 13

def list(loc, dir, output=$stdout)
  trips = trips_with_times(loc, dir).select { |_, time| time > now }
  options = {:output => output, :starting_at => loc}

  trips.each { |trip, time| print_trip(trip, time, options) }
end

.next(loc, dir, output = $stdout) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/caltrain/schedule.rb', line 20

def next(loc, dir, output=$stdout)
  trip = trips_with_times(loc, dir).find { |_, time| time > now }
  raise("No trips found!") unless trip
  options = {:output => output, :starting_at => loc}

  print_trip(trip.first, trip.last, options)
end

.nowObject



46
47
48
# File 'lib/caltrain/schedule.rb', line 46

def now
  Time.now.strftime('%H:%M:%S')
end

.saturday?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/caltrain/schedule.rb', line 58

def saturday?
  Time.now.strftime("%A") == "Saturday"
end

.stop_orderObject

for 1.8 support :/



99
100
101
102
103
104
105
# File 'lib/caltrain/schedule.rb', line 99

def stop_order
  @stop_order ||= [
    :gil, :smt, :mrg, :bhl, :cap, :tam, :sj, :clp, :sc, :law, :sv,
    :mv, :sa, :cal, :pa, :men, :ath, :rc, :scl, :bel, :hil, :hay, :sm,
    :brl, :bdw, :mil, :sb, :ssf, :bsh, :tt, :sf
  ].freeze
end

.sunday?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/caltrain/schedule.rb', line 54

def sunday?
  Time.now.strftime("%A") == "Sunday"
end

.times_pathObject



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

def times_path
  "#{Caltrain.base_dir}/data/google_transit/stop_times.txt"
end

.trips_for_today(dir) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/caltrain/schedule.rb', line 36

def trips_for_today(dir)
  if weekend?
    Trip.weekend(dir)
  elsif saturday?
    Trip.saturday_only(dir)
  else
    Trip.weekday(dir)
  end
end

.trips_with_times(loc, dir) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/caltrain/schedule.rb', line 28

def trips_with_times(loc, dir)
  @trips_with_times ||= trips_for_today(dir).map do |trip|
    if time = trip.time_at_location(loc)
      [trip, time]
    end
  end.compact.sort_by_nth(1)
end

.weekend?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/caltrain/schedule.rb', line 50

def weekend?
  saturday? || sunday?
end