Class: MBTARealtime::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/mbta-realtime/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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" }

  @options = {query: { api_key: @api_key, format: @format }}
  @options.freeze
end

Instance Attribute Details

#api_keyObject (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

#formatObject (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

Raises:

  • (NotImplementedError)


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

#routesObject



23
24
25
# File 'lib/mbta-realtime/client.rb', line 23

def routes
  self.class.get("/routes", @options).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