Class: Caltrain

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

Constant Summary collapse

TIMES_PATH =
'data/google_transit/stop_times.txt'
TRIPS_PATH =
'data/google_transit/trips.txt'

Class Method Summary collapse

Class Method Details

.abbrevsObject



46
47
48
49
50
51
52
53
54
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
# File 'lib/caltrain.rb', line 46

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



100
101
102
# File 'lib/caltrain.rb', line 100

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

.all_timesObject



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

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

.all_tripsObject



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

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

.clean!Object



104
105
106
107
# File 'lib/caltrain.rb', line 104

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

.next_departure(loc, dir) ⇒ Object



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

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

.nowObject



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

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

.pretty_abbrevsObject



82
83
84
85
86
87
88
# File 'lib/caltrain.rb', line 82

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



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/caltrain.rb', line 109

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



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

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



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

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

.trip_ids_for_direction(dir) ⇒ Object



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

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



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

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

.upcoming_departures(loc, dir) ⇒ Object



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

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

.usageObject



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

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)


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

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