class TripSpark::Client::Vehicles

Public Instance Methods

all(*routes)
Alias for: list
by_pattern(*routes) click to toggle source

Return a Hash of Pattern objects to Vehicles on that pattern. If `routes` is provided (as an array of route keys), only vehicles on those routes will be included.

# File lib/tripspark_api/client/vehicles.rb, line 8
def by_pattern *routes
  params = _route_direction_pair_params(routes)
  routes = post_request('/RouteMap/GetVehicles/', body: params)
  routes.each.with_object({}) do |route, patterns_hash|
    route['VehiclesByPattern'].each do |pat|
      pattern = Pattern.new(pat['Pattern'])
      # Apparently, vehicles can go in multiple directions concurrently,
      # even though that directly conflicts with the idea that vehicles
      # belong to patterns and patterns have one direction.
      # To avoid creating more Vehicle objects that necessary, first clear
      # the Vehicles hash of any duplicates, then create the objects.
      vehicles = pat['Vehicles'].uniq{ |vehicle| vehicle['Key'] }
      patterns_hash[pattern] = vehicles.map{ |vehicle| Vehicle.new(vehicle) }
    end
  end
end
by_route(*route_keys) click to toggle source

Return a Hash of Route keys to Vehicles. If `route_keys` are given, only those Routes will be included

# File lib/tripspark_api/client/vehicles.rb, line 37
def by_route *route_keys
  list(*route_keys).each.with_object({}) do |vehicle, route_hash|
    (route_hash[vehicle.route.key] ||= []) << vehicle
  end
end
find(key)
Alias for: get
get(key) click to toggle source

Return the vehicle whose key matches the given key

# File lib/tripspark_api/client/vehicles.rb, line 45
def get key
  list.find{ |vehicle| vehicle.key == key }
end
Also aliased as: find
list(*routes) click to toggle source

Return a list of all vehicles currently traveling on routes. If `routes` is provided (as an array of route keys), only vehicles on those routes will be included.

# File lib/tripspark_api/client/vehicles.rb, line 29
def list *routes
  by_pattern(*routes).values.flatten
end
Also aliased as: all

Private Instance Methods

_route_direction_pair_params(routes) click to toggle source

Return a parameter string suitable for supplying routeDirectionKeys to a call to /GetVehicles/. If rd_pairs is provided (as an array of route- direction_pairs), onlye those routes will be included.

# File lib/tripspark_api/client/vehicles.rb, line 56
def _route_direction_pair_params routes
  # The information to populate `routeDirectionKeys` for a request is
  # most easily found in `Client::Routes`, if it was not given directly.
  rd_pairs = Client::Routes.singleton.route_direction_pairs(*routes)
  rd_pairs.each.with_index.with_object([]) do |((route, dir), idx), params|
    params << "routeDirectionKeys[#{idx}][routeKey]=#{route}"
    params << "routeDirectionKeys[#{idx}][directionKey]=#{dir}"
  end.join('&')
end