Class: Velov::StationList

Inherits:
Object
  • Object
show all
Defined in:
lib/velov/station_list.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.fetch(params = {}) ⇒ Object

Fetch data of all stations



20
21
22
23
24
# File 'lib/velov/station_list.rb', line 20

def self.fetch(params = {})
  response = API.get(params)

  build_list(response.body)
end

.from_json(json) ⇒ Object



54
55
56
# File 'lib/velov/station_list.rb', line 54

def self.from_json(json)
  build_list(JSON.parse(json))
end

Instance Method Details

#add_station(params) ⇒ Object



7
8
9
# File 'lib/velov/station_list.rb', line 7

def add_station(params)
  @list << Station.new(params)
end

#available_bike_standsObject



36
37
38
# File 'lib/velov/station_list.rb', line 36

def available_bike_stands
  @list.map(&:available_bike_stands).inject(:+)
end

#available_bikesObject



40
41
42
# File 'lib/velov/station_list.rb', line 40

def available_bikes
  @list.map(&:available_bikes).inject(:+)
end

#bike_standsObject



32
33
34
# File 'lib/velov/station_list.rb', line 32

def bike_stands
  @list.map(&:bike_stands).inject(:+)
end

#nearest(lat, lng) ⇒ Object



26
27
28
29
30
# File 'lib/velov/station_list.rb', line 26

def nearest(lat,lng)
  @list.sort_by do |station|
    station.distance_to(lat,lng)
  end
end

#sizeObject



11
12
13
# File 'lib/velov/station_list.rb', line 11

def size
  @list.size
end

#to_aObject



15
16
17
# File 'lib/velov/station_list.rb', line 15

def to_a
  @list
end

#walking_distance(start, arrival) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/velov/station_list.rb', line 44

def walking_distance(start, arrival)
  start_station = self.nearest(start.first, start.last).find do |station|
    station.status == "OPEN" and station.available_bikes > 0
  end
  arrival_station = self.nearest(arrival.first, arrival.last).find do |station|
    station.status == "OPEN" and station.available_bike_stands > 0
  end
  start_station.distance_to(start.first, start.last) + arrival_station.distance_to(arrival.first, arrival.last)
end