metra_schedule 0.2.2 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,25 @@
1
+ <div id="serviceAdvisory">
2
+
3
+
4
+
5
+ <h2><span class="hidden">Service Advisories</span></h2>
6
+
7
+
8
+ <dl>
9
+
10
+
11
+
12
+
13
+
14
+ <dt class="line md-w first">Milwaukee District / West Line</dt>
15
+
16
+
17
+
18
+
19
+ <dd class="first"><a href="#serviceAdvisory1">Inbound Train #2226 15 Minute Delay</a></dd>
20
+ <dd id="serviceAdvisory1" class="details">Train #2226, scheduled to arrive Chicago at 9:00 a.m., is operating approximately 15 minutes behind schedule due to earlier delays. Updated at 8:34 a.m.</dd>
21
+
22
+
23
+ </dl>
24
+ </div>
25
+
@@ -3,20 +3,20 @@ require File.join(File.dirname(__FILE__), "../", "test_helper.rb")
3
3
  class TestLine < Test::Unit::TestCase
4
4
  include MetraSchedule::TrainData
5
5
 
6
- def up_nw_stub
6
+ def setup
7
7
  f = File.open(File.join(File.dirname(__FILE__), '../fixture/UP_NW.html'), 'r')
8
8
  parser = MetraSchedule::Parser.new f
9
9
  parser.line = LINES[:up_nw]
10
- line = Metra.new.line(:up_nw)
11
- line.engines = parser.scrape
12
- line
10
+ @line = Metra.new.line(:up_nw)
11
+ @line.engines = parser.scrape
12
+ @line
13
13
  end
14
14
 
15
15
  def test_all_filters
16
- line = up_nw_stub.from(:ogilve).to(:barrington).at(Time.parse("11:29PM")).on(Date.parse("Dec 27 2009"))
17
- assert_equal(1, line.trains.count)
18
- line = up_nw_stub.from(:ogilve).to(:barrington).at(Time.parse("3:00AM")).on(Date.parse("Dec 27 2009"))
19
- assert_equal(7, line.trains.count)
16
+ @line.from(:ogilve).to(:barrington).at(Time.parse("11:29PM")).on(Date.parse("Dec 27 2009"))
17
+ assert_equal(2, @line.trains.count)
18
+ @line.from(:ogilve).to(:barrington).at(Time.parse("3:00AM")).on(Date.parse("Dec 27 2009"))
19
+ assert_equal(8, @line.trains.count)
20
20
  end
21
21
 
22
22
  end
@@ -9,4 +9,20 @@ class TestLine < Test::Unit::TestCase
9
9
  assert_not_nil(line.trains)
10
10
  end
11
11
 
12
+ def test_inject_delays_filter
13
+ cleanup_cache_dir
14
+ data = [{:train_num => 602, :delay => (10..15)}]
15
+ result = MetraSchedule::Cacher.new.persist_delays(data)
16
+ assert_equal(true, result)
17
+ line = Metra.new.line(:up_nw).from(:barrington).to(:ogilve)
18
+ line.load_schedule
19
+ trains = line.trains
20
+ the_602 = trains.find {|t| t.train_num == 602}
21
+ assert_not_nil(the_602)
22
+ assert_not_nil(the_602.delay)
23
+ assert_equal(data.first[:delay], the_602.delay)
24
+ cleanup_cache_dir
25
+ end
26
+
27
+
12
28
  end
data/test/test_helper.rb CHANGED
@@ -2,6 +2,7 @@ require 'date'
2
2
  require 'time'
3
3
  require 'test/unit'
4
4
  require 'fileutils'
5
+ require 'rubygems'
5
6
 
6
7
  require File.join(File.dirname(__FILE__), "timecop", "lib", "timecop")
7
8
  require File.join(File.dirname(__FILE__), "../lib", "metra")
@@ -76,4 +76,33 @@ class TestLine < Test::Unit::TestCase
76
76
  cleanup_dir
77
77
  end
78
78
 
79
+ def test_persist_delays
80
+ cleanup_dir
81
+ c = MetraSchedule::Cacher.new
82
+ data = [{:train_num => 800, :delay => (15..30)}]
83
+ assert_equal(true, c.persist_delays(data))
84
+ cleanup_dir
85
+ end
86
+
87
+ def test_retrieve_delays
88
+ cleanup_dir
89
+ c = MetraSchedule::Cacher.new
90
+ data = [{:train_num => 800, :delay => (15..30)}]
91
+ c.persist_delays(data)
92
+ assert_equal(data, c.retrieve_delays)
93
+ cleanup_dir
94
+ end
95
+
96
+ def test_clear_delays
97
+ cleanup_dir
98
+ c = MetraSchedule::Cacher.new
99
+ data = [{:train_num => 800, :delay => (15..30)}]
100
+ c.persist_delays(data)
101
+ assert_equal(true, c.delays_exist?)
102
+ assert_equal(true, c.clear_delays)
103
+ assert_equal(false, c.delays_exist?)
104
+ assert_equal(false, c.clear_delays)
105
+ cleanup_dir
106
+ end
107
+
79
108
  end
@@ -0,0 +1,76 @@
1
+ require File.join(File.dirname(__FILE__), "../", "test_helper.rb")
2
+
3
+ class TestDelayParser < Test::Unit::TestCase
4
+ include MetraSchedule::TrainData
5
+
6
+ def advisory_alert_stub
7
+ f = File.open(File.join(File.dirname(__FILE__), '../fixture/service_updates_alerts.html'), 'r')
8
+ parser = MetraSchedule::DelayParser.new f
9
+ parser.scrape
10
+ parser
11
+ end
12
+
13
+ def no_advisory_stub
14
+ f = File.open(File.join(File.dirname(__FILE__), '../fixture/service_updates_alerts_no_advisories.html'), 'r')
15
+ parser = MetraSchedule::DelayParser.new f
16
+ parser.scrape
17
+ parser
18
+ end
19
+
20
+ def no_range_stub
21
+ f = File.open(File.join(File.dirname(__FILE__), '../fixture/service_updates_alerts_no_range.html'), 'r')
22
+ parser = MetraSchedule::DelayParser.new f
23
+ parser.scrape
24
+ parser
25
+ end
26
+
27
+ def no_delay_advisory
28
+ f = File.open(File.join(File.dirname(__FILE__), '../fixture/service_updates_alerts_multiple_with_non_delay.html'), 'r')
29
+ parser = MetraSchedule::DelayParser.new f
30
+ parser.scrape
31
+ parser
32
+ end
33
+
34
+ def test_init
35
+ assert_nothing_raised do
36
+ @@p = MetraSchedule::DelayParser.new 'http://blake.com'
37
+ end
38
+ assert_not_nil(advisory_alert_stub.html_doc)
39
+ assert_not_nil(@@p.html_doc)
40
+ end
41
+
42
+ def test_find_train_num
43
+ node = advisory_alert_stub.html_doc.css('#serviceAdvisory')
44
+ assert_equal(2156, advisory_alert_stub.find_train_num(node))
45
+ end
46
+
47
+ def test_find_delay
48
+ node = advisory_alert_stub.html_doc.css('#serviceAdvisory')
49
+ assert_equal("20 minutes delayed", advisory_alert_stub.find_delay(node))
50
+ end
51
+
52
+ def test_make_train_delays
53
+ assert_equal([{:train_num => 2156, :delay => "18 - 20 minutes delayed"}], advisory_alert_stub.make_train_delays)
54
+ end
55
+
56
+ def test_has_delays?
57
+ assert_equal(true, advisory_alert_stub.has_delays?)
58
+ assert_equal(false, no_advisory_stub.has_delays?)
59
+ end
60
+
61
+ def test_find_delay_no_range
62
+ node = no_range_stub.html_doc.css('#serviceAdvisory dd.first')
63
+ assert_equal("15 Minute Delay", no_range_stub.find_delay(node))
64
+ end
65
+
66
+ def test_find_delay
67
+ node = advisory_alert_stub.html_doc.css('#serviceAdvisory dd.first')
68
+ assert_equal("18 - 20 minutes delayed", advisory_alert_stub.find_delay(node))
69
+ end
70
+
71
+ def test_find_delay_advisory_with_no_delay
72
+ node = no_delay_advisory.html_doc.css('#serviceAdvisory dd.first')
73
+ assert_equal("Stopped North of Ashburn", no_delay_advisory.find_delay(node))
74
+ end
75
+
76
+ end
@@ -1,4 +1,9 @@
1
1
  require File.join(File.dirname(__FILE__), "../", "test_helper.rb")
2
+ begin
3
+ require 'mocha'
4
+ rescue
5
+ puts "Not all the tests may function properly without Mocha installed. gem install mocha"
6
+ end
2
7
 
3
8
  class TestLine < Test::Unit::TestCase
4
9
 
@@ -189,6 +194,21 @@ class TestLine < Test::Unit::TestCase
189
194
  assert_equal([train2], valid_trains)
190
195
  end
191
196
 
197
+ def test_trains_filter_by_start_with_delay_threshold
198
+ line = Metra.new.line(:up_nw).delay_threshold(10)
199
+
200
+ stop1 = MetraSchedule::Stop.new :station => :arlington_heights, :time => Time.parse('12:30')
201
+ stop2 = MetraSchedule::Stop.new :station => :arlington_heights, :time => Time.parse('12:40')
202
+ stop3 = MetraSchedule::Stop.new :station => :ogilve, :time => Time.parse('13:30')
203
+ train1 = MetraSchedule::Train.new :stops => [stop1, stop3], :direction => :inbound, :schedule => :weekday
204
+ train2 = MetraSchedule::Train.new :stops => [stop2, stop3], :direction => :inbound, :schedule => :weekday
205
+ train1.delay = "Delayed"
206
+ line.engines = [train1, train2]
207
+
208
+ valid_trains = line.to(:ogilve).from(:arlington_heights).at('12:35').trains
209
+ assert_equal([train1, train2], valid_trains)
210
+ end
211
+
192
212
  def test_trains_just_direction_and_time
193
213
  line = Metra.new.line(:up_nw)
194
214
 
@@ -321,4 +341,10 @@ class TestLine < Test::Unit::TestCase
321
341
  assert_equal(train1, line.find_train_by_train_num(642))
322
342
  end
323
343
 
344
+ def test_delay_threshold
345
+ line = Metra.new.line(:up_nw)
346
+ line.delay_threshold(5)
347
+ assert_equal(5, line.del_threshold)
348
+ end
349
+
324
350
  end
@@ -3,7 +3,7 @@ require File.join(File.dirname(__FILE__), "../", "test_helper.rb")
3
3
  class TestLine < Test::Unit::TestCase
4
4
  include MetraSchedule::TrainData
5
5
 
6
- def up_nw_stub
6
+ def p
7
7
  f = File.open(File.join(File.dirname(__FILE__), '../fixture/UP_NW.html'), 'r')
8
8
  parser = MetraSchedule::Parser.new f
9
9
  parser.line = LINES[:up_nw]
@@ -11,6 +11,10 @@ class TestLine < Test::Unit::TestCase
11
11
  parser
12
12
  end
13
13
 
14
+ def up_nw_stub
15
+ p
16
+ end
17
+
14
18
  def test_init
15
19
  assert_nothing_raised do
16
20
  @@p = MetraSchedule::Parser.new 'http://blake.com'
@@ -20,20 +24,17 @@ class TestLine < Test::Unit::TestCase
20
24
  end
21
25
 
22
26
  def test_seperate_tables
23
- p = up_nw_stub
24
27
  assert_equal(2, p.tables.find_all {|t| t[:schedule] == :weekday}.count)
25
28
  assert_equal(2, p.tables.find_all {|t| t[:schedule] == :saturday}.count)
26
29
  assert_equal(2, p.tables.find_all {|t| t[:schedule] == :sunday}.count)
27
30
  end
28
31
 
29
32
  def test_sanitize
30
- p = up_nw_stub
31
33
  list = [1, 2, 3]
32
34
  assert_equal(2, p.sanitize(list).count)
33
35
  end
34
36
 
35
37
  def test_train_count
36
- p = up_nw_stub
37
38
  assert_equal(16, p.train_count(p.tables[0][:tables][0]))
38
39
  assert_equal(16, p.train_count(p.tables[0][:tables][1]))
39
40
  assert_equal(12, p.train_count(p.tables[1][:tables][0]))
@@ -45,13 +46,10 @@ class TestLine < Test::Unit::TestCase
45
46
  end
46
47
 
47
48
  def test_stop_count
48
- p = up_nw_stub
49
49
  assert_equal(23, p.stop_count(p.tables[0][:tables][0]))
50
50
  end
51
51
 
52
52
  def test_make_stop_inbound
53
- p = up_nw_stub
54
-
55
53
  node = p.tables[0][:tables][0].xpath("tbody[4]/tr/td[1]")[0]
56
54
  expected = MetraSchedule::Stop.new :station => :crystal_lake, :time => Time.parse("4:47AM")
57
55
  assert_equal(expected.station, p.make_stop(node, 3, :inbound).station)
@@ -59,8 +57,6 @@ class TestLine < Test::Unit::TestCase
59
57
  end
60
58
 
61
59
  def test_make_stop_outbound
62
- p = up_nw_stub
63
-
64
60
  node = p.tables[3][:tables][0].xpath("tbody[4]/tr/td[1]")[0]
65
61
  expected = MetraSchedule::Stop.new :station => :jefferson_park, :time => Time.parse("6:13AM")
66
62
  assert_equal(expected.station, p.make_stop(node, 3, :outbound).station)
@@ -68,15 +64,11 @@ class TestLine < Test::Unit::TestCase
68
64
  end
69
65
 
70
66
  def test_make_stop_nil
71
- p = up_nw_stub
72
-
73
67
  node = p.tables[0][:tables][0].xpath("tbody[1]/tr/td[1]")[0]
74
68
  assert_nil(p.make_stop(node, 0, :inbound))
75
69
  end
76
70
 
77
71
  def test_make_stop_am
78
- p = up_nw_stub
79
-
80
72
  node = p.tables[0][:tables][1].xpath("tbody[18]/tr/td[5]")[0]
81
73
  expected = MetraSchedule::Stop.new :station => :norwood_park, :time => Time.parse("11:57AM")
82
74
  assert_equal(expected.station, p.make_stop(node, 17, :inbound).station)
@@ -84,8 +76,6 @@ class TestLine < Test::Unit::TestCase
84
76
  end
85
77
 
86
78
  def test_make_stop_pm
87
- p = up_nw_stub
88
-
89
79
  node = p.tables[0][:tables][1].xpath("tbody[20]/tr/td[5]")[0]
90
80
  expected = MetraSchedule::Stop.new :station => :jefferson_park, :time => Time.parse("12:01PM")
91
81
  assert_equal(expected.station, p.make_stop(node, 19, :inbound).station)
@@ -93,8 +83,6 @@ class TestLine < Test::Unit::TestCase
93
83
  end
94
84
 
95
85
  def test_make_stop_pm_express
96
- p = up_nw_stub
97
-
98
86
  node = p.tables[3][:tables][1].xpath("tbody[2]/tr/td[5]")[0]
99
87
  expected = MetraSchedule::Stop.new :station => :clyborn, :time => Time.parse("5:29PM")
100
88
  assert_equal(expected.station, p.make_stop(node, 1, :outbound).station)
@@ -102,15 +90,11 @@ class TestLine < Test::Unit::TestCase
102
90
  end
103
91
 
104
92
  def test_find_stops
105
- p = up_nw_stub
106
-
107
93
  table = p.tables[0][:tables][0]
108
94
  assert_equal(19, p.find_stops(table, 1, :inbound).count)
109
95
  end
110
96
 
111
97
  def test_find_stops_express
112
- p = up_nw_stub
113
-
114
98
  table = p.tables[0][:tables][0]
115
99
  stops = p.find_stops(table, 5, :inbound)
116
100
  assert_equal(Time.parse("6:43AM").to_f , stops[6].time.to_f)
@@ -119,13 +103,11 @@ class TestLine < Test::Unit::TestCase
119
103
  end
120
104
 
121
105
  def test_make_trains_count
122
- p = up_nw_stub
123
106
  trains = p.make_trains
124
107
  assert_equal(104, p.make_trains.count)
125
108
  end
126
109
 
127
110
  def test_find_train_num
128
- p = up_nw_stub
129
111
  train_602 = p.make_trains.first
130
112
  train_632 = p.make_trains[15]
131
113
 
@@ -134,7 +116,6 @@ class TestLine < Test::Unit::TestCase
134
116
  end
135
117
 
136
118
  def test_find_bike_count
137
- p = up_nw_stub
138
119
  train_602 = p.make_trains.first
139
120
  train_640 = p.make_trains[18]
140
121
 
@@ -9,4 +9,11 @@ class TestTimeExtension < Test::Unit::TestCase
9
9
  assert_equal(expected, sample.to_today)
10
10
  end
11
11
 
12
+ def test_to_today_midnight_inclusive
13
+ sample = Time.local(2009, 5, 5, 0, 30, 45)
14
+ today = Time.now
15
+ expected = Time.local(today.year, today.month, today.day+1, sample.hour, sample.min, sample.sec)
16
+ assert_equal(expected, sample.to_today)
17
+ end
18
+
12
19
  end
@@ -50,6 +50,16 @@ class TestLine < Test::Unit::TestCase
50
50
  assert_equal(false, @@t.in_time?(:barrington, Time.parse("12:35PM")))
51
51
  end
52
52
 
53
+ def test_in_time_midnight_inclusive
54
+ stop1 = MetraSchedule::Stop.new :station => :arlington_heights, :time => Time.parse("12:30AM")
55
+ stop2 = MetraSchedule::Stop.new :station => :cary, :time => Time.parse("2:45AM")
56
+ stop3 = MetraSchedule::Stop.new :station => :harvard, :time => Time.parse("4:00AM")
57
+ @@t = MetraSchedule::Train.new :train_num => 651, :bike_limit => 12, :schedule => :weekday, :direction => :inbound, :stops => [stop1, stop2, stop3]
58
+ assert_equal(true, @@t.in_time?(:arlington_heights, Time.parse("11:45PM")))
59
+ assert_equal(true, @@t.in_time?(:cary, Time.parse("11:45PM")))
60
+ assert_equal(false, @@t.in_time?(:harvard, Time.parse("11:45PM")))
61
+ end
62
+
53
63
  def test_in_time_same_time_next_day
54
64
  stop1 = MetraSchedule::Stop.new :station => :barrington, :time => Time.parse("12:30PM")
55
65
  stop2 = MetraSchedule::Stop.new :station => :arlington_heights, :time => Time.parse("12:40PM")
@@ -165,4 +175,11 @@ class TestLine < Test::Unit::TestCase
165
175
  assert_equal("2 hours 5 minutes", train.print_my_travel_time)
166
176
  end
167
177
 
178
+ def test_departure_with_delay
179
+ train = MetraSchedule::Train.new :train_num => 651, :bike_limit => 12, :schedule => :weekday, :direction => :inbound
180
+ train.my_departure = Time.parse("12:30PM")
181
+ train.del_threshold = 5
182
+ assert_equal(Time.parse("12:35PM"), train.departure_with_delay)
183
+ end
184
+
168
185
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: metra_schedule
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Blake Smith
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-05 00:00:00 -06:00
12
+ date: 2010-02-03 00:00:00 -06:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -40,6 +40,7 @@ files:
40
40
  - lib/metra.rb
41
41
  - lib/metra/cacher.rb
42
42
  - lib/metra/classmethods.rb
43
+ - lib/metra/delay_parser.rb
43
44
  - lib/metra/extensions/time_extension.rb
44
45
  - lib/metra/line.rb
45
46
  - lib/metra/parser.rb
@@ -48,6 +49,10 @@ files:
48
49
  - lib/metra/train_data.rb
49
50
  - metra_schedule.gemspec
50
51
  - test/fixture/UP_NW.html
52
+ - test/fixture/service_updates_alerts.html
53
+ - test/fixture/service_updates_alerts_multiple_with_non_delay.html
54
+ - test/fixture/service_updates_alerts_no_advisories.html
55
+ - test/fixture/service_updates_alerts_no_range.html
51
56
  - test/functional/test_all_filters.rb
52
57
  - test/integration/test_line_integration.rb
53
58
  - test/test_helper.rb
@@ -61,6 +66,7 @@ files:
61
66
  - test/timecop/lib/timecop/time_stack_item.rb
62
67
  - test/timecop/lib/timecop/timecop.rb
63
68
  - test/unit/test_cacher.rb
69
+ - test/unit/test_delay_parser.rb
64
70
  - test/unit/test_line.rb
65
71
  - test/unit/test_metra.rb
66
72
  - test/unit/test_parser.rb
@@ -108,5 +114,6 @@ test_files:
108
114
  - test/unit/test_metra.rb
109
115
  - test/unit/test_stop.rb
110
116
  - test/unit/test_parser.rb
117
+ - test/unit/test_delay_parser.rb
111
118
  - test/unit/test_cacher.rb
112
119
  - test/test_helper.rb