Class: BusTime::BusStop

Inherits:
Object
  • Object
show all
Defined in:
lib/bus_time/bus_stop.rb

Overview

Service bus stop handling, including prediction retrieval via ‘BusTime::Api`

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, name, coords:, direction:, routes: []) ⇒ BusStop

Returns a new instance of BusStop.



11
12
13
14
15
16
17
18
# File 'lib/bus_time/bus_stop.rb', line 11

def initialize(id, name, coords:, direction:, routes: [])
  @id = id
  @name = name
  @lat, @lon = coords
  @direction = direction

  @routes = routes
end

Instance Attribute Details

#directionObject

Returns the value of attribute direction.



9
10
11
# File 'lib/bus_time/bus_stop.rb', line 9

def direction
  @direction
end

#idObject (readonly)

Returns the value of attribute id.



5
6
7
# File 'lib/bus_time/bus_stop.rb', line 5

def id
  @id
end

#latObject (readonly)

Returns the value of attribute lat.



5
6
7
# File 'lib/bus_time/bus_stop.rb', line 5

def lat
  @lat
end

#lonObject (readonly)

Returns the value of attribute lon.



5
6
7
# File 'lib/bus_time/bus_stop.rb', line 5

def lon
  @lon
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/bus_time/bus_stop.rb', line 5

def name
  @name
end

#predictionsObject



20
21
22
# File 'lib/bus_time/bus_stop.rb', line 20

def predictions
  @predictions || fetch_predictions
end

#routesObject

Returns the value of attribute routes.



9
10
11
# File 'lib/bus_time/bus_stop.rb', line 9

def routes
  @routes
end

Instance Method Details

#distance_from(lat, lon) ⇒ Object



28
29
30
31
32
# File 'lib/bus_time/bus_stop.rb', line 28

def distance_from(lat, lon)
  require "geocoder"

  Geocoder::Calculations.distance_between([@lat, @lon], [lat, lon])
end

#fetch_predictionsObject



24
25
26
# File 'lib/bus_time/bus_stop.rb', line 24

def fetch_predictions
  @predictions = BusTime.api.fetch_predictions(@id)
end

#nearby?(lat, lon, max_nearby_distance = BusTime.nearby_distance) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/bus_time/bus_stop.rb', line 34

def nearby?(lat, lon, max_nearby_distance = BusTime.nearby_distance)
  distance_from(lat, lon) <= max_nearby_distance
end