module Onebusaway

Constants

API_BASE

Public Class Methods

api_key=(key) click to toggle source
# File lib/onebusaway.rb, line 6
def api_key=(key)
  @api_key = key
end
arrivals_and_departures_for_stop(params) click to toggle source
# File lib/onebusaway.rb, line 57
def arrivals_and_departures_for_stop(params)
  raise if params[:id].nil?

  doc = request('arrivals-and-departures-for-stop', params)
  arrivals = []
  doc.elements.each("arrivalsAndDepartures/arrivalAndDeparture") do |arrival_el|
    arrivals << ArrivalAndDeparture.from_xml(arrival_el)
  end
  arrivals
end
route_by_id(params) click to toggle source
# File lib/onebusaway.rb, line 17
def route_by_id(params)
  raise if params[:id].nil?

  xml = request('route', params)
  route = Route.from_xml(xml)
end
routes_for_location(params) click to toggle source
# File lib/onebusaway.rb, line 35
def routes_for_location(params)
  raise if params[:lat].nil? || params[:lon].nil?

  doc = request('routes-for-location', params)
  routes = []
  doc.elements.each("routes/route") do |route_el|
    routes << Route.from_xml(route_el)
  end
  routes
end
stop_by_id(params) click to toggle source
# File lib/onebusaway.rb, line 10
def stop_by_id(params)
  raise if params[:id].nil?

  xml = request('stop', params)
  stop = Stop.from_xml(xml)
end
stops_for_location(params) click to toggle source
# File lib/onebusaway.rb, line 24
def stops_for_location(params)
  raise if params[:lat].nil? || params[:lon].nil?

  doc = request('stops-for-location', params)
  stops = []
  doc.elements.each("stops/stop") do |stop_el|
    stops << Stop.from_xml(stop_el)
  end
  stops
end
stops_for_route(params) click to toggle source
# File lib/onebusaway.rb, line 46
def stops_for_route(params)
  raise if params[:id].nil?

  doc = request('stops-for-route', params)
  stops = []
  doc.elements.each("stops/stop") do |stop_el|
    stops << Stop.from_xml(stop_el)
  end
  stops
end

Private Class Methods

api_url(call, options = {}) click to toggle source
# File lib/onebusaway.rb, line 70
def api_url(call, options = {})
  url = API_BASE + call
  id = options.delete(:id)
  if id
    url += "/" + id
  end
  url += ".xml?"
  options[:key] = @api_key
  url += options.to_a.map{|pair| "#{pair[0]}=#{pair[1]}"}.join("&")
  url
end
request(call, url_options) click to toggle source
# File lib/onebusaway.rb, line 82
def request(call, url_options)
  url = api_url(call, url_options)

  doc = REXML::Document.new(open(url))
  root = doc.root
  status_code = root.elements["code"].text
  status_text = root.elements["text"].text

  # failed status
  unless /2\d{2}/.match(status_code)
    raise "Request failed (#{status_code}): #{status_text}"
  end

  return root.elements["data"]
end