Class: MetraSchedule::Line

Inherits:
Object
  • Object
show all
Includes:
TrainData
Defined in:
lib/metra/line.rb

Constant Summary

Constants included from TrainData

TrainData::LINES

Instance Attribute Summary collapse

Attributes included from TrainData

#LINES

Instance Method Summary collapse

Constructor Details

#initialize(line_name) ⇒ Line

Returns a new instance of Line.



11
12
13
14
15
16
17
18
19
# File 'lib/metra/line.rb', line 11

def initialize(line_name)
  raise ArgumentError.new "That's not a valid line symbol. Please see the list in the README" \
    unless LINES.has_key?(line_name)
  raise ArgumentError.new "Please pass a symbol containing the line name" unless line_name.is_a?(Symbol)
  @line_key = line_name
  @name = LINES[line_name][:name]
  @url = LINES[line_name][:url]
  @filters = [filter_by_stop, filter_by_start, filter_by_direction, filter_by_schedule, inject_my_times]
end

Instance Attribute Details

#cache_dirObject (readonly)

Returns the value of attribute cache_dir.



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

def cache_dir
  @cache_dir
end

#cacherObject (readonly)

Returns the value of attribute cacher.



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

def cacher
  @cacher
end

#destinationObject (readonly)

Returns the value of attribute destination.



8
9
10
# File 'lib/metra/line.rb', line 8

def destination
  @destination
end

#dirObject (readonly)

Returns the value of attribute dir.



8
9
10
# File 'lib/metra/line.rb', line 8

def dir
  @dir
end

#enginesObject

Returns the value of attribute engines.



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

def engines
  @engines
end

#line_keyObject (readonly)

Returns the value of attribute line_key.



8
9
10
# File 'lib/metra/line.rb', line 8

def line_key
  @line_key
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/metra/line.rb', line 8

def name
  @name
end

#schedObject (readonly)

Returns the value of attribute sched.



8
9
10
# File 'lib/metra/line.rb', line 8

def sched
  @sched
end

#startObject (readonly)

Returns the value of attribute start.



8
9
10
# File 'lib/metra/line.rb', line 8

def start
  @start
end

#timeObject (readonly)

Returns the value of attribute time.



8
9
10
# File 'lib/metra/line.rb', line 8

def time
  @time
end

#urlObject (readonly)

Returns the value of attribute url.



8
9
10
# File 'lib/metra/line.rb', line 8

def url
  @url
end

Instance Method Details

#at(time) ⇒ Object



121
122
123
124
125
126
127
128
129
130
# File 'lib/metra/line.rb', line 121

def at(time)
  if time.is_a?(String)
    @time = Time.parse(time) 
  elsif time.is_a?(Time)
    @time = time 
  else
    raise ArgumentError.new "Time must be either a valid time object, or a string that parses to one"
  end
  self
end

#config(args) ⇒ Object



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

def config(args)
  @cacher = args[:cacher] if args.has_key?(:cacher)
  @cache_dir = args[:cache_dir] if args.has_key?(:cache_dir)
end

#deduce_directionObject



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/metra/line.rb', line 54

def deduce_direction
  return @dir if @dir
  return :unknown unless @start and @destination
  if LINES[@line_key][:stations].rindex(@start) < LINES[@line_key][:stations].rindex(@destination)
    :outbound
  elsif LINES[@line_key][:stations].rindex(@start) > LINES[@line_key][:stations].rindex(@destination)
    :inbound
  else
    :unknown
  end
end

#deduce_direction_by_timeObject



140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/metra/line.rb', line 140

def deduce_direction_by_time
  before_noon = Time.now < Time.parse("12:00PM")
  after_noon = Time.now > Time.parse("12:00PM")
  after_midnight_until_two = (Time.now >= Time.parse("12:00AM") and Time.now < Time.parse("2:00AM"))
  if after_noon or after_midnight_until_two
    @dir = :outbound
  elsif before_noon
    @dir = :inbound
  else
    @dir = :inbound
  end
  self
end

#deduce_scheduleObject



99
100
101
# File 'lib/metra/line.rb', line 99

def deduce_schedule
  on(Date.today)
end

#direction(dir = nil) ⇒ Object



48
49
50
51
52
# File 'lib/metra/line.rb', line 48

def direction(dir=nil)
  raise ArgumentError.new "Direction must be either :inbound or :outbound" unless dir == :outbound || dir == :inbound || dir == nil
  @dir = dir unless dir == nil
  self
end

#find_train_by_train_num(train_num) ⇒ Object



154
155
156
# File 'lib/metra/line.rb', line 154

def find_train_by_train_num(train_num)
  @engines.find {|e| e.train_num == train_num}
end

#from(station) ⇒ Object



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

def from(station)
  set_station(:start, station)
end

#holidayObject



95
96
97
# File 'lib/metra/line.rb', line 95

def holiday
  schedule(:holiday)
end

#inboundObject



71
72
73
# File 'lib/metra/line.rb', line 71

def inbound
  direction(:inbound)
end

#load_scheduleObject



26
27
28
29
30
31
32
33
# File 'lib/metra/line.rb', line 26

def load_schedule
  cached_engines = MetraSchedule::Cacher.load_from_cache(self)
  unless cached_engines
    update_schedule
  else
    @engines = cached_engines
  end
end

#on(date) ⇒ Object



132
133
134
135
136
137
138
# File 'lib/metra/line.rb', line 132

def on(date)
  raise ArgumentError.new "Argument must be a date object!" unless date.is_a?(Date)
  @sched = :weekday if (1..5).include?(date.cwday)
  @sched = :saturday if date.cwday == 6
  @sched = :sunday if date.cwday == 7
  self
end

#outboundObject



67
68
69
# File 'lib/metra/line.rb', line 67

def outbound
  direction(:outbound)
end

#saturdayObject



87
88
89
# File 'lib/metra/line.rb', line 87

def saturday
  schedule(:saturday)
end

#schedule(sched = nil) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/metra/line.rb', line 75

def schedule(sched=nil)
  unless sched == :weekday or sched == :saturday or sched == :sunday or sched == :holiday or sched == nil
    raise ArgumentError.new "Schedule must be :weekday, :saturday, :sunday or :holiday"
  end
  @sched = sched unless sched == nil
  self
end

#set_station(start_or_destination, station) ⇒ Object



103
104
105
106
107
108
109
110
111
# File 'lib/metra/line.rb', line 103

def set_station(start_or_destination, station)
  unless start_or_destination == :start or start_or_destination == :destination
    raise ArgumentError.new "First argument must be either :start or :destination"
  end
  raise ArgumentError.new "Not a valid station" unless LINES[@line_key][:stations].include?(station)
  @start = station if start_or_destination == :start
  @destination = station if start_or_destination == :destination 
  self
end

#sundayObject



91
92
93
# File 'lib/metra/line.rb', line 91

def sunday
  schedule(:sunday)
end

#to(station) ⇒ Object



117
118
119
# File 'lib/metra/line.rb', line 117

def to(station)
  set_station(:destination, station)
end

#trainsObject



158
159
160
161
# File 'lib/metra/line.rb', line 158

def trains
  return [] unless engines
  @filters.inject(engines) { |e, fun| fun.call(e) }.sort
end

#update_scheduleObject



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/metra/line.rb', line 35

def update_schedule
  parser = MetraSchedule::Parser.new @url
  parser.line = LINES[@line_key]
  new_trains = parser.scrape
  if new_trains
    @engines = new_trains
    MetraSchedule::Cacher.store_to_cache(self)
    true
  else
    false
  end
end

#weekdayObject



83
84
85
# File 'lib/metra/line.rb', line 83

def weekday
  schedule(:weekday)
end