lothianbusestimetable 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b3bf2af622eaa822e7a296b7cea1bbdb8680c7a8
4
- data.tar.gz: bc2f0e8851c2333a28196598ba339373dd61a2f5
3
+ metadata.gz: c6802c5354c2dbd6e9312f333ce0b7b9a2076cdb
4
+ data.tar.gz: 73dfb063629361ca8a03157faf17748f52994ec9
5
5
  SHA512:
6
- metadata.gz: 86524dc73bc3fdc147c83f38ce5353a3fa2689cdd7bc998617d88122321150b346a68ea6b45bfca6357bb23661da7821463bd8a27619cbd81b8d5cd72e8c51ef
7
- data.tar.gz: d03b438eca70aef97dc79b0fa012e64f27ce965903042c6f25024034c3295d24ec5babdcbddc05ba3d663572f5f321a8eb6b89905ef49f531a2f7657ebaeb8fe
6
+ metadata.gz: cb8b225f2d9ec7bd18fa726e2d25acc24b65eecb7f701fe7fb109672e51712faa3e3a196376a075103ce22b2ed26764a04d4093cc0c902113694f6823fce826a
7
+ data.tar.gz: f803b70caef9250d09645059ef17087d6391eb19d9dcdfb4ddd453773c0548eea5b8e13c707003173f38f2c61f6f2055103aaae195cdd25ffe31a68847431413
checksums.yaml.gz.sig CHANGED
Binary file
data.tar.gz.sig CHANGED
@@ -1,2 +1 @@
1
- Qx
2
- 6��ŠE*fF3�.��(�+����O�9e�
1
+ ��~��A/d�NH_�����ᛍ��^�X�i�:�ܻƓ�\R�IeD%���t�U@�E:�Z�����I�Jz$��U2(ա�p�q�=���ٹ��]��d?���np���� b٦�Z��Ћ.�b����:�PFi/���[bL�������ĵ]�{i�]��-����&���$LxGR�Bu���n���%���1�1�uWUǼ��/"�ѪfT�I<�|2~���+i1�ݼI�T�7}��[�i� ��Q�m2�F�E�⃾S
@@ -46,19 +46,26 @@ class LothianBusesTimetable
46
46
 
47
47
  # get the name
48
48
  a = tr.xpath('td//text()').map(&:unescape)
49
-
49
+
50
50
  next unless a.any?
51
51
 
52
52
  col1 = a.shift.strip
53
53
 
54
- if col1 =~ /Service (\w?\d+)/ then
54
+ if col1 =~ /^Service (\w?\d+)/ and a0.empty?then
55
55
 
56
56
  a0 << {service: $1}
57
57
 
58
- elsif col1.empty? or col1.length <= 1
58
+ elsif prev_col =~ /^Service \w?\d+$/
59
+
60
+ a0.last.merge!(timetable: col1)
61
+ a0 << {}
62
+ #=begin
63
+ elsif col1.empty? and a.length > 1
64
+ next
65
+ elsif col1.empty? or a.length <= 1
59
66
 
60
67
  #next if a.length < 1
61
- if prev_col.empty? or prev_col.length <= 1 and a0.last.any? then
68
+ if prev_col.empty? or prev_col.length <= 1 and a0.last and a0.last.any? then
62
69
 
63
70
  a0 << {}
64
71
 
@@ -66,17 +73,14 @@ class LothianBusesTimetable
66
73
 
67
74
  prev_col = ''
68
75
  end
69
-
70
- elsif prev_col =~ /Service \w?\d+/
71
-
72
- a0.last.merge!(timetable: col1)
73
-
76
+ #=end
74
77
  else
75
78
 
76
79
  if a.any? and a.length > 1 then
77
80
 
81
+ next if col1.empty?
78
82
  h = a0.last
79
-
83
+
80
84
  if h.has_key? col1 then
81
85
  h[col1].concat a
82
86
  else
@@ -85,7 +89,8 @@ class LothianBusesTimetable
85
89
 
86
90
  else
87
91
 
88
- a0.pop if a0.last.empty?
92
+ #a0.pop if a0.last.empty?
93
+ next
89
94
  end
90
95
  end
91
96
 
@@ -100,18 +105,22 @@ class LothianBusesTimetable
100
105
  saturday: {desc: '', inbound: {}, outbound: {}},
101
106
  sunday: {desc: '', inbound: {}, outbound: {}}
102
107
  }
103
-
108
+
104
109
  h = a0.shift
110
+
105
111
  master[:service] = h[:service]
106
112
  master[:weekday][:desc] = h[:timetable]
107
113
 
108
114
  h = a0.shift
115
+
109
116
  master[:weekday][:inbound] = h
110
117
  h = a0.shift
118
+
111
119
  master[:weekday][:outbound] = h
112
120
 
113
121
  h = a0.shift
114
122
  h = a0.shift until h.any?
123
+
115
124
  master[:saturday][:desc] = h[:timetable]
116
125
  h = a0.shift
117
126
  master[:saturday][:inbound] = h
@@ -120,7 +129,7 @@ class LothianBusesTimetable
120
129
 
121
130
  h = a0.shift
122
131
 
123
- if h then
132
+ if h and a0.any? then
124
133
 
125
134
  h = a0.shift until h.any?
126
135
  master[:sunday][:desc] = h[:timetable]
@@ -131,6 +140,7 @@ class LothianBusesTimetable
131
140
 
132
141
  end
133
142
 
143
+
134
144
  master.to_a[1..-1].each do |key, timetable|
135
145
 
136
146
  timetable.to_a[1..-1].each do |direction, printed_rows|
@@ -140,22 +150,27 @@ class LothianBusesTimetable
140
150
  # find the interval gaps
141
151
 
142
152
  a = printed_rows.to_a
143
- index = a.index a.detect {|x| x.last.grep(/^ |then$/).any? }
144
-
153
+
154
+ index = a.index a.detect {|x| x.last.grep(/^then$/).any? }
155
+
145
156
  if index then
146
157
  a2 = a[index].last
147
158
 
148
- gaps = a2.map.with_index.select {|x,i| x == " " \
149
- or x == 'then'}.map(&:last)
159
+ gaps = a2.map.with_index.select {|x,i| x == 'then'}.map(&:last)
150
160
 
151
161
  gaps.delete_at -1 if gaps.last >= a2.length - 1
152
162
  else
153
163
  gaps = []
154
164
  end
155
165
 
166
+
156
167
  # sanitise the times (where short hand times are
157
168
  # given i.e. minutes only)
158
169
 
170
+ =begin
171
+ # sanitise diabled for now
172
+ # probably perform this at the end now
173
+
159
174
  printed_rows.each do |name, row|
160
175
 
161
176
  prev_time = nil
@@ -171,6 +186,7 @@ class LothianBusesTimetable
171
186
  col
172
187
  when /^\d{2}$/
173
188
 
189
+ next if prev_time.nil?
174
190
  # substitute with a time
175
191
  val = "%02d%02d" % [prev_time.hour, col.to_i]
176
192
 
@@ -186,7 +202,7 @@ class LothianBusesTimetable
186
202
  end
187
203
 
188
204
  end
189
-
205
+ =end
190
206
  # fill in the gaps
191
207
 
192
208
  periods = gaps.map {|i| a.map {|k,v| v[i].to_s.gsub(/\W/,' ')}
@@ -197,39 +213,28 @@ class LothianBusesTimetable
197
213
 
198
214
  intervaltimes = gap_times.map do |i, desc|
199
215
 
200
- interval = if desc =~
201
- /^then at these mins past each hour(?: until)?/ then
202
- 60
203
- else
204
- interval = desc[/then (?:at least )?every (\d+) mins(?: until)?/,1].to_i
205
- end
206
-
207
- next if interval == 0
216
+ intervals = []
208
217
 
209
- new_rows = printed_rows.map do |k,v|
218
+ if desc =~ /^then every hour(?: until)?/ then
219
+ new_rows = every_hourly_interval printed_rows, i
220
+ elsif desc =~ /^then at these mins past each hour(?: until)?/
210
221
 
211
- times = []
212
- next unless v[i-1] && v[i-1] =~ /^\d{4}$/
213
-
214
- t = Time.strptime(v[i-1], "%H%M") #if v[i-1] =~ /^\d{4}$/
215
-
216
- while t + (60 * interval) < Time.strptime(v[i+1], "%H%M") do
217
- t = t + (60 * interval)
218
- times << t.strftime("%H%M")
219
- end
220
-
221
- [k,times]
222
- end.compact
222
+ new_rows = every_hourlyset_interval printed_rows, i
223
+ else
224
+ interval = desc[/then (?:at least )?every (\d+) mins(?: until)?/,1]
225
+ new_rows = every_interval printed_rows, i, interval
226
+ end
223
227
 
224
228
  [i, new_rows]
225
229
  end
226
230
 
227
231
  intervaltimes.reverse.each do |i, rows|
228
-
229
- rows.each do |name, xtimes|
232
+
233
+ rows.each do |name, xtimes, del_indices|
230
234
 
231
235
  printed_rows[name].delete_at i
232
236
  printed_rows[name].insert i, *xtimes
237
+ del_indices.to_a.reverse.each {|j| printed_rows[name].delete_at j}
233
238
 
234
239
  end
235
240
 
@@ -264,4 +269,78 @@ class LothianBusesTimetable
264
269
 
265
270
  end
266
271
 
272
+ private
273
+
274
+ def every_hourlyset_interval(rows, i)
275
+
276
+ rows.map do |k,v|
277
+
278
+ i -= 19 if v.length < i # caters for special journey entries which happen at limited times of the day
279
+ start_i = i - 1
280
+
281
+ start_i -= 1 while v[start_i] =~ /^(?:\d{2}|-|\||[A-Z]|) *$/
282
+
283
+ times = []
284
+
285
+ a = v[start_i+1..i-1].cycle
286
+
287
+ # get the starting hour
288
+
289
+ t1 = Time.strptime(v[start_i], "%H%M")
290
+
291
+ prev_time = t1
292
+
293
+ t2 = t1
294
+ #j = v[i+1] =~ /^\d{4}/ ? i+1 : i+2
295
+ j = i + 1
296
+ j += 1 until v[j] =~ /^\d{4}/
297
+
298
+ t3 = Time.strptime(v[j].rstrip, "%H%M")
299
+
300
+ while t2 < t3 do
301
+
302
+ val = a.next
303
+
304
+ if val =~ /^\d{2} *$/ then
305
+
306
+ t2 = Time.strptime("%02d%s" % [prev_time.hour, val], "%H%M")
307
+
308
+ t2 += 60 * 60 if t2 <= prev_time
309
+
310
+ times << t2.strftime("%H%M") if t2 < t3
311
+ prev_time = t2
312
+ else
313
+ times << val
314
+ end
315
+
316
+ end
317
+ [k,times, (start_i+1..i-1)]
318
+ end.compact
319
+
320
+
321
+ end
322
+
323
+ def every_interval(rows, i, interval=60)
324
+
325
+ return unless interval
326
+
327
+ rows.map do |k,v|
328
+
329
+ times = []
330
+
331
+ next unless v[i-1] && v[i-1] =~ /^\d{4}$/
332
+
333
+ t = Time.strptime(v[i-1], "%H%M") #if v[i-1] =~ /^\d{4}$/
334
+
335
+ while t + (60 * interval.to_i) < Time.strptime(v[i+1], "%H%M") do
336
+ t = t + (60 * interval.to_i)
337
+ times << t.strftime("%H%M")
338
+ end
339
+
340
+ [k,times]
341
+ end.compact
342
+ end
343
+
344
+ alias every_hourly_interval every_interval
345
+
267
346
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lothianbusestimetable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Robertson
metadata.gz.sig CHANGED
Binary file