class MBTARealtime::Client
Attributes
api_key[R]
format[R]
Public Class Methods
new(args={})
click to toggle source
# File lib/mbta-realtime/client.rb, line 11 def initialize(args={}) @api_key = args.fetch(:api_key) { args } @format = args.fetch(:format) { "json" } @options = {query: { api_key: @api_key, format: @format }} @options.freeze end
Public Instance Methods
flat_complex_predictions(stop_id, route_ids)
click to toggle source
# File lib/mbta-realtime/client.rb, line 99 def flat_complex_predictions(stop_id, route_ids) flatten_predictions(predictions_by_stop_and_route(stop_id, route_ids)) end
nearest_stop(location={})
click to toggle source
# File lib/mbta-realtime/client.rb, line 52 def nearest_stop(location={}) stops_by_location(location)["stop"].first end
nearest_stop_by_location_name(intersection)
click to toggle source
# File lib/mbta-realtime/client.rb, line 68 def nearest_stop_by_location_name(intersection) stops_by_location_name(intersection)["stop"].first end
nearest_stop_id(location={})
click to toggle source
# File lib/mbta-realtime/client.rb, line 57 def nearest_stop_id(location={}) nearest_stop(location)["stop_id"] end
nearest_stop_id_by_location_name(intersection)
click to toggle source
# File lib/mbta-realtime/client.rb, line 73 def nearest_stop_id_by_location_name(intersection) nearest_stop_by_location_name(intersection)["stop_id"] end
predictions_by_location_name(intersection, flatten=false)
click to toggle source
# File lib/mbta-realtime/client.rb, line 109 def predictions_by_location_name(intersection, flatten=false) ps = predictions_by_stop( nearest_stop_id_by_location_name(intersection) ) flatten ? flatten_predictions( ps ) : ps end
predictions_by_route(route_id)
click to toggle source
# File lib/mbta-realtime/client.rb, line 104 def predictions_by_route(route_id) raise NotImplementedError end
predictions_by_stop(stop_id, flatten=false)
click to toggle source
# File lib/mbta-realtime/client.rb, line 78 def predictions_by_stop(stop_id, flatten=false) ps = self.class.get("/predictionsbystop", url_opts({stop: stop_id})).to_h flatten ? flatten_predictions(ps) : ps end
predictions_by_stop_and_route(stop_id, route_ids)
click to toggle source
# File lib/mbta-realtime/client.rb, line 84 def predictions_by_stop_and_route(stop_id, route_ids) preds = predictions_by_stop(stop_id) rids = route_ids.map {|i| i.to_s } return preds if rids.empty? preds['mode'].each do |mode| # Keep only predictions that are within the route ID mode['route'].keep_if { |route| rids.include?(route['route_name']) } end preds end
routes()
click to toggle source
Routes
# File lib/mbta-realtime/client.rb, line 23 def routes self.class.get("/routes", @options).to_h end
routes_by_stop(stop_id)
click to toggle source
# File lib/mbta-realtime/client.rb, line 28 def routes_by_stop(stop_id) self.class.get("/routesbystop", url_opts({stop: stop_id})).to_h end
stop_name(stop_id)
click to toggle source
Stops
# File lib/mbta-realtime/client.rb, line 36 def stop_name(stop_id) self.class.get("/schedulebystop", url_opts({stop: stop_id})).to_h["stop_name"] end
stops_by_location(location={})
click to toggle source
# File lib/mbta-realtime/client.rb, line 46 def stops_by_location(location={}) # Requires a hash {lat: 42.35291,lon: -71.064648} self.class.get("/stopsbylocation", url_opts( location )).to_h end
stops_by_location_name(intersection)
click to toggle source
# File lib/mbta-realtime/client.rb, line 62 def stops_by_location_name(intersection) lat, lng = Geocoder.coordinates(intersection) stops_by_location(lat: lat, lon: lng) end
stops_by_route(route_id)
click to toggle source
# File lib/mbta-realtime/client.rb, line 41 def stops_by_route(route_id) self.class.get("/stopsbyroute", url_opts({route: route_id})).to_h end
Private Instance Methods
flatten_predictions(predictions_response)
click to toggle source
# File lib/mbta-realtime/client.rb, line 122 def flatten_predictions(predictions_response) @predictions = [] travel_modes = predictions_response['mode'] travel_modes.each do |mode| routes = mode['route'] routes.each do |route| route_obj = OpenStruct.new(id: route['route_id'], name: route['route_name'], trips: []) directions = route['direction'] directions.each do |dir| trips = dir['trip'] trips.each do |trip| route_obj.trips << OpenStruct.new(id: trip['trip_id'], time: trip['pre_away']) end end @predictions << route_obj end end @predictions end
url_opts(args={})
click to toggle source
# File lib/mbta-realtime/client.rb, line 118 def url_opts(args={}) { query: @options[:query].merge( args ) } end