mybustracker 0.2.0 → 0.2.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 54a03b29fb608175ede8ece1d2edd85a90fdf3df
4
- data.tar.gz: b9fee2a737759b20a6c0b830db3f0e5e40329f08
3
+ metadata.gz: a801fd044ed04b944a8d68f6f17d306ce3caa373
4
+ data.tar.gz: 4e36c945203320806c6bf718ed85feaf08cfd954
5
5
  SHA512:
6
- metadata.gz: 416ab8ca11c80f68f38d38ac40a8b8a03d6149988d4737612ff892405db1df1220d4fc7000f86e4c7892d7bb5ec8b0189cfa2f588f003507cc52873aaed7397b
7
- data.tar.gz: df8b09df88105badb3f94c01593b0b9a058997ffc819181ada379935ef8791b422fcbf2dab481bdc3630739dca2395b39500378b64e4b5dfdcf4f0f779dd5c13
6
+ metadata.gz: 69a20bd482a626dab9b1e7735b5b8d779d2b0304acfec739c54cae73c7d53478d8c87acae690c343e12ba2e28877d4d820876e3f4787fda68540c2578035708f
7
+ data.tar.gz: 0eaa2a5bebf2d10e7263ea94b3fbb74296e09a6e9889b72c2272a44525ccc77ad98d4e541d5d92c9491ca4cfad6c2431455dc17809e88b2fc7f5ac1e86337e18
@@ -1,2 +1,3 @@
1
- q n[��#)�>�"����<���N�� ��.��۔h4���et3������^p��[���o&E�r��􅌶�.����n9BG��Z�UƂ���,��^���N��
2
- �f���f���K��$7L�8&���O8��5rΟ�3��ҽ�:.��/v9ݴ��=S]I���'g
1
+ ���y���]����y��y1��W踺L׽���Z� /y.�ApRB�Ak�-�����spj}�a�>(V$�=K|�L� �@"���l�t��+1I`��PX험+yE�8�ͨ�^
2
+ 7(9�H�laZ����F�!�ۓ�4e1�ef�L�{��skfMhl�1�R�Fb���
3
+ e
data.tar.gz.sig CHANGED
@@ -1 +1,2 @@
1
- 9��������t��B��F(��^$/Fk��n�j�nDRGX���:���;����$��!9tGM�:����T�s5x_�YA4c�� �=D����&�ǩ�cջa߲�X���W|�k촡��*��'
1
+ Q��D$��-&X�p''^�����b��u�aH�Q�����5�lE��b��u���Xst�^{ȼ�?G �����N���+����2K�7g��za��E��Na��\�v�+�'�a�����t�U�<��]�`���`t�g
2
+ �WV�i����1z�F��=Ӄ�s��� ,ˈ"��\�:��[�^VTT���W��$NG���\��#�g`-�+;����N��]�);,�A`�ɳ9:
@@ -23,28 +23,28 @@ class MyBusTracker
23
23
 
24
24
  attr_reader :ref, :operator, :number, :name, :bus_stops
25
25
 
26
- def initialize(client, appkey, service, all_dests,
27
- all_bus_stops, number='')
26
+ def initialize(client, appkey, service, all_bus_stops, number='')
28
27
 
29
28
  if number.empty? then
30
29
  raise MyBusTrackerException::Service, 'provide a bus service number'
31
30
  end
32
31
 
33
- @client, @appkey = client, appkey
34
-
35
- # find out the reference number from the service hash object
36
-
37
- #e.g. => {:ref=>"7", :operator_id=>"LB", :mnemo=>"4", :name=>"The Jewel
38
- #-- Hillend", :type=>nil, :dests=>{:list=>["458752", ...]}
32
+ @client, @appkey = client, appkey
39
33
 
40
34
  @number, @name, @ref, relative_dests, operator = \
41
35
  %i(mnemo name ref dests operator).map {|field| service[field]}
42
36
 
43
37
  @all_bus_stops = all_bus_stops
38
+
39
+ Thread.new{ fetch_bus_stops() }
44
40
 
45
41
 
46
42
  end
47
43
 
44
+ def inspect()
45
+ "<#<MyBusTracker::Service:%s @number=\"%s\">" % [self.object_id, @number]
46
+ end
47
+
48
48
  # accepts a bus service number and returns the relative bus times
49
49
  #
50
50
  def query(times: 'next hour', from: nil, to: nil)
@@ -56,36 +56,74 @@ class MyBusTracker
56
56
 
57
57
  # get the bus times
58
58
 
59
- @bus_stops, journey_times = get_stop_journeys(start_bus_stop1)
59
+ bus_times = get_bus_times(start_bus_stop1)
60
+
61
+ journey_times = get_stop_journeys(start_bus_stop1, bus_times, index: 0)
62
+ return unless journey_times
60
63
 
61
64
  # select the bus stop end
62
65
 
63
66
  end_stop = journey_times.find do |x|
64
- x[:stop_id] == end_bus_stop1[:stop_id] or x[:stop_id] == end_bus_stop2[:stop_id]
67
+ x[:stop_id] == end_bus_stop1[:stop_id] or
68
+ x[:stop_id] == end_bus_stop2[:stop_id]
65
69
  end
70
+
71
+
66
72
 
67
73
  if end_stop then
68
74
  start_bus_stop = start_bus_stop1
69
75
  else
70
76
  start_bus_stop = start_bus_stop2
71
- @bus_stops, journey_times = get_stop_journeys(start_bus_stop2) unless end_stop
72
- return unless @bus_stops
77
+ bus_times = get_bus_times(start_bus_stop2)
78
+
79
+ unless end_stop
80
+ journey_times = get_stop_journeys(start_bus_stop2, bus_times)
81
+ end
82
+
83
+ return unless journey_times
73
84
  # select the bus stop end
74
85
 
75
86
  end_stop = journey_times.find do |x|
76
- (x[:stop_id] == end_bus_stop1[:stop_id]) or (x[:stop_id] == end_bus_stop2[:stop_id])
87
+ (x[:stop_id] == end_bus_stop1[:stop_id]) or
88
+ (x[:stop_id] == end_bus_stop2[:stop_id])
77
89
  end
78
90
 
79
91
  end
92
+
93
+ stop_id = end_stop[:stop_id]
94
+
95
+ # get the other journeys
96
+ # still todo
97
+ journeys = [[journey_times[0][:time], end_stop[:time]]]
98
+ journey = get_stop_journeys(start_bus_stop, bus_times, index: 1)
99
+
100
+ if journey then
101
+ end_stop = journey.find {|x| x[:stop_id] == stop_id }
102
+ puts 'end_stop:' + end_stop.inspect
103
+
104
+ journeys << [journey[0][:time], end_stop[:time]]
105
+ end
106
+
107
+ # get the journeys for the given period
108
+ #secs = journeys[1][0][:time] - journey_times[0][:time]
109
+
110
+ from_times = journeys.map(&:first)
111
+ to_times = journeys.map(&:last)
80
112
 
81
- tstart = Time.strptime(journey_times[0][:time],"%H:%M")
82
- tend = Time.strptime(end_stop[:time],"%H:%M")
113
+ tstart = Time.strptime(journeys[0][0],"%H:%M")
114
+ tend = Time.strptime(journeys[0][-1],"%H:%M")
83
115
  travel_time = '~' + Subunit.new(units={minutes:60, hours:60}, \
84
116
  seconds: tend - tstart).to_s(omit: [:seconds])
85
117
 
118
+ index = journey_times.index journey_times.find {|x| x[:stop_id] == stop_id}
119
+ stops = journey_times[0..index].map {|x| x[:stop_name] }
120
+
86
121
  {
87
- from: {bus_stop: start_bus_stop[:name], bus_stop_id: start_bus_stop[:stop_id]},
88
- to: {bus_stop: end_stop[:stop_name], bus_stop_id: end_stop[:stop_id]},
122
+ from: {bus_stop: start_bus_stop[:name], bus_stop_id:
123
+ start_bus_stop[:stop_id], times: from_times},
124
+ to: {bus_stop: end_stop[:stop_name], bus_stop_id: end_stop[:stop_id],
125
+ times: to_times},
126
+ stops: stops ,
89
127
  start: journey_times[0][:time],
90
128
  end: end_stop[:time],
91
129
  travel_time: travel_time
@@ -113,57 +151,68 @@ class MyBusTracker
113
151
 
114
152
  end
115
153
 
116
- def get_stop_journeys(start_bus_stop)
154
+ def get_bus_times(start_bus_stop)
117
155
 
118
- response = @client.call(:get_bus_times, message: { key: @appkey, time_requests: {list: [{stop_id: start_bus_stop[:stop_id] }]} })
156
+ response = @client.call(:get_bus_times, message: { key: @appkey,
157
+ time_requests: {list: [{stop_id: start_bus_stop[:stop_id] }]} })
119
158
 
120
- bus_times = response.body[:bus_times_response][:bus_times][:list].find do |bus_time|
159
+ response.body[:bus_times_response][:bus_times][:list].find do |bus_time|
121
160
  bus_time[:ref_service] == @ref
122
- end
161
+ end
162
+ end
163
+
164
+ def get_stop_journeys(start_bus_stop, bus_times, index: 0)
123
165
 
124
- #e.g.r.keys => [:operator_id, :stop_id, :stop_name, :ref_service, :mnemo_service, :name_service,
125
- # :time_datas, :global_disruption, :service_disruption, :bus_stop_disruption, :service_diversion]
126
166
 
127
167
  return unless bus_times
128
168
 
129
169
  # get the 1st journey id
130
170
  list = bus_times[:time_datas][:list]
131
- journey_id = list.is_a?(Array) ? list[0][:journey_id] : list[:journey_id]
171
+
172
+ journey_id = list.is_a?(Array) ? list[index][:journey_id] : \
173
+ list[:journey_id]
174
+ response = @client.call(:get_journey_times, message: { key: @appkey,
175
+ journey_id: journey_id, stop_id: start_bus_stop[:stop_id] })
176
+
177
+ return unless response.body[:journey_times_response][:journey_times]
132
178
 
133
- response = @client.call(:get_journey_times, message: { key: @appkey, journey_id: journey_id, stop_id: start_bus_stop[:stop_id] })
134
179
  journey = response.body[:journey_times_response][:journey_times][:list]
135
- #=> [:journey_id, :bus_id, :operator_id, :ref_service, :mnemo_service, :name_service, :ref_dest, :name_dest, :terminus, :journey_time_datas, :global_disruption, :service_disruption, :service_diversion]
136
180
 
137
181
  journey_times = journey[:journey_time_datas][:list]
138
- # get the bus stops
139
- [journey_times.inject({}) {|r,x| r.merge(x[:stop_name] => {id: x[:stop_id]}) }, journey_times]
182
+
183
+ return journey_times
140
184
  end
141
185
 
142
- # method no longer used
186
+ # returns all bus stops for outbound route ("R")
143
187
  #
144
- def fetch_bus_stops2()
145
- # find the outbound destination
146
- dest = relative_dests[:list].find do |ref|
147
- all_dests.find {|x| x[:ref] == ref and x[:direction] == 'R' }
148
- end
188
+ def fetch_bus_stops()
149
189
 
150
- # go through each bus stop to find the dests
190
+ client, appkey, ref = @client, @appkey, @ref
191
+
192
+ response = client.call(:get_dests, message: { key: appkey })
193
+ r = response.body
194
+
195
+ response = client.call(:get_bus_stops, message: { key: appkey })
196
+ all_bus_stops = response.body[:bus_stops_response][:bus_stops][:list]
197
+
198
+ r[:dests_response][:dests][:list][0].keys
199
+ #=> [:ref, :operator_id, :name, :direction, :service]
200
+
201
+ dest = r[:dests_response][:dests][:list].find do |d|
202
+ d[:service] == ref and d[:direction] == 'R'
203
+ end
151
204
 
152
205
  raw_bus_stops = all_bus_stops.select do |bus_stop|
153
- bus_stop[:dests][:list].include? dest
206
+ bus_stop[:dests][:list].include? dest[:ref]
154
207
  end
155
208
 
156
209
  c = ->(coord){ coord.to_f.round(4)}
157
-
210
+
158
211
  response = client.call(:get_service_points, message: \
159
- { key: appkey, ref: ref })
212
+ { key: appkey, ref: ref })
160
213
  all_service_points = response.body[:service_points_response]\
161
214
  [:service_points][:list]
162
-
163
- # select only the 1st chain
164
- #service_points = all_service_points#.group_by {|x| x[:chainage]}.first[-1]
165
- #puts 'service_points: ' + service_points.inspect
166
-
215
+
167
216
  unsorted_bus_stops = []
168
217
  raw_bus_stops.each do |bus_stop|
169
218
 
@@ -174,21 +223,18 @@ class MyBusTracker
174
223
  end
175
224
 
176
225
  r.each do |x|
177
- unsorted_bus_stops << [bus_stop[:name], bus_stop[:stop_id], x, x[:order].to_i ]
226
+ unsorted_bus_stops << [bus_stop[:name],
227
+ bus_stop[:stop_id], x, x[:order].to_i ]
178
228
  end
179
229
 
180
230
  end
181
-
182
-
183
- #@bus_stops = unsorted_bus_stops.compact.sort_by(&:last)
184
- #=begin
231
+
185
232
  # find the correct chainage by the most records
186
233
  h = unsorted_bus_stops.group_by {|x| x[2][:chainage]}
187
234
  a = h.map {|k,v| v.count {|x| x[2][:chainage] == k}}
235
+
236
+ @bus_stops = h.to_a[a.index(a.max)].last.sort_by(&:last).map(&:first)
188
237
 
189
- @bus_stops = h.to_a[a.index(a.max)].last\
190
- .sort_by(&:last).inject({}){|r, x| r.merge(x[0] => {id: x[1]}) }
191
- #=end
192
238
  end
193
239
 
194
240
  end
@@ -211,10 +257,7 @@ class MyBusTracker
211
257
  Time.now.strftime("%Y%m%d%H"))
212
258
 
213
259
  response = client.call(:get_services, message: { key: appkey })
214
- @all_services= response.body[:services_response][:services][:list]
215
-
216
- response = client.call(:get_dests, message: { key: appkey })
217
- @all_dests = response.body[:dests_response][:dests][:list]
260
+ @all_services= response.body[:services_response][:services][:list]
218
261
 
219
262
  response = client.call(:get_bus_stops, message: { key: appkey })
220
263
  @all_bus_stops = response.body[:bus_stops_response][:bus_stops][:list]
@@ -222,6 +265,10 @@ class MyBusTracker
222
265
 
223
266
  end
224
267
 
268
+ def inspect()
269
+ "<#<MyBusTracker:%s>" % [self.object_id]
270
+ end
271
+
225
272
  # returns the number and name of all bus services
226
273
  #
227
274
  def services()
@@ -235,7 +282,7 @@ class MyBusTracker
235
282
  def service(number='')
236
283
 
237
284
  service = @all_services.find {|x| x[:mnemo] == number }
238
- Service.new @client, @appkey, service, @all_dests, @all_bus_stops, number
285
+ Service.new @client, @appkey, service, @all_bus_stops, number
239
286
 
240
287
  end
241
288
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mybustracker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Robertson
@@ -31,7 +31,7 @@ cert_chain:
31
31
  C8VA38E3Zv6BW0qJKU1AtzULrvfSjB6DHafrzks8aANOZ48CMYywrbaiNHG6cJaZ
32
32
  0Hd2sNogoB6McA==
33
33
  -----END CERTIFICATE-----
34
- date: 2017-08-24 00:00:00.000000000 Z
34
+ date: 2017-08-25 00:00:00.000000000 Z
35
35
  dependencies:
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: savon
metadata.gz.sig CHANGED
Binary file