Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Episode 2 #15

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
39 changes: 31 additions & 8 deletions movie_json.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,44 @@
require_relative "lib/movie"
require_relative "lib/api"

def get_average(movies, attribute)
data = movies.map(&attribute)
average = data.inject(0.0) { |sum, rating| sum + rating } / data.size
end

def calculate_slope(movies)
movies = movies.sort_by { |i| i.year }
slope = (movies.last.score - movies.first.score).to_f /
(movies.last.year - movies.first.year).to_f
if slope < 0
puts "You are getting madder."
elsif slope > 0
puts "You are getting happier."
end
end

def find_movie
puts "OH HAI. Search?"
puts "Add a movie you really like."
movie_title = gets
movie = Api.search_by_title(movie_title)
puts "Found: #{movie.title}. Score: #{movie.score}"
movie
rescue NoMethodError
puts "Movie not found."
end

find_movie

while true do
puts "Search Again (Y/N)"
answer = gets.upcase[0]
if answer == "Y"
find_movie
movies = []
while true
movies << find_movie
if movies.last.nil?
movies.pop
else
puts "Average rating: #{get_average(movies, :score)}. Average year: #{get_average(movies, :year)}"
end
puts "Search Again (Y/N)"
answer = gets.upcase[0]
if answer != "Y"
calculate_slope(movies) if movies.count > 1
break
end
end
6 changes: 6 additions & 0 deletions spec/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,10 @@
it "should return the year" do
movie.year.should eq(1994)
end

it "should not throw an error" do
expect {
Api.search_by_title("NOTHINGFOUNDHERE")
}.to_not raise_error
end
end