class BikeShare

Constants

BAY_AREA_BIKE_JSON_URL
FIRST_STATION_ID

Public Class Methods

new(url = nil) click to toggle source
# 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

Public Instance Methods

available_bikes(station_id) click to toggle source
# 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) click to toggle source
# 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_stations() click to toggle source
# File lib/bikeshare.rb, line 34
def empty_stations
  @response.select { |station| station["availableBikes"] == 0 }
end
full?(station_id) click to toggle source
# 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_id() click to toggle source
# File lib/bikeshare.rb, line 13
def last_station_id
  @response.last["id"]
end
lat_and_long(station_id) click to toggle source
# 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_stations() click to toggle source
# File lib/bikeshare.rb, line 81
def offline_stations
  list = @response.select { |station| station["statusKey"] == 0 }

  list.empty? ? [] : list
end
percent_available(station_id) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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

Private Instance Methods

check_valid_station_id!(station_id) click to toggle source
# File lib/bikeshare.rb, line 100
def check_valid_station_id!(station_id)
  first = FIRST_STATION_ID
  last = last_station_id

  raise "Please enter a station id in between #{first} and #{last}" unless station_id.between?(first, last)
end