From 1c8dd934f803f94c3e45fa593b74d32c8d93eab5 Mon Sep 17 00:00:00 2001 From: Ralphos Date: Sun, 9 Dec 2012 18:50:38 +0800 Subject: [PATCH 01/11] code does not blow up when searching for non-existent movie --- lib/api.rb | 18 +++++++++++------- movie_json.rb | 6 +++++- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/lib/api.rb b/lib/api.rb index a8d499c..a1296bb 100644 --- a/lib/api.rb +++ b/lib/api.rb @@ -4,21 +4,25 @@ require_relative "./movie" class Api - APIKEY="4t6456xa33z8qhcqyuqgnkjh" + APIKEY="u2wh4hkgt7xkuqh6bhgm7f83" def self.search_by_title(title) url = "http://api.rottentomatoes.com/api/public/v1.0/movies.json?apikey=#{APIKEY}&q=#{URI.encode(title)}&page_limit=1" struct = OpenStruct.new(get_url_as_json(url).fetch("movies").first) - Movie.new(id: struct.id.to_i, - title: struct.title, - year: struct.year, - score: struct.ratings["critics_score"] - ) + begin + Movie.new(id: struct.id.to_i, + title: struct.title, + year: struct.year, + score: struct.ratings["critics_score"] + ) + rescue + nil + end end def self.get_url_as_json(url) - JSON.parse(open(url).read) + JSON.parse(open(url, 'User-Agent' => 'Ruby').read) end end diff --git a/movie_json.rb b/movie_json.rb index d8a91d7..15f8c41 100644 --- a/movie_json.rb +++ b/movie_json.rb @@ -5,7 +5,11 @@ def find_movie puts "OH HAI. Search?" movie_title = gets movie = Api.search_by_title(movie_title) - puts "Found: #{movie.title}. Score: #{movie.score}" + if movie + puts "Found: #{movie.title}. Score: #{movie.score}" + else + puts "Movie not found." + end end find_movie From dc199f301ac24f9f9b883640ae5dc23982ffd52a Mon Sep 17 00:00:00 2001 From: Ralphos Date: Sun, 9 Dec 2012 19:32:12 +0800 Subject: [PATCH 02/11] tests for context in which movie is not found --- lib/api.rb | 2 +- spec/api_spec.rb | 31 ++++++++++++++++++++++++------- spec/fixtures/no_movie.json | 1 + 3 files changed, 26 insertions(+), 8 deletions(-) create mode 100644 spec/fixtures/no_movie.json diff --git a/lib/api.rb b/lib/api.rb index a1296bb..a30a710 100644 --- a/lib/api.rb +++ b/lib/api.rb @@ -16,7 +16,7 @@ def self.search_by_title(title) score: struct.ratings["critics_score"] ) rescue - nil + nil end end diff --git a/spec/api_spec.rb b/spec/api_spec.rb index 9014106..4859ebe 100644 --- a/spec/api_spec.rb +++ b/spec/api_spec.rb @@ -13,15 +13,32 @@ movie.title.should eq("Forrest Gump") end - it "should return the score" do - movie.score.should eq(71) - end + context "when movie is found" do + + it "should return the score" do + movie.score.should eq(71) + end + + it "should return the id" do + movie.id.should eq(10036) + end - it "should return the id" do - movie.id.should eq(10036) + it "should return the year" do + movie.year.should eq(1994) + end end - it "should return the year" do - movie.year.should eq(1994) + context "when movie is not found" do + + before do + Api.stub(:get_url_as_json) { JSON.parse(File.read("spec/fixtures/no_movie.json")) } + end + + let(:no_movie_found) { Api.search_by_title("No movies exists") } + + it "doesn't raise an error" do + expect{:no_movie_found}.to_not raise_error(NoMethodError) + end end + end diff --git a/spec/fixtures/no_movie.json b/spec/fixtures/no_movie.json new file mode 100644 index 0000000..cc94537 --- /dev/null +++ b/spec/fixtures/no_movie.json @@ -0,0 +1 @@ +{"total":0,"movies":[],"links":{"self":"http://api.rottentomatoes.com/api/public/v1.0/movies.json?q=top+tomatoes&page_limit=1&page=1"},"link_template":"http://api.rottentomatoes.com/api/public/v1.0/movies.json?q={search-term}&page_limit={results-per-page}&page={page-number}"} \ No newline at end of file From be75f3f5c1a3e1c47e07e4fbcf6bc7a9ece3ebf9 Mon Sep 17 00:00:00 2001 From: Ralphos Date: Sun, 9 Dec 2012 20:24:19 +0800 Subject: [PATCH 03/11] displays a user's search history at the end of a session --- lib/user.rb | 12 ++++++++++++ movie_json.rb | 16 +++++++++++++++- spec/user_spec.rb | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 lib/user.rb create mode 100644 spec/user_spec.rb diff --git a/lib/user.rb b/lib/user.rb new file mode 100644 index 0000000..2ba4724 --- /dev/null +++ b/lib/user.rb @@ -0,0 +1,12 @@ +class User + attr_reader :name, :searches + + def initialize(name) + @name = name + @searches = [] + end + + def add_to_searches(movie) + @searches << movie + end +end diff --git a/movie_json.rb b/movie_json.rb index 15f8c41..42afefe 100644 --- a/movie_json.rb +++ b/movie_json.rb @@ -1,10 +1,17 @@ require_relative "lib/movie" require_relative "lib/api" +require_relative "lib/user" + +def start_movie_finder + puts "OH HAI. What's your name?" + @user = User.new(gets) +end def find_movie - puts "OH HAI. Search?" + puts "Search for a movie, #{@user.name}" movie_title = gets movie = Api.search_by_title(movie_title) + @user.add_to_searches(movie) if movie puts "Found: #{movie.title}. Score: #{movie.score}" else @@ -12,6 +19,12 @@ def find_movie end end +def show_movie_searches + puts "Your search history:" + @user.searches.each { |movie| puts movie.title } +end + +start_movie_finder find_movie while true do @@ -20,6 +33,7 @@ def find_movie if answer == "Y" find_movie else + show_movie_searches break end end diff --git a/spec/user_spec.rb b/spec/user_spec.rb new file mode 100644 index 0000000..610612f --- /dev/null +++ b/spec/user_spec.rb @@ -0,0 +1,34 @@ +require_relative "../lib/user" +require_relative "../lib/movie" +describe User do + + let(:user) { User.new("Ralph") } + + context "when user is initialized" do + + it "has a name" do + expect(user.name).to eq("Ralph") + end + + it "has a list of movie searches" do + expect(user.searches).to eq([]) + end + end + + context "when a searching" do + + it "records a user's search" do + movie = Movie.new(id: "1", title: "Some movie", year: 1985, score: 99) + user.add_to_searches(movie) + expect(user.searches.first).to eq(movie) + end + + it "records multiple searches" do + movie = Movie.new(id: "1", title: "Some movie", year: 1985, score: 99) + user.add_to_searches(movie) + another_movie = Movie.new(id: "2", title: "Another movie", year: 2000, score: 1) + user.add_to_searches(another_movie) + expect(user.searches).to eq([movie, another_movie]) + end + end +end From 2523c29b069df012baddf016ee4787ff195b7504 Mon Sep 17 00:00:00 2001 From: Ralphos Date: Sun, 9 Dec 2012 20:42:12 +0800 Subject: [PATCH 04/11] reworded the question and now displays an average movie rating --- lib/user.rb | 5 +++++ movie_json.rb | 6 +++++- spec/user_spec.rb | 8 ++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/user.rb b/lib/user.rb index 2ba4724..c03446e 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -9,4 +9,9 @@ def initialize(name) def add_to_searches(movie) @searches << movie end + + def rating + movie_scores = @searches.map { |movie| movie.score } + movie_scores.inject { |sum, el| sum + el } / @searches.size + end end diff --git a/movie_json.rb b/movie_json.rb index 42afefe..c42570c 100644 --- a/movie_json.rb +++ b/movie_json.rb @@ -8,18 +8,22 @@ def start_movie_finder end def find_movie - puts "Search for a movie, #{@user.name}" + puts "Add a movie you really like, #{@user.name}" movie_title = gets movie = Api.search_by_title(movie_title) @user.add_to_searches(movie) if movie puts "Found: #{movie.title}. Score: #{movie.score}" + puts "Your average movie rating is: #{@user.rating}" else puts "Movie not found." end end def show_movie_searches + puts "============================" + puts "Your final average movie rating score is: #{@user.rating}" + puts "============================" puts "Your search history:" @user.searches.each { |movie| puts movie.title } end diff --git a/spec/user_spec.rb b/spec/user_spec.rb index 610612f..ef4140d 100644 --- a/spec/user_spec.rb +++ b/spec/user_spec.rb @@ -13,6 +13,14 @@ it "has a list of movie searches" do expect(user.searches).to eq([]) end + + it "can calculate an average movie rating score" do + movie = Movie.new(id: "1", title: "Some movie", year: 1985, score: 80) + user.add_to_searches(movie) + another_movie = Movie.new(id: "2", title: "Another movie", year: 2000, score: 60) + user.add_to_searches(another_movie) + expect(user.rating).to eq(70) + end end context "when a searching" do From ba6ae0656bfe491883c0e95881df8379450cc37f Mon Sep 17 00:00:00 2001 From: Ralphos Date: Sun, 9 Dec 2012 20:48:13 +0800 Subject: [PATCH 05/11] Refactored user_spec --- spec/user_spec.rb | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/spec/user_spec.rb b/spec/user_spec.rb index ef4140d..6b5ffbc 100644 --- a/spec/user_spec.rb +++ b/spec/user_spec.rb @@ -3,6 +3,8 @@ describe User do let(:user) { User.new("Ralph") } + let(:movie) { Movie.new(id: "1", title: "Some movie", year: 1985, score: 80) } + let(:another_movie) { Movie.new(id: "2", title: "Another movie", year: 2000, score: 60) } context "when user is initialized" do @@ -15,9 +17,7 @@ end it "can calculate an average movie rating score" do - movie = Movie.new(id: "1", title: "Some movie", year: 1985, score: 80) user.add_to_searches(movie) - another_movie = Movie.new(id: "2", title: "Another movie", year: 2000, score: 60) user.add_to_searches(another_movie) expect(user.rating).to eq(70) end @@ -26,15 +26,12 @@ context "when a searching" do it "records a user's search" do - movie = Movie.new(id: "1", title: "Some movie", year: 1985, score: 99) user.add_to_searches(movie) expect(user.searches.first).to eq(movie) end it "records multiple searches" do - movie = Movie.new(id: "1", title: "Some movie", year: 1985, score: 99) user.add_to_searches(movie) - another_movie = Movie.new(id: "2", title: "Another movie", year: 2000, score: 1) user.add_to_searches(another_movie) expect(user.searches).to eq([movie, another_movie]) end From eee909ab41f704b5bc7e4835fad7942f07660199 Mon Sep 17 00:00:00 2001 From: Ralphos Date: Mon, 10 Dec 2012 00:09:21 +0800 Subject: [PATCH 06/11] displays the average year for the movies a user has liked --- lib/user.rb | 9 +++++++-- movie_json.rb | 1 + spec/user_spec.rb | 6 ++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/user.rb b/lib/user.rb index c03446e..340d01b 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -11,7 +11,12 @@ def add_to_searches(movie) end def rating - movie_scores = @searches.map { |movie| movie.score } - movie_scores.inject { |sum, el| sum + el } / @searches.size + movie_scores = searches.map { |movie| movie.score } + movie_scores.inject { |sum, el| sum + el } / searches.size + end + + def average_year + movie_years = searches.map { |movie| movie.year } + movie_years.inject { |sum, el| sum + el } / searches.size end end diff --git a/movie_json.rb b/movie_json.rb index c42570c..353b0b2 100644 --- a/movie_json.rb +++ b/movie_json.rb @@ -23,6 +23,7 @@ def find_movie def show_movie_searches puts "============================" puts "Your final average movie rating score is: #{@user.rating}" + puts "The average year for the movies you have liked is: #{@user.average_year}" puts "============================" puts "Your search history:" @user.searches.each { |movie| puts movie.title } diff --git a/spec/user_spec.rb b/spec/user_spec.rb index 6b5ffbc..68c45cf 100644 --- a/spec/user_spec.rb +++ b/spec/user_spec.rb @@ -21,6 +21,12 @@ user.add_to_searches(another_movie) expect(user.rating).to eq(70) end + + it "can calculate the average year of movies liked" do + user.add_to_searches(movie) + user.add_to_searches(another_movie) + expect(user.average_year).to eq(1992) + end end context "when a searching" do From 07b22d43f160205fbb392115cdb888fc0f0f7ed5 Mon Sep 17 00:00:00 2001 From: Ralphos Date: Mon, 10 Dec 2012 00:29:22 +0800 Subject: [PATCH 07/11] clarified spec wording --- spec/user_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/user_spec.rb b/spec/user_spec.rb index 68c45cf..0e33da6 100644 --- a/spec/user_spec.rb +++ b/spec/user_spec.rb @@ -12,7 +12,7 @@ expect(user.name).to eq("Ralph") end - it "has a list of movie searches" do + it "has an empty list of movie searches" do expect(user.searches).to eq([]) end From 498c80392b6ec037da19ed85412e0f2cd3430639 Mon Sep 17 00:00:00 2001 From: Ralphos Date: Mon, 10 Dec 2012 14:50:17 +0800 Subject: [PATCH 08/11] can now calculate user's average movie rating per year' --- lib/movie.rb | 3 +++ lib/user.rb | 28 ++++++++++++++++++++++++---- movie_json.rb | 22 +++++++++++----------- spec/movie_spec.rb | 2 ++ spec/user_spec.rb | 25 ++++++++++++++++++------- 5 files changed, 58 insertions(+), 22 deletions(-) diff --git a/lib/movie.rb b/lib/movie.rb index 167a23e..bd7edd1 100644 --- a/lib/movie.rb +++ b/lib/movie.rb @@ -1,10 +1,13 @@ class Movie attr_reader :id, :title, :year, :score + attr_accessor :liked_at + def initialize(hash={}) @id = hash.fetch(:id) @title = hash.fetch(:title) @year = hash.fetch(:year) @score = hash.fetch(:score) + @liked_at = Time.now.year end end diff --git a/lib/user.rb b/lib/user.rb index 340d01b..e575b76 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -11,12 +11,32 @@ def add_to_searches(movie) end def rating - movie_scores = searches.map { |movie| movie.score } - movie_scores.inject { |sum, el| sum + el } / searches.size + searches.inject { |sum, el| sum.score + el.score } / searches.size end def average_year - movie_years = searches.map { |movie| movie.year } - movie_years.inject { |sum, el| sum + el } / searches.size + searches.inject { |sum, el| sum.year + el.year } / searches.size + end + + def average_rating_per_year + years_and_scores = searches.map { |movie| { movie.liked_at.to_s => movie.score }} + merged = Hash.new(0) + years_and_scores.each do |h| + h.each do |y| + if merged[y[0]].is_a?(Array) + merged[y[0]] << y[1] + else + merged[y[0]] = [y[1]] + end + end + end + + output = {} + # Compute the average + merged.each do |m| + output[m[0]] = m[1].inject { |sum, el| sum + el } / m[1].length + end + output end end + diff --git a/movie_json.rb b/movie_json.rb index 353b0b2..6564acf 100644 --- a/movie_json.rb +++ b/movie_json.rb @@ -30,15 +30,15 @@ def show_movie_searches end start_movie_finder -find_movie +# find_movie -while true do - puts "Search Again (Y/N)" - answer = gets.upcase[0] - if answer == "Y" - find_movie - else - show_movie_searches - break - end -end +#while true do + #puts "Search Again (Y/N)" + #answer = gets.upcase[0] + #if answer == "Y" + #find_movie + #else + #show_movie_searches + #break + #end +#end diff --git a/spec/movie_spec.rb b/spec/movie_spec.rb index 088bd37..b12cc0a 100644 --- a/spec/movie_spec.rb +++ b/spec/movie_spec.rb @@ -3,10 +3,12 @@ it "should store the title, year, and score" do movie = Movie.new(id: "the-id", title: "the-title", year: 1998, score: 50) + movie.liked_at = 2012 movie.id.should eq("the-id") movie.title.should eq("the-title") movie.year.should eq(1998) movie.score.should eq(50) + movie.liked_at.should eq(2012) end end diff --git a/spec/user_spec.rb b/spec/user_spec.rb index 0e33da6..2e23036 100644 --- a/spec/user_spec.rb +++ b/spec/user_spec.rb @@ -3,8 +3,10 @@ describe User do let(:user) { User.new("Ralph") } - let(:movie) { Movie.new(id: "1", title: "Some movie", year: 1985, score: 80) } - let(:another_movie) { Movie.new(id: "2", title: "Another movie", year: 2000, score: 60) } + let(:movie) { Movie.new(id: "1", title: "First movie", year: 1985, score: 80) } + let(:second_movie) { Movie.new(id: "2", title: "Second movie", year: 2000, score: 60) } + let(:third_movie) { Movie.new(id: "3", title: "Third movie", year: 2000, score: 90) } + context "when user is initialized" do @@ -16,15 +18,24 @@ expect(user.searches).to eq([]) end + it "can calculate the average rating per year" do + third_movie.liked_at = 2010 + user.add_to_searches(movie) + user.add_to_searches(second_movie) + user.add_to_searches(third_movie) + average_ratings = user.average_rating_per_year + expect(average_ratings).to eq({"2012" => 70, "2010" => 90}) + end + it "can calculate an average movie rating score" do user.add_to_searches(movie) - user.add_to_searches(another_movie) - expect(user.rating).to eq(70) + user.add_to_searches(second_movie) + expect(user.rating).to eq 70 end it "can calculate the average year of movies liked" do user.add_to_searches(movie) - user.add_to_searches(another_movie) + user.add_to_searches(second_movie) expect(user.average_year).to eq(1992) end end @@ -38,8 +49,8 @@ it "records multiple searches" do user.add_to_searches(movie) - user.add_to_searches(another_movie) - expect(user.searches).to eq([movie, another_movie]) + user.add_to_searches(second_movie) + expect(user.searches).to eq([movie, second_movie]) end end end From 5a41527268f679dddc23ad34552c5c29169d751a Mon Sep 17 00:00:00 2001 From: Ralphos Date: Mon, 10 Dec 2012 16:07:30 +0800 Subject: [PATCH 09/11] Calculates slope and picking ability and refactored spec --- lib/user.rb | 27 +++++++++++++++++++++++++-- movie_json.rb | 28 ++++++++++++++++------------ spec/user_spec.rb | 43 +++++++++++++++++++++++++++---------------- 3 files changed, 68 insertions(+), 30 deletions(-) diff --git a/lib/user.rb b/lib/user.rb index e575b76..0213030 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -11,11 +11,13 @@ def add_to_searches(movie) end def rating - searches.inject { |sum, el| sum.score + el.score } / searches.size + movie_scores = searches.map { |movie| movie.score } + movie_scores.inject { |sum, el| sum + el } / searches.size end def average_year - searches.inject { |sum, el| sum.year + el.year } / searches.size + movie_years = searches.map { |movie| movie.year } + movie_years.inject { |sum, el| sum + el } / searches.size end def average_rating_per_year @@ -38,5 +40,26 @@ def average_rating_per_year end output end + + def calculate_slope + avg_ratings = average_rating_per_year + key_array = [] + avg_ratings.each_key { |key| key_array << key.to_i } + if key_array.size > 1 + first_year = key_array.min + last_year = key_array.max + (avg_ratings[last_year.to_s] - avg_ratings[first_year.to_s]) / (last_year - first_year) + else + "No slope" + end + end + + def picking_ability + if calculate_slope == "No slope" + "not yet determined." + else + calculate_slope > 0 ? "getting better." : "getting worse." + end + end end diff --git a/movie_json.rb b/movie_json.rb index 6564acf..2825091 100644 --- a/movie_json.rb +++ b/movie_json.rb @@ -22,23 +22,27 @@ def find_movie def show_movie_searches puts "============================" - puts "Your final average movie rating score is: #{@user.rating}" + puts "Your overall average movie rating score is: #{@user.rating}" puts "The average year for the movies you have liked is: #{@user.average_year}" puts "============================" + puts "Your average movie rating score per year is:" + @user.average_rating_per_year.each_pair { |k, v| puts "For #{k}, your average movie rating was #{v}!"} + puts "Your picking ability is #{@user.picking_ability}" + puts "============================" puts "Your search history:" @user.searches.each { |movie| puts movie.title } end start_movie_finder -# find_movie +find_movie -#while true do - #puts "Search Again (Y/N)" - #answer = gets.upcase[0] - #if answer == "Y" - #find_movie - #else - #show_movie_searches - #break - #end -#end +while true do + puts "Search Again (Y/N)" + answer = gets.upcase[0] + if answer == "Y" + find_movie + else + show_movie_searches + break + end +end diff --git a/spec/user_spec.rb b/spec/user_spec.rb index 2e23036..73293de 100644 --- a/spec/user_spec.rb +++ b/spec/user_spec.rb @@ -19,38 +19,49 @@ end it "can calculate the average rating per year" do - third_movie.liked_at = 2010 - user.add_to_searches(movie) - user.add_to_searches(second_movie) - user.add_to_searches(third_movie) + add_movies_to_searches(user) average_ratings = user.average_rating_per_year expect(average_ratings).to eq({"2012" => 70, "2010" => 90}) end - it "can calculate an average movie rating score" do - user.add_to_searches(movie) - user.add_to_searches(second_movie) - expect(user.rating).to eq 70 + it "can calculate a total average movie rating score" do + add_movies_to_searches(user) + expect(user.rating).to eq 76 end it "can calculate the average year of movies liked" do - user.add_to_searches(movie) - user.add_to_searches(second_movie) - expect(user.average_year).to eq(1992) + add_movies_to_searches(user) + expect(user.average_year).to eq 1995 + end + + it "can calculate the slope of yearly average ratings" do + add_movies_to_searches(user) + expect(user.calculate_slope).to eq -10 + end + + it "can tell if user is getting worse at picking movies" do + add_movies_to_searches(user) + expect(user.picking_ability).to eq("getting worse.") end end - context "when a searching" do + context "when searching" do it "records a user's search" do - user.add_to_searches(movie) + add_movies_to_searches(user) expect(user.searches.first).to eq(movie) end it "records multiple searches" do - user.add_to_searches(movie) - user.add_to_searches(second_movie) - expect(user.searches).to eq([movie, second_movie]) + add_movies_to_searches(user) + expect(user.searches).to eq([movie, second_movie, third_movie]) end end + + def add_movies_to_searches(user) + third_movie.liked_at = 2010 + user.add_to_searches(movie) + user.add_to_searches(second_movie) + user.add_to_searches(third_movie) + end end From 10f261db0342395b9780d54e90f478fa9f07dada Mon Sep 17 00:00:00 2001 From: Ralphos Date: Mon, 10 Dec 2012 16:27:41 +0800 Subject: [PATCH 10/11] Added a more explicit if/else statement if no movie is returned --- lib/api.rb | 6 +++--- lib/user.rb | 2 ++ movie_json.rb | 5 +---- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/lib/api.rb b/lib/api.rb index a30a710..9d1ccf8 100644 --- a/lib/api.rb +++ b/lib/api.rb @@ -9,14 +9,14 @@ class Api def self.search_by_title(title) url = "http://api.rottentomatoes.com/api/public/v1.0/movies.json?apikey=#{APIKEY}&q=#{URI.encode(title)}&page_limit=1" struct = OpenStruct.new(get_url_as_json(url).fetch("movies").first) - begin + if !struct.id.nil? Movie.new(id: struct.id.to_i, title: struct.title, year: struct.year, score: struct.ratings["critics_score"] ) - rescue - nil + else + puts "No Movies returned" end end diff --git a/lib/user.rb b/lib/user.rb index 0213030..b15c8f3 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -11,11 +11,13 @@ def add_to_searches(movie) end def rating + searches.delete_if { |m| m == nil } # since we are returning a string when no movie is found, I'm removing any nil values movie_scores = searches.map { |movie| movie.score } movie_scores.inject { |sum, el| sum + el } / searches.size end def average_year + searches.delete_if { |m| m == nil } movie_years = searches.map { |movie| movie.year } movie_years.inject { |sum, el| sum + el } / searches.size end diff --git a/movie_json.rb b/movie_json.rb index 2825091..1b0b5a2 100644 --- a/movie_json.rb +++ b/movie_json.rb @@ -12,11 +12,8 @@ def find_movie movie_title = gets movie = Api.search_by_title(movie_title) @user.add_to_searches(movie) - if movie + if !movie.nil? puts "Found: #{movie.title}. Score: #{movie.score}" - puts "Your average movie rating is: #{@user.rating}" - else - puts "Movie not found." end end From 5f254b3514c9108133459db862e7013855ca9633 Mon Sep 17 00:00:00 2001 From: Ralphos Date: Mon, 10 Dec 2012 16:28:35 +0800 Subject: [PATCH 11/11] Removed unneccesary line --- movie_json.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/movie_json.rb b/movie_json.rb index 1b0b5a2..f9d4ae2 100644 --- a/movie_json.rb +++ b/movie_json.rb @@ -22,7 +22,6 @@ def show_movie_searches puts "Your overall average movie rating score is: #{@user.rating}" puts "The average year for the movies you have liked is: #{@user.average_year}" puts "============================" - puts "Your average movie rating score per year is:" @user.average_rating_per_year.each_pair { |k, v| puts "For #{k}, your average movie rating was #{v}!"} puts "Your picking ability is #{@user.picking_ability}" puts "============================"