Class: MBTARealtime::Client
- Inherits:
-
Object
- Object
- MBTARealtime::Client
- Includes:
- HTTParty
- Defined in:
- lib/mbta-realtime/client.rb
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#format ⇒ Object
readonly
Returns the value of attribute format.
Instance Method Summary collapse
-
#initialize(args = {}) ⇒ Client
constructor
A new instance of Client.
- #nearest_stop(location = {}) ⇒ Object
- #nearest_stop_by_location_name(intersection) ⇒ Object
- #nearest_stop_id(location = {}) ⇒ Object
- #nearest_stop_id_by_location_name(intersection) ⇒ Object
- #predictions_by_location_name(intersection, flatten = false) ⇒ Object
- #predictions_by_route(route_id) ⇒ Object
- #predictions_by_stop(stop_id, flatten = false) ⇒ Object
- #routes ⇒ Object
- #routes_by_stop(stop_id) ⇒ Object
- #stop_name(stop_id) ⇒ Object
- #stops_by_location(location = {}) ⇒ Object
- #stops_by_location_name(intersection) ⇒ Object
- #stops_by_route(route_id) ⇒ Object
Constructor Details
#initialize(args = {}) ⇒ Client
Returns a new instance of Client.
11 12 13 14 15 16 17 |
# File 'lib/mbta-realtime/client.rb', line 11 def initialize(args={}) @api_key = args.fetch(:api_key) { args } @format = args.fetch(:format) { "json" } = {query: { api_key: @api_key, format: @format }} .freeze end |
Instance Attribute Details
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
7 8 9 |
# File 'lib/mbta-realtime/client.rb', line 7 def api_key @api_key end |
#format ⇒ Object (readonly)
Returns the value of attribute format.
7 8 9 |
# File 'lib/mbta-realtime/client.rb', line 7 def format @format end |
Instance Method Details
#nearest_stop(location = {}) ⇒ Object
52 53 54 |
# 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) ⇒ Object
68 69 70 |
# 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 = {}) ⇒ Object
57 58 59 |
# 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) ⇒ Object
73 74 75 |
# 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) ⇒ Object
89 90 91 92 |
# File 'lib/mbta-realtime/client.rb', line 89 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) ⇒ Object
84 85 86 |
# File 'lib/mbta-realtime/client.rb', line 84 def predictions_by_route(route_id) raise NotImplementedError end |
#predictions_by_stop(stop_id, flatten = false) ⇒ Object
78 79 80 81 |
# 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 |
#routes ⇒ Object
23 24 25 |
# File 'lib/mbta-realtime/client.rb', line 23 def routes self.class.get("/routes", ).to_h end |
#routes_by_stop(stop_id) ⇒ Object
28 29 30 |
# 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) ⇒ Object
36 37 38 |
# 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 = {}) ⇒ Object
46 47 48 49 |
# 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) ⇒ Object
62 63 64 65 |
# 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) ⇒ Object
41 42 43 |
# 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 |