Class: OneBusAway

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/onebusaway_ruby.rb

Instance Method Summary collapse

Constructor Details

#initialize(key, url) ⇒ OneBusAway

Returns a new instance of OneBusAway.



9
10
11
12
13
14
# 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

Instance Method Details

#agencies_with_coverageHash<String, Agency>

lists all supported agencies along with the center of their coverage area

Returns:

  • (Hash<String, Agency>)

    a hash of all agencies with their agency ids as keys



37
38
39
40
41
42
43
# 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

#agency(opts) ⇒ Agency

gets details for a specific agency

Parameters:

  • opts (Hash)

Options Hash (opts):

  • :agency_id (String)

    the id of the agency to be returned

Returns:



49
50
51
52
53
# 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(stop_id, trip_id, service_date) ⇒ Object

arrival_and_departure_for_stop - details about a specific arrival/departure at a stop



56
57
58
# 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(stop_id) ⇒ Object

arrivals_and_departures_for_stop - get current arrivals and departures for a stop



61
62
63
64
65
66
67
68
# 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(alarm_id) ⇒ Object

cancel_alarm - cancel a registered alarm



71
72
73
# File 'lib/onebusaway_ruby.rb', line 71

def cancel_alarm(alarm_id)
  response = get("/cancel-alarm/#{alarm_id}.json?key=#{@api_key}")
end

#current_timeDateTime

retrieves the current system time

Returns:

  • (DateTime)


78
79
80
81
# 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(stop_id, trip_id, service_date, vehicle_id, stop_sequence, alarm_time_offset, callback_url) ⇒ Object

register_alarm_for_arrival_and_departure_at_stop - register an alarm for an arrival-departure event



84
85
86
# 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(route_id) ⇒ Object

route - get details for a specific route



95
96
97
98
# 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(agency) ⇒ Object

route_ids_for_agency - get a list of all route ids for an agency



89
90
91
92
# 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(agency) ⇒ Object

routes_for_agency - get a list of all routes for an agency



101
102
103
104
105
106
107
108
# 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(lat, lon) ⇒ Object

routes_for_location - search for routes near a location, optionally by route name



111
112
113
114
115
116
117
118
# 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(stop) ⇒ Object

schedule_for_stop - get the full schedule for a stop on a particular day



121
122
123
124
# 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(shape_id) ⇒ Object

shape - get details for a specific shape (polyline drawn on a map)



127
128
129
# File 'lib/onebusaway_ruby.rb', line 127

def shape(shape_id)
  response = get("/shape/#{shape_id}.json?key=#{@api_key}")
end

#stop(stop_id) ⇒ Object

stop - get details for a specific stop



138
139
140
141
# 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(agency) ⇒ Object

stop_ids_for_agency - get a list of all stops for an agency



132
133
134
135
# 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(lat, lon) ⇒ Object

stops_for_location - search for stops near a location, optionally by stop code



144
145
146
147
148
149
150
151
# 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(route_id) ⇒ Object

stops_for_route - get the set of stops and paths of travel for a particular route



154
155
156
157
158
159
160
161
# 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(trip_id) ⇒ Object

trip - get details for a specific trip



174
175
176
# File 'lib/onebusaway_ruby.rb', line 174

def trip(trip_id)
  response = get("/trip/#{trip_id}.json?key=#{@api_key}")
end

#trip_details(trip_id) ⇒ Object

trip_details - get extended details for a specific trip



164
165
166
# 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(vehicle_id) ⇒ Object

trip_for_vehicle - get extended trip details for current trip of a specific transit vehicle



169
170
171
# 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(lon, lat) ⇒ Object

trips_for_location - get active trips near a location



179
180
181
# 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(route_id) ⇒ Object

trips_for_route - get active trips for a route



184
185
186
# 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(agency_id) ⇒ Object

vehicles_for_agency - get active vehicles for an agency



189
190
191
192
193
194
195
196
# 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