Class: MyBusTracker

Inherits:
Object
  • Object
show all
Defined in:
lib/mybustracker.rb

Defined Under Namespace

Classes: Service

Instance Method Summary collapse

Constructor Details

#initialize(api_key: '') ⇒ MyBusTracker

Returns a new instance of MyBusTracker.



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/mybustracker.rb', line 228

def initialize(api_key: '')


  raise MyBusTrackerException, 'api_key missing' if api_key.empty?

  @client = client = Savon.client(wsdl: 'http://ws.mybustracker.co.uk/?wsdl')

  #@client.operations

  #=> [:get_topo_id, :get_services, :get_service_points, :get_dests, 
  #    :get_bus_stops, :get_bus_times, :get_journey_times, :get_disruptions, 
  #    :get_diversions, :get_diversion_points]  

  @appkey = appkey = Digest::MD5.hexdigest( api_key + 
                                            Time.now.strftime("%Y%m%d%H"))
  
  response = client.call(:get_services, message: { key: appkey  })
  @all_services= response.body[:services_response][:services][:list]    
  
  response = client.call(:get_bus_stops, message: { key: appkey  })
  @all_bus_stops = response.body[:bus_stops_response][:bus_stops][:list]

end

Instance Method Details

#find_nearest_stops(address, limit: 4) ⇒ Object Also known as: nearest_stops



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/mybustracker.rb', line 252

def find_nearest_stops(address, limit: 4)

  results = Geocoder.search(address.sub(/,? *edinburgh$/i,'') \
                            + ", Edinburgh")
  
  p1    = Geodesic::Position.new(*results[0].coordinates)

  a = @all_bus_stops.map do |h|
    
    x, y = i(x y).map {|fields| h[fields].to_f.round(4)}

    p2 = Geodesic::Position.new(x, y)
    d = Geodesic::dist_haversine(p1.lat, p1.lon, p2.lat, p2.lon).round(4)
    [h,d]
  end
  
  a.sort_by(&:last).take limit

end

#inspectObject



274
275
276
# File 'lib/mybustracker.rb', line 274

def inspect()
  "<#<MyBusTracker:%s>" % [self.object_id]
end

#service(number = '') ⇒ Object

accepts a bus service number and returns the relative bus times



288
289
290
291
292
293
# File 'lib/mybustracker.rb', line 288

def service(number='')
  
  service = @all_services.find {|x| x[:mnemo] == number }        
  Service.new @client, @appkey, self, service,  number
  
end

#servicesObject

returns the number and name of all bus services



280
281
282
283
284
# File 'lib/mybustracker.rb', line 280

def services()

  @all_services.map {|x| [x[:mnemo], x[:name]] }.to_h

end