Class: Caltrain

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

Class Method Summary collapse

Class Method Details

.abbrevsObject



55
56
57
58
59
60
61
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
# File 'lib/caltrain.rb', line 55

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

.actionsObject



109
110
111
# File 'lib/caltrain.rb', line 109

def actions
  { :list => :upcoming_departures, :next => :next_departure }
end

.all_timesObject



39
40
41
# File 'lib/caltrain.rb', line 39

def all_times
  @all_times ||= File.read(times_path).split(/[\n\r]+/)[1..-1].map { |line| line.gsub('"', '').split(/,+/) }
end

.all_tripsObject



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

def all_trips
  @all_trips ||= File.read(trips_path).split(/[\n\r]+/)[1..-1].map { |line| line.gsub('"', '').split(/,+/) }.sort
end

.base_dirObject



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

def base_dir
  File.expand_path('..', File.dirname(__FILE__))
end

.clean!Object



113
114
115
116
# File 'lib/caltrain.rb', line 113

def clean!
  instance_variables.each { |i| instance_variable_set(i, nil) }
  true
end

.next_departure(loc, dir) ⇒ Object



19
20
21
# File 'lib/caltrain.rb', line 19

def next_departure(loc, dir)
  times(loc, dir).find { |time| time > now }
end

.nowObject



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

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

.pretty_abbrevsObject



91
92
93
94
95
96
97
# File 'lib/caltrain.rb', line 91

def pretty_abbrevs
  max_field_width = abbrevs.values.map(&:size).max

  abbrevs.values.zip(abbrevs.keys).each do |name, abr|
    puts "%#{max_field_width}s #{abr}" % name
  end
end

.run!(args) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/caltrain.rb', line 118

def run!(args)
  begin
    act, loc, dir = *args

    if dir =~ /^n/
      puts method(actions[act.to_sym]).call(loc.to_sym, :north)
    elsif dir =~ /^s/
      puts method(actions[act.to_sym]).call(loc.to_sym, :south)
    else
      raise
    end
  rescue
    usage
  end
end

.times(loc, dir) ⇒ Object



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

def times(loc, dir)
  @times ||= times_for_location(loc).select { |line| trip_ids_for_direction(dir).include?(line[0]) }.map {|i| i[1]}.sort
end

.times_for_location(loc) ⇒ Object



35
36
37
# File 'lib/caltrain.rb', line 35

def times_for_location(loc)
  @times_for_location ||= all_times.select { |arr| arr[3] =~ /^#{abbrevs[loc]}/ }
end

.times_pathObject



7
8
9
# File 'lib/caltrain.rb', line 7

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

.trip_ids_for_direction(dir) ⇒ Object



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

def trip_ids_for_direction(dir)
  @trip_ids_for_direction ||= trips_for_time_of_week.select {|trip| trip[4] == (dir == :north ? '0' : '1') }.map(&:first)
end

.trips_for_time_of_weekObject



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

def trips_for_time_of_week
  @trips_for_time_of_week ||= all_trips.select { |trip| trip[2] =~ (weekend? ? /^WE/ : /^WD/) }
end

.trips_pathObject



11
12
13
# File 'lib/caltrain.rb', line 11

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

.upcoming_departures(loc, dir) ⇒ Object



15
16
17
# File 'lib/caltrain.rb', line 15

def upcoming_departures(loc, dir)
  times(loc, dir).select { |time| time > now }.sort
end

.usageObject



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

def usage
  puts "Usage:"
  puts "  caltrain <action> <location> <direction>"
  puts "  action => [ next | list ]"
  puts ""
  puts "Abbreviations:"
  pretty_abbrevs
  exit(1)
end

.weekend?Boolean

Returns:

  • (Boolean)


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

def weekend?
  @weekend ||= (Time.now.saturday? || Time.now.sunday?)
end