From dbf6a498a25ec0b835e5a3fffdc621fe88fa8706 Mon Sep 17 00:00:00 2001 From: meltar Date: Wed, 6 Mar 2013 23:03:48 -0500 Subject: [PATCH 1/5] panda level complete --- movie_json.rb | 2 ++ spec/api_spec.rb | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/movie_json.rb b/movie_json.rb index d8a91d7..4152a2b 100644 --- a/movie_json.rb +++ b/movie_json.rb @@ -6,6 +6,8 @@ def find_movie movie_title = gets movie = Api.search_by_title(movie_title) puts "Found: #{movie.title}. Score: #{movie.score}" +rescue + puts "Movie not found." end find_movie diff --git a/spec/api_spec.rb b/spec/api_spec.rb index 9014106..fe98a4f 100644 --- a/spec/api_spec.rb +++ b/spec/api_spec.rb @@ -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 From 62a9dc7953653f4138bbb922702abbe136992fea Mon Sep 17 00:00:00 2001 From: meltar Date: Thu, 7 Mar 2013 00:18:20 -0500 Subject: [PATCH 2/5] tiger level complete --- movie_json.rb | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/movie_json.rb b/movie_json.rb index 4152a2b..cb00a06 100644 --- a/movie_json.rb +++ b/movie_json.rb @@ -1,23 +1,27 @@ require_relative "lib/movie" require_relative "lib/api" -def find_movie - puts "OH HAI. Search?" +def get_average_rating(movies) + scores = movies.collect { |i| i.score } + average = scores.inject(0.0) { |sum, rating| sum + rating } / scores.size +end + +def find_movie(movies) + puts "Add a movie you really like." movie_title = gets - movie = Api.search_by_title(movie_title) - puts "Found: #{movie.title}. Score: #{movie.score}" + movies << Api.search_by_title(movie_title) + puts "Found: #{movies.last.title}. Score: #{movies.last.score}" + puts "Average rating: #{get_average_rating(movies)}" rescue - puts "Movie not found." + puts "Movie not found." end -find_movie - -while true do +movies = [] +while true + find_movie(movies) puts "Search Again (Y/N)" answer = gets.upcase[0] - if answer == "Y" - find_movie - else + if answer != "Y" break end end From fe804c6bde9cd94ae518c9d737acd1d321fa1ad6 Mon Sep 17 00:00:00 2001 From: meltar Date: Thu, 7 Mar 2013 13:07:49 -0500 Subject: [PATCH 3/5] Improved code --- movie_json.rb | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/movie_json.rb b/movie_json.rb index cb00a06..1a07651 100644 --- a/movie_json.rb +++ b/movie_json.rb @@ -6,19 +6,20 @@ def get_average_rating(movies) average = scores.inject(0.0) { |sum, rating| sum + rating } / scores.size end -def find_movie(movies) +def find_movie puts "Add a movie you really like." movie_title = gets - movies << Api.search_by_title(movie_title) - puts "Found: #{movies.last.title}. Score: #{movies.last.score}" - puts "Average rating: #{get_average_rating(movies)}" + movie = Api.search_by_title(movie_title) + puts "Found: #{movie.title}. Score: #{movie.score}" + movie rescue puts "Movie not found." end movies = [] while true - find_movie(movies) + movies << find_movie + puts "Average rating: #{get_average_rating(movies)}" puts "Search Again (Y/N)" answer = gets.upcase[0] if answer != "Y" From f24689947f953a2f757269d24d0c50e6840fb68e Mon Sep 17 00:00:00 2001 From: meltar Date: Mon, 11 Mar 2013 18:37:36 -0400 Subject: [PATCH 4/5] eagle complete --- movie_json.rb | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/movie_json.rb b/movie_json.rb index 1a07651..d08c39f 100644 --- a/movie_json.rb +++ b/movie_json.rb @@ -1,9 +1,26 @@ require_relative "lib/movie" require_relative "lib/api" -def get_average_rating(movies) - scores = movies.collect { |i| i.score } - average = scores.inject(0.0) { |sum, rating| sum + rating } / scores.size +def get_average(movies, attribute) + if attribute == "score" + data = movies.collect { |i| i.score } + elsif attribute == "year" + data = movies.collect { |i| i.year } + else + data = [] + end + 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 @@ -19,10 +36,11 @@ def find_movie movies = [] while true movies << find_movie - puts "Average rating: #{get_average_rating(movies)}" - puts "Search Again (Y/N)" + puts "Average rating: #{get_average(movies, "score")}. Average year: #{get_average(movies, "year")}" + puts "Search Again (Y/N)" answer = gets.upcase[0] if answer != "Y" + calculate_slope(movies) if movies.count > 1 break end end From 6d5f6936376c0044a8a6e01c0f91fe7e62849a79 Mon Sep 17 00:00:00 2001 From: meltar Date: Tue, 12 Mar 2013 21:24:31 -0400 Subject: [PATCH 5/5] Improved error handling and attribute test --- movie_json.rb | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/movie_json.rb b/movie_json.rb index d08c39f..2941546 100644 --- a/movie_json.rb +++ b/movie_json.rb @@ -2,13 +2,7 @@ require_relative "lib/api" def get_average(movies, attribute) - if attribute == "score" - data = movies.collect { |i| i.score } - elsif attribute == "year" - data = movies.collect { |i| i.year } - else - data = [] - end + data = movies.map(&attribute) average = data.inject(0.0) { |sum, rating| sum + rating } / data.size end @@ -29,14 +23,18 @@ def find_movie movie = Api.search_by_title(movie_title) puts "Found: #{movie.title}. Score: #{movie.score}" movie -rescue +rescue NoMethodError puts "Movie not found." end movies = [] while true movies << find_movie - puts "Average rating: #{get_average(movies, "score")}. Average year: #{get_average(movies, "year")}" + 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"