class MyBusTracker

Public Class Methods

new(api_key: '') click to toggle source
# 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

Public Instance Methods

find_nearest_stops(address, limit: 4) click to toggle source
# 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
Also aliased as: nearest_stops
inspect() click to toggle source
# File lib/mybustracker.rb, line 274
def inspect()
  "<#<MyBusTracker:%s>" % [self.object_id]
end
nearest_stops(address, limit: 4)
Alias for: find_nearest_stops
service(number='') click to toggle source

accepts a bus service number and returns the relative bus times

# 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
services() click to toggle source

returns the number and name of all bus services

# File lib/mybustracker.rb, line 280
def services()

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

end