mybustracker 0.2.1 → 0.2.2

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: a801fd044ed04b944a8d68f6f17d306ce3caa373
4
- data.tar.gz: 4e36c945203320806c6bf718ed85feaf08cfd954
3
+ metadata.gz: 5bfe5361ebaaf8cfb2d4041c6c5be7d6f71c33bf
4
+ data.tar.gz: bb54cf30465299d3b636705e2a76e65753cbe693
5
5
  SHA512:
6
- metadata.gz: 69a20bd482a626dab9b1e7735b5b8d779d2b0304acfec739c54cae73c7d53478d8c87acae690c343e12ba2e28877d4d820876e3f4787fda68540c2578035708f
7
- data.tar.gz: 0eaa2a5bebf2d10e7263ea94b3fbb74296e09a6e9889b72c2272a44525ccc77ad98d4e541d5d92c9491ca4cfad6c2431455dc17809e88b2fc7f5ac1e86337e18
6
+ metadata.gz: 8aea94a9cae4527ef23b1027b93df78314a6b97f872730efbb404c64396a324e3de3be87175b3bc626cb6dfa0da409d5441e4acfb2424b70568b5bdfd5f76e19
7
+ data.tar.gz: 724ea11b6e270d4d9c3bd8e9f8a101f7b91f661fed8037337d7c062449fe557a1d86921280dac14bde2fd1bb6023b0c78cc3b833ff8601e61ab0d6ac73dd73fe
Binary file
data.tar.gz.sig CHANGED
@@ -1,2 +1,3 @@
1
- Q��D$��-&X�p''^�����b��u�aH�Q�����5�lE��b��u���X�st�^{ȼ�?G �����N���+����2�K�7g��za��E��Na��\�v�+�'a�����tU�<��]�`���`t�g
2
- �WV�i����1z�F��=Ӄ�s��� ,ˈ"��\�:��[�^VTT���W��$NG���\��#�g`-�+;����N��]�);,�A`�ɳ9:
1
+ ���~�L��˂.f��M��q�����X���[6`��-����t�����V�ZcX糲�:�nq���v�T���H{3{ft
2
+ �.���_X�ϼB�$}^�W�^�PL?ʝͼ�7:�w�,–&c�u�1��V��;�A�P7����t�UÂ����i���c?#k����W�93Lt��1��%��)5�h�
3
+ �3�}O���,�h����� ����ܐ��2�z����A�Oc/m����Y
@@ -23,27 +23,24 @@ class MyBusTracker
23
23
 
24
24
  attr_reader :ref, :operator, :number, :name, :bus_stops
25
25
 
26
- def initialize(client, appkey, service, all_bus_stops, number='')
26
+ def initialize(client, appkey, mbt, service, number='')
27
27
 
28
28
  if number.empty? then
29
29
  raise MyBusTrackerException::Service, 'provide a bus service number'
30
30
  end
31
31
 
32
- @client, @appkey = client, appkey
32
+ @client, @appkey, @mbt = client, appkey, mbt
33
33
 
34
34
  @number, @name, @ref, relative_dests, operator = \
35
- %i(mnemo name ref dests operator).map {|field| service[field]}
36
-
37
- @all_bus_stops = all_bus_stops
35
+ %i(mnemo name ref dests operator).map {|field| service[field]}
38
36
 
39
37
  Thread.new{ fetch_bus_stops() }
40
-
41
38
 
42
39
  end
43
40
 
44
41
  def inspect()
45
42
  "<#<MyBusTracker::Service:%s @number=\"%s\">" % [self.object_id, @number]
46
- end
43
+ end
47
44
 
48
45
  # accepts a bus service number and returns the relative bus times
49
46
  #
@@ -53,7 +50,7 @@ class MyBusTracker
53
50
 
54
51
  start_bus_stop1, start_bus_stop2 = find_bus_stop(from)
55
52
  end_bus_stop1, end_bus_stop2 = find_bus_stop(to)
56
-
53
+
57
54
  # get the bus times
58
55
 
59
56
  bus_times = get_bus_times(start_bus_stop1)
@@ -68,7 +65,6 @@ class MyBusTracker
68
65
  x[:stop_id] == end_bus_stop2[:stop_id]
69
66
  end
70
67
 
71
-
72
68
 
73
69
  if end_stop then
74
70
  start_bus_stop = start_bus_stop1
@@ -99,7 +95,6 @@ class MyBusTracker
99
95
 
100
96
  if journey then
101
97
  end_stop = journey.find {|x| x[:stop_id] == stop_id }
102
- puts 'end_stop:' + end_stop.inspect
103
98
 
104
99
  journeys << [journey[0][:time], end_stop[:time]]
105
100
  end
@@ -107,8 +102,8 @@ class MyBusTracker
107
102
  # get the journeys for the given period
108
103
  #secs = journeys[1][0][:time] - journey_times[0][:time]
109
104
 
110
- from_times = journeys.map(&:first)
111
- to_times = journeys.map(&:last)
105
+ from_times = journeys.map {|x| Time.strptime(x.first, "%H:%M").strftime("%-I:%M%P")}
106
+ to_times = journeys.map {|x| Time.strptime(x.last, "%H:%M").strftime("%-I:%M%P")}
112
107
 
113
108
  tstart = Time.strptime(journeys[0][0],"%H:%M")
114
109
  tend = Time.strptime(journeys[0][-1],"%H:%M")
@@ -124,33 +119,23 @@ class MyBusTracker
124
119
  to: {bus_stop: end_stop[:stop_name], bus_stop_id: end_stop[:stop_id],
125
120
  times: to_times},
126
121
  stops: stops ,
127
- start: journey_times[0][:time],
128
- end: end_stop[:time],
122
+ start: tstart.strftime("%-I:%M%P"),
123
+ end: tend.strftime("%-I:%M%P"),
129
124
  travel_time: travel_time
130
125
  }
131
126
 
132
127
  end
133
128
 
129
+
134
130
  private
135
131
 
136
132
  def find_bus_stop(address)
137
133
 
138
- results = Geocoder.search(address.sub(/,? *edinburgh$/i,'') \
139
- + ", Edinburgh")
140
-
141
- p1 = Geodesic::Position.new(*results[0].coordinates)
142
-
143
- a = @all_bus_stops.sort_by do |h|
144
-
145
- x, y = %i(x y).map {|fields| h[fields].to_f.round(4)}
146
-
147
- p2 = Geodesic::Position.new(x, y)
148
- d = Geodesic::dist_haversine(p1.lat, p1.lon, p2.lat, p2.lon).round(4)
149
- end
150
- a.take 2
134
+ @mbt.find_nearest_stops(address, limit: 2).map(&:first)
151
135
 
152
136
  end
153
137
 
138
+
154
139
  def get_bus_times(start_bus_stop)
155
140
 
156
141
  response = @client.call(:get_bus_times, message: { key: @appkey,
@@ -262,12 +247,33 @@ class MyBusTracker
262
247
  response = client.call(:get_bus_stops, message: { key: appkey })
263
248
  @all_bus_stops = response.body[:bus_stops_response][:bus_stops][:list]
264
249
 
250
+ end
251
+
252
+ def find_nearest_stops(address, limit: 4)
253
+
254
+ results = Geocoder.search(address.sub(/,? *edinburgh$/i,'') \
255
+ + ", Edinburgh")
256
+
257
+ p1 = Geodesic::Position.new(*results[0].coordinates)
258
+
259
+ a = @all_bus_stops.map do |h|
260
+
261
+ x, y = %i(x y).map {|fields| h[fields].to_f.round(4)}
262
+
263
+ p2 = Geodesic::Position.new(x, y)
264
+ d = Geodesic::dist_haversine(p1.lat, p1.lon, p2.lat, p2.lon).round(4)
265
+ [h,d]
266
+ end
267
+
268
+ a.sort_by(&:last).take limit
265
269
 
266
270
  end
267
271
 
272
+ alias nearest_stops find_nearest_stops
273
+
268
274
  def inspect()
269
275
  "<#<MyBusTracker:%s>" % [self.object_id]
270
- end
276
+ end
271
277
 
272
278
  # returns the number and name of all bus services
273
279
  #
@@ -282,9 +288,8 @@ class MyBusTracker
282
288
  def service(number='')
283
289
 
284
290
  service = @all_services.find {|x| x[:mnemo] == number }
285
- Service.new @client, @appkey, service, @all_bus_stops, number
286
-
287
- end
291
+ Service.new @client, @appkey, self, service, number
288
292
 
293
+ end
289
294
 
290
295
  end
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.1
4
+ version: 0.2.2
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-25 00:00:00.000000000 Z
34
+ date: 2017-08-31 00:00:00.000000000 Z
35
35
  dependencies:
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: savon
metadata.gz.sig CHANGED
Binary file