Class: BikeShare

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

Constant Summary collapse

FIRST_STATION_ID =
2
BAY_AREA_BIKE_JSON_URL =
"http://bayareabikeshare.com/stations/json"

Instance Method Summary collapse

Constructor Details

#initialize(url = nil) ⇒ BikeShare

Returns a new instance of BikeShare.



6
7
8
9
10
11
# File 'lib/bikeshare.rb', line 6

def initialize(url = nil)
  response_url = url || BAY_AREA_BIKE_JSON_URL

  response = JSON.parse(open(response_url).read)
  @response = response["stationBeanList"]
end

Instance Method Details

#available_bikes(station_id) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/bikeshare.rb', line 54

def available_bikes(station_id)
  check_valid_station_id!(station_id)

  station = @response.select { |station| station["id"] == station_id }

  station.first["availableBikes"]
end

#empty?(station_id) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
44
# File 'lib/bikeshare.rb', line 38

def empty?(station_id)
  check_valid_station_id!(station_id)

  station = @response.select { |station| station["id"] == station_id }

  station.first["availableBikes"] == 0 ? true : false
end

#empty_stationsObject



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

def empty_stations
  @response.select { |station| station["availableBikes"] == 0 }
end

#full?(station_id) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
49
50
51
52
# File 'lib/bikeshare.rb', line 46

def full?(station_id)
  check_valid_station_id!(station_id)

  station = @response.select { |station| station["id"] == station_id }

  station.first["availableBikes"] == station.first["totalDocks"] ? true : false
end

#last_station_idObject



13
14
15
# File 'lib/bikeshare.rb', line 13

def last_station_id
  @response.last["id"]
end

#lat_and_long(station_id) ⇒ Object



87
88
89
90
91
92
93
94
95
96
# File 'lib/bikeshare.rb', line 87

def lat_and_long(station_id)
  check_valid_station_id!(station_id)

  station = @response.select { |station| station["id"] == station_id }

  lat = station.first["latitude"]
  long = station.first["longitude"]

  [lat, long]
end

#offline_stationsObject



81
82
83
84
85
# File 'lib/bikeshare.rb', line 81

def offline_stations
  list = @response.select { |station| station["statusKey"] == 0 }

  list.empty? ? [] : list
end

#percent_available(station_id) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/bikeshare.rb', line 69

def percent_available(station_id)
  check_valid_station_id!(station_id)

  station = @response.select { |station| station["id"] == station_id }

  available = (station.first["availableBikes"]).to_f
  total = (station.first["totalDocks"]).to_f

  percentage = (available * 100.0) / total
  percentage.round(2)
end

#station_info(station_id) ⇒ Object



17
18
19
20
21
22
# File 'lib/bikeshare.rb', line 17

def station_info(station_id)
  check_valid_station_id!(station_id)

  station = @response.select { |station| station["id"] == station_id } 
  station.first
end

#stations(*city_name) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/bikeshare.rb', line 24

def stations(*city_name)
  city_name = city_name.first

  if city_name.nil?
    stations = @response
  else
    stations = @response.select { |station| station["landMark"] == "#{city_name}" }
  end
end

#total_docks(station_id) ⇒ Object



62
63
64
65
66
67
# File 'lib/bikeshare.rb', line 62

def total_docks(station_id)
  check_valid_station_id!(station_id)

  station = @response.select { |station| station["id"] == station_id }
  station.first["totalDocks"]
end