class OneBusAway
Public Class Methods
# File lib/onebusaway_ruby.rb, line 9 def initialize(key, url) raise "invalid url format" unless url =~ URI::regexp self.class.base_uri(url) @api_key = key raise "cannot connect" unless valid_connection? end
Public Instance Methods
lists all supported agencies along with the center of their coverage area @return [Hash<String, Agency>] a hash of all agencies with their agency ids as keys
# File lib/onebusaway_ruby.rb, line 37 def agencies_with_coverage response = get("/agencies-with-coverage.json?key=#{@api_key}") response['data'].reduce({}) do |hash, agency| a = Agency.new.extend(AgencyRepresenter).from_json(agency['agency'].to_json) hash[a.agency_id] = a; hash end end
gets details for a specific agency @return [Agency] @param [Hash] opts @option opts [String] :agency_id the id of the agency to be returned
# File lib/onebusaway_ruby.rb, line 49 def agency(opts) raise if opts[:agency_id].nil? response = get("/agency/#{opts[:agency_id]}.json?key=#{@api_key}") Agency.new.extend(AgencyRepresenter).from_json(response['data']['entry'].to_json) end
arrival_and_departure_for_stop
- details about a specific arrival/departure at a stop
# File lib/onebusaway_ruby.rb, line 56 def arrival_and_departure_for_stop(stop_id, trip_id, service_date) response = get("/arrival-and-departure-for-stop/#{stop_id}.json?key=#{@api_key}&tripId=#{trip_id}&serviceDate=#{service_date}") end
arrivals_and_departures_for_stop
- get current arrivals and departures for a stop
# File lib/onebusaway_ruby.rb, line 61 def arrivals_and_departures_for_stop(stop_id) response = get("/arrivals-and-departures-for-stop/#{stop_id}.json?key=#{@api_key}") response['data']['arrivalsAndDepartures'].reduce({}) do |hash, ad| r = ArrivalAndDeparture.new.extend(ArrivalAndDepartureRepresenter).from_json(ad.to_json) hash[r.route_id] = r hash end end
cancel_alarm
- cancel a registered alarm
# File lib/onebusaway_ruby.rb, line 71 def cancel_alarm(alarm_id) response = get("/cancel-alarm/#{alarm_id}.json?key=#{@api_key}") end
retrieves the current system time
@return [DateTime]
# File lib/onebusaway_ruby.rb, line 78 def current_time response = get("/current-time.json?key=#{@api_key}") Time.at(response["data"]["time"] / 1000).to_datetime end
register_alarm_for_arrival_and_departure_at_stop
- register an alarm for an arrival-departure event
# File lib/onebusaway_ruby.rb, line 84 def register_alarm_for_arrival_and_departure_at_stop(stop_id, trip_id, service_date, vehicle_id, stop_sequence, alarm_time_offset, callback_url) response = get("/register-alarm-for-arrival-and-departure-at-stop/#{stop_id}.json?key=#{@api_key}&tripId=#{trip_id}&serviceDate=#{service_date}&vehicleId=#{vehicle_id}&stopSequence=#{stop_sequence}&alarmTimeOffset=#{alarm_time_offset}&url=#{callback_url}") end
route - get details for a specific route
# File lib/onebusaway_ruby.rb, line 95 def route(route_id) response = get("/route/#{route_id}.json?key=#{@api_key}") Route.new.extend(RouteRepresenter).from_json(response['data'].to_json) end
route_ids_for_agency
- get a list of all route ids for an agency
# File lib/onebusaway_ruby.rb, line 89 def route_ids_for_agency(agency) response = get("/route-ids-for-agency/#{agency}.json?key=#{@api_key}") response["data"]["list"] end
routes_for_agency
- get a list of all routes for an agency
# File lib/onebusaway_ruby.rb, line 101 def routes_for_agency(agency) response = get("/routes-for-agency/#{agency}.json?key=#{@api_key}") response['data']['list'].reduce({}) do |hash, route| r = Route.new.extend(RouteRepresenter).from_json(route.to_json) hash[r.route_id] = r hash end end
routes_for_location
- search for routes near a location, optionally by route name
# File lib/onebusaway_ruby.rb, line 111 def routes_for_location(lat, lon) response = get("/routes-for-location.json?key=#{@api_key}&lat=#{lat}&lon=#{lon}") response['data']['routes'].reduce({}) do |hash, route| r = Route.new.extend(RouteRepresenter).from_json(route.to_json) hash[r.route_id] = r hash end end
schedule_for_stop
- get the full schedule for a stop on a particular day
# File lib/onebusaway_ruby.rb, line 121 def schedule_for_stop(stop) response = get("/schedule-for-stop/#{stop}.json?key=#{@api_key}") StopSchedule.new.extend(StopScheduleRepresenter).from_json(response['data']['entry'].to_json) end
shape - get details for a specific shape (polyline drawn on a map)
# File lib/onebusaway_ruby.rb, line 127 def shape(shape_id) response = get("/shape/#{shape_id}.json?key=#{@api_key}") end
stop - get details for a specific stop
# File lib/onebusaway_ruby.rb, line 138 def stop(stop_id) response = get("/stop/#{stop_id}.json?key=#{@api_key}") Stop.new.extend(StopRepresenter).from_json(response['data'].to_json) end
stop_ids_for_agency
- get a list of all stops for an agency
# File lib/onebusaway_ruby.rb, line 132 def stop_ids_for_agency(agency) response = get("/stop-ids-for-agency/#{agency}.json?key=#{@api_key}") response["data"]["list"] end
stops_for_location
- search for stops near a location, optionally by stop code
# File lib/onebusaway_ruby.rb, line 144 def stops_for_location(lat, lon) response = get("/stops-for-location.json?key=#{@api_key}&lat=#{lat}&lon=#{lon}") response['data']['stops'].reduce({}) do |hash, stop| s = Stop.new.extend(StopRepresenter).from_json(stop.to_json) hash[s.stop_id] = s hash end end
stops_for_route
- get the set of stops and paths of travel for a particular route
# File lib/onebusaway_ruby.rb, line 154 def stops_for_route(route_id) response = get("/stops-for-route/#{route_id}.json?key=#{@api_key}&version=2") response['data']['references']['stops'].reduce({}) do |hash, stop| s = Stop.new.extend(StopRepresenter).from_json(stop.to_json) hash[s.stop_id] = s hash end end
trip - get details for a specific trip
# File lib/onebusaway_ruby.rb, line 174 def trip(trip_id) response = get("/trip/#{trip_id}.json?key=#{@api_key}") end
trip_details
- get extended details for a specific trip
# File lib/onebusaway_ruby.rb, line 164 def trip_details(trip_id) response = get("/trip-details/#{trip_id}.json?key=#{@api_key}") end
trip_for_vehicle
- get extended trip details for current trip of a specific transit vehicle
# File lib/onebusaway_ruby.rb, line 169 def trip_for_vehicle(vehicle_id) response = get("/trip-for-vehicle/#{vehicle_id}.json?key=#{@api_key}") end
trips_for_location
- get active trips near a location
# File lib/onebusaway_ruby.rb, line 179 def trips_for_location(lon, lat) response = get("/trips-for-location.json?key=#{@api_key}&lat=#{lat}&lon=#{lon}") end
trips_for_route
- get active trips for a route
# File lib/onebusaway_ruby.rb, line 184 def trips_for_route(route_id) response = get("/trips-for-route/#{route_id}.json?key=#{@api_key}") end
vehicles_for_agency
- get active vehicles for an agency
# File lib/onebusaway_ruby.rb, line 189 def vehicles_for_agency(agency_id) response = get("/vehicles-for-agency/#{agency_id}.json?key=#{@api_key}") response['data']['list'].reduce({}) do |hash, vehicle| v = Vehicle.new.extend(VehicleRepresenter).from_json(vehicle.to_json) hash[v.vehicle_id] = v hash end end
Private Instance Methods
# File lib/onebusaway_ruby.rb, line 25 def get(url) response = self.class.get(url) if response.success? raise response['text'] unless response['code'] == 200 response else raise response.response end end
# File lib/onebusaway_ruby.rb, line 16 def valid_connection? begin response = get("/current-time.json?key=#{@api_key}") true rescue return false end end