Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
f8f7fe0
Set up Rakefile, spec_helper, ride_share.rb, and ran initial tests
ltrickey Mar 7, 2017
f93ac04
renamed files, created first tests and initialze for Trips
ltrickey Mar 7, 2017
84f6ec9
Adding let to specs, created test for find_all, failing
ltrickey Mar 7, 2017
30547eb
created find_all method
ltrickey Mar 7, 2017
343941f
added and fixed test for find all
ltrickey Mar 7, 2017
8c4dada
finished testing find_all method for trips
ltrickey Mar 7, 2017
5fc78e2
tested and created find_all_driver method
ltrickey Mar 7, 2017
9995b0b
adding find_all_rider method and tests
ltrickey Mar 7, 2017
d7d5826
added init tests for Rider and Driver classes
ltrickey Mar 8, 2017
c52a7d5
passing all init tests
ltrickey Mar 8, 2017
8e4621b
updated test for vin == 17 characters
ltrickey Mar 8, 2017
34199e2
added test and method for Driver method trips
ltrickey Mar 8, 2017
4a41b21
created average_rating method for Driver
ltrickey Mar 8, 2017
3470bb3
created find_all initial test and method for Drivers. More testing ne…
ltrickey Mar 8, 2017
02c03f7
Left note where testing should continue
ltrickey Mar 8, 2017
ed0308c
finishing testing on Drivers find_all method
ltrickey Mar 8, 2017
825c705
added testing for Driver find_driver method and tests
ltrickey Mar 8, 2017
c1be632
added initial testing for Rider.trips and method
ltrickey Mar 8, 2017
7a5e266
finish testing for Rider.trips
ltrickey Mar 8, 2017
2dc3da8
created test and method for trip find_driver
ltrickey Mar 8, 2017
45877ef
added initial test and method for find_all Class method in Riders
ltrickey Mar 8, 2017
1342507
finishing find_all for Riders, adding rescue in reading CSV
ltrickey Mar 8, 2017
649b530
finished initial test for find_rider Rider method. Refractored find_d…
ltrickey Mar 8, 2017
670f3f1
Rider.drivers initial test and method complete
ltrickey Mar 8, 2017
9631371
changed rating to be float instead of integer. Adjusted tests
ltrickey Mar 8, 2017
c079954
drying up find methods with find & find_all enumerables
ltrickey Mar 8, 2017
efae1b9
refractored Rider.trips to go through Trips class instead of directly…
ltrickey Mar 8, 2017
961322f
working on test for .trips in Rider. Testing WIP
ltrickey Mar 9, 2017
0ee2b95
updated Rider.drivers to handle duplicate driver instances & .trips t…
ltrickey Mar 9, 2017
acdea67
cleaning up indents
ltrickey Mar 9, 2017
f0bd43d
DRYd up some tests on Driver w/find_all instead of each. Added test …
ltrickey Mar 13, 2017
24473bd
used find_all to DRY up test for Rider.trips
ltrickey Mar 13, 2017
279fb4d
finished checking all code & trying to cover edge cases with testing.
ltrickey Mar 13, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/coverage/
10 changes: 9 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
# Fill me in!
require 'rake/testtask'

Rake::TestTask.new do |t|
t.libs = ["lib"]
t.warning = true
t.test_files = FileList['specs/*_spec.rb']
end

task default: :test
50 changes: 50 additions & 0 deletions lib/driver.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@

class RideShare::Driver
attr_reader :id, :name, :vin

def initialize(id, name, vin)
@id = id
@name = name

unless vin.length == 17
raise InvalidVinError.new("VIN numbers should be 17 characters")
end
@vin = vin
end

def trips
RideShare::Trip.find_all_driver(@id)
end

def average_rating
total = 0
if trips == 0
return 0
else
trips.each { |trip| total += trip.rating }
end

rating = (total.to_f / trips.length)
return rating.round(1)
end

def self.find_all
drivers = []
CSV.open("support/drivers.csv").each do |driver|
begin
drivers << RideShare::Driver.new(driver[0].to_i, driver[1], driver[2])
rescue InvalidVinError => e
puts "#{ e }"
end
end
return drivers
end

def self.find_driver(driver_id)
all_drivers = RideShare::Driver.find_all
found_driver = all_drivers.find { |driver| driver.id == driver_id }
return 0 if found_driver == nil
return found_driver
end

end
43 changes: 43 additions & 0 deletions lib/rider.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

class RideShare::Rider
attr_reader :id, :name, :phone

def initialize(id, name, phone)
unless id > 0
raise InvalidIDError.new("Rider IDs must be an Integer grater than 0")
end

@id = id
@name = name
@phone = phone
end

def trips
RideShare::Trip.find_all_rider(@id)
end

def drivers
drivers = trips.map { |trip| trip.driver }

return drivers.uniq { |driver| driver.id }
end

def self.find_all
riders = []
CSV.open("support/riders.csv").each do |rider|
begin
riders << RideShare::Rider.new(rider[0].to_i, rider[1], rider[2])
rescue InvalidIDError => e
puts "#{ e }"
end
end
return riders
end

def self.find_rider(rider_id)
all_riders = RideShare::Rider.find_all
found_rider = all_riders.find { |rider| rider.id == rider_id }
return 0 if found_rider == nil
found_rider
end
end
55 changes: 55 additions & 0 deletions lib/trip.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@

class RideShare::Trip
attr_reader :id, :driver_id, :rider_id, :date, :rating

def initialize(id, driver_id, rider_id, date, rating)
@id = id
@driver_id = driver_id
@rider_id = rider_id
@date = date

unless rating > 0 && rating < 6
raise InvalidRatingError.new("Rating must be 1-5, your rating was #{ rating }")
end
@rating = rating
end

def driver
RideShare::Driver.find_driver(@driver_id)
end

def rider
RideShare::Rider.find_rider(@rider_id)
end

def self.find_all
trips = []
CSV.open("support/trips.csv").each do |trip|
begin
trips << RideShare::Trip.new(trip[0].to_i, trip[1].to_i, trip[2].to_i, trip[3], trip[4].to_i)
rescue InvalidRatingError => e
puts "#{ e }"
end
end
return trips
end

def self.find_all_driver(driver_id)
all_trips = RideShare::Trip.find_all
driver_trips = all_trips.find_all do |trip|
trip.driver_id == driver_id
end
return 0 if driver_trips.empty?
driver_trips
end

def self.find_all_rider(rider_id)
all_trips = RideShare::Trip.find_all
rider_trips = all_trips.find_all do |trip|
trip.rider_id == rider_id
end
return 0 if rider_trips.empty?
rider_trips
end

end
70 changes: 70 additions & 0 deletions pseudocode.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
Trip CLASS

Each trip should be INSTANTIATED (initialize) with an ID, rider ID, driver ID, date, rating (each rating should be within an acceptable range 1-5)
input: Trip.new
output: new Trip object w/ applicable ratings

Retrieve associated driver instance through driver ID
input: Driver ID - send this information to Driver class
output: Driver Class returns Driver Object associated w/ trip

Retrieve associated Rider instance through Rider ID
input: Rider ID
output: Rider object associated w/ trip

Find all trip instances (driver) CLASS METHOD
input: Driver ID
output: list (ARRAY) of all trip instances for that Driver

Find all trip instances (rider) CLASS METHOD
input: Rider ID
output: list (ARRAY) of all trip instances for that Rider

Retrieve all trips from CSV file CLASS METHOD
input: calling Class method Trips.find_all
output: list all trips from CSV file.

Driver CLASS

Each driver should be INSTANTIATED (initialize) with an ID, name and VIN (VIN must be specific length to make sure it is actually a vin)
input: Driver.new
output: new instance of Driver with associated ID, Name and VIN. Each of those should be ACCESSIBLE (attr_reader)

with Driver ID we need to retrieve a list of trip instances (objects class: trip) that only this driver has taken.
input: Driver ID
output: ARRAY of Trips that this driver has taken

retrieve an average rating for that driver based on all trips taken.
input: ARRAY of trips that this driver has taken
output: Average star rating FIXNUM

Retrieve all drivers from the CSV file
input: calling the CLASS METHOD
output: list of all drivers (ARRAY) from CSV file

Find a specific driver using numeric ID
input: Numeric Driver_ID
output: Specific driver from list of drivers.


Riders CLASS

Each rider should be instantiated (INITIALIZE) with an ID, name and phone number
input: Rider.new
output: new rider object.

Retrieve a list of trips only this rider has taken
input: Rider ID
output: Array of Trip instances

Retrieve a list of all previous driver instances this rider has rode with.
input: Array of Trip instances (from above)
output: Array of the associated drivers.

Retrieve all riders from CSV file
input: calling CLASS METHOD
output: list of all RIDERS

Retrieve specific rider using numeric ID
input: Rider ID
output: Specific rider (from list above)
16 changes: 16 additions & 0 deletions ride_share.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'csv'

module RideShare; end

require_relative 'lib/driver.rb'
require_relative 'lib/trip.rb'
require_relative 'lib/rider.rb'

class InvalidRatingError < StandardError
end

class InvalidVinError < StandardError
end

class InvalidIDError < StandardError
end
93 changes: 93 additions & 0 deletions specs/driver_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
require_relative 'spec_helper'

describe "Driver" do
let(:my_driver) {RideShare::Driver.new(77, "Mr. Shanie Gusikowski", "XF9HHMKS402GD41NF")}
let(:trips_csv) {CSV.read("support/trips.csv")}
let(:all_drivers) {RideShare::Driver.find_all}
let(:drivers_csv) {CSV.read("support/drivers.csv")}

describe "Diver#initialize" do
it "takes an ID, Name, and VIN to initialize" do
my_driver.must_respond_to :id
my_driver.must_respond_to :name
my_driver.must_respond_to :vin
end

it "VIN numbers should be 17 characters" do
proc {RideShare::Driver.new(1, 2, "3")}.must_raise InvalidVinError
end
end

describe "Driver#trips" do
it "returns an array of trips that this driver has taken" do
my_driver.trips.must_be_instance_of Array
end

it "each item is of class Trip" do
my_driver.trips.each do |trip|
trip.must_be_instance_of RideShare::Trip
end
end

it "trips.length matches number of trips from CSV file for that driver" do
trips_number = my_driver.trips.length
lines_from_csv = trips_csv.find_all do |line|
line[1].to_i == my_driver.id
end
lines_from_csv.length.must_equal trips_number
end

it "Returns 0 if driver is not found" do
bad_id = RideShare::Driver.new(77777, "Mr. Shanie Gusikowski", "XF9HHMKS402GD41NF")
bad_id.trips.must_equal 0
end
end

describe "Driver#averate_rating" do
it "returns an average_rating based on all trips given" do
my_driver.average_rating.must_be_instance_of Float
my_driver.average_rating.must_equal 4.2
end

it "returns 0 if that driver has not yet had any trips" do
new_driver = RideShare::Driver.new(123456, "Ms. Lynn Trickey", "ZFLHHMKS402GD4P09")
new_driver.average_rating.must_equal 0
end
end

describe "find_all Driver class method" do
it "returns an array of Driver instances" do
all_drivers.must_be_instance_of Array
end

it "each item is of class Driver" do
all_drivers.each do |driver|
driver.must_be_instance_of RideShare::Driver
end
end

it "number of drivers matches number of lines in CSV - 1 for headder line" do
csv_length = drivers_csv.length
all_drivers.length.must_equal(csv_length - 1)
end

it "id of first & last match id of first & last in CSV" do
all_drivers[0].id.must_equal(drivers_csv[1][0].to_i)
all_drivers[-1].id.must_equal(drivers_csv[-1][0].to_i)
end
end

describe "find_driver Driver class method" do
it "should return one Driver based on numeric ID" do
my_driver = RideShare::Driver.find_driver(53)
my_driver.must_be_instance_of RideShare::Driver
end

it "should return 0 if no driver found by that ID" do
bad_id = RideShare::Driver.find_driver("apple")
other_bad_id = RideShare::Driver.find_driver(98098098098)
bad_id.must_equal 0
other_bad_id.must_equal 0
end
end
end
Loading