From 155a905f5160383f6c0be8b448bc975341371e81 Mon Sep 17 00:00:00 2001 From: Meshed Date: Thu, 22 Nov 2012 20:52:41 -0500 Subject: [PATCH 01/10] Prevent code from blowing up for search results from unfound movies --- lib/api.rb | 14 ++++++++------ spec/api_spec.rb | 10 +++++++++- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/lib/api.rb b/lib/api.rb index a8d499c..ccaf33b 100644 --- a/lib/api.rb +++ b/lib/api.rb @@ -9,12 +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) - Movie.new(id: struct.id.to_i, - title: struct.title, - year: struct.year, - score: struct.ratings["critics_score"] - ) - end + if struct.ratings.nil? == false then + Movie.new(id: struct.id.to_i, + title: struct.title, + year: struct.year, + score: struct.ratings["critics_score"] + ) + end + end def self.get_url_as_json(url) diff --git a/spec/api_spec.rb b/spec/api_spec.rb index 9014106..cc5d3d9 100644 --- a/spec/api_spec.rb +++ b/spec/api_spec.rb @@ -6,7 +6,7 @@ let(:movie) { Api.search_by_title("Forrest Gump") } before do - Api.stub(:get_url_as_json) { JSON.parse(File.read("spec/fixtures/forrest.json")) } + #Api.stub(:get_url_as_json) { JSON.parse(File.read("spec/fixtures/forrest.json")) } end it "should search for movies" do @@ -24,4 +24,12 @@ it "should return the year" do movie.year.should eq(1994) end + + describe "Movie Not Found" do + it "title should be 'Movie Not Found'" do + expect { + Api.search_by_title("INVALIDMOVIENAME") + }.to_not raise_error + end + end end From 7bd8276c4d6f29031c0d1c4dbb66d5c7985d37d5 Mon Sep 17 00:00:00 2001 From: Meshed Date: Tue, 27 Nov 2012 18:23:38 -0500 Subject: [PATCH 02/10] Handle blank titles and invalid movies --- lib/api.rb | 32 ++++++++++++++++++++-------- spec/api_spec.rb | 55 +++++++++++++++++++++++++++--------------------- 2 files changed, 54 insertions(+), 33 deletions(-) diff --git a/lib/api.rb b/lib/api.rb index ccaf33b..50d58c2 100644 --- a/lib/api.rb +++ b/lib/api.rb @@ -7,15 +7,29 @@ class Api APIKEY="4t6456xa33z8qhcqyuqgnkjh" 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) - if struct.ratings.nil? == false then - Movie.new(id: struct.id.to_i, - title: struct.title, - year: struct.year, - score: struct.ratings["critics_score"] - ) - end + if title != "" then + 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) + if struct.ratings.nil? == false then + Movie.new(id: struct.id.to_i, + title: struct.title, + year: struct.year, + score: struct.ratings["critics_score"] + ) + else + Movie.new(id: 0, + title: "NOTHINGFOUNDHERE", + year: 0, + score: 0 + ) + end + else + Movie.new(id: 0, + title: "NOTHINGFOUNDHERE", + year: 0, + score: 0 + ) + end end diff --git a/spec/api_spec.rb b/spec/api_spec.rb index cc5d3d9..e940845 100644 --- a/spec/api_spec.rb +++ b/spec/api_spec.rb @@ -2,34 +2,41 @@ require "ostruct" describe Api do - - let(:movie) { Api.search_by_title("Forrest Gump") } - - before do - #Api.stub(:get_url_as_json) { JSON.parse(File.read("spec/fixtures/forrest.json")) } + context "Missing Movie" do + describe "Empty Search" do + it "should not throw an error" do + expect { + Api.search_by_title("NOTHINGFOUNDHERE") + }.to_not raise_error + end + it "should have the title 'NOTHINGFOUNDHERE'" do + movie = Api.search_by_title("") + movie.title.should eq("NOTHINGFOUNDHERE") + end + end end - it "should search for movies" do - movie.title.should eq("Forrest Gump") - end + context "Valid Movie" do + let(:movie) { Api.search_by_title("Forrest Gump") } - it "should return the score" do - movie.score.should eq(71) - end + before do + #Api.stub(:get_url_as_json) { JSON.parse(File.read("spec/fixtures/forrest.json")) } + end - it "should return the id" do - movie.id.should eq(10036) - end + it "should search for movies" do + movie.title.should eq("Forrest Gump") + end - it "should return the year" do - movie.year.should eq(1994) - end + it "should return the score" do + movie.score.should eq(71) + end - describe "Movie Not Found" do - it "title should be 'Movie Not Found'" do - expect { - Api.search_by_title("INVALIDMOVIENAME") - }.to_not raise_error - end - end + it "should return the id" do + movie.id.should eq(10036) + end + + it "should return the year" do + movie.year.should eq(1994) + end + end end From 81a2fc5af32bf7abbb1c47dcb24bd2484b226750 Mon Sep 17 00:00:00 2001 From: Meshed Date: Tue, 27 Nov 2012 21:45:30 -0500 Subject: [PATCH 03/10] Add movie search history tracking --- lib/api.rb | 17 +++++++++++++---- movie_json.rb | 2 +- spec/api_spec.rb | 14 +++++++++++++- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/lib/api.rb b/lib/api.rb index 50d58c2..b882254 100644 --- a/lib/api.rb +++ b/lib/api.rb @@ -3,6 +3,7 @@ require "ostruct" require_relative "./movie" class Api + @@search_history = [] APIKEY="4t6456xa33z8qhcqyuqgnkjh" @@ -11,11 +12,13 @@ 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) if struct.ratings.nil? == false then - Movie.new(id: struct.id.to_i, + movie = Movie.new(id: struct.id.to_i, title: struct.title, year: struct.year, score: struct.ratings["critics_score"] ) + @@search_history << movie + return movie else Movie.new(id: 0, title: "NOTHINGFOUNDHERE", @@ -29,12 +32,18 @@ def self.search_by_title(title) year: 0, score: 0 ) - end - end - + end + end def self.get_url_as_json(url) JSON.parse(open(url).read) end + def self.get_search_history() + @@search_history + end + + def self.reset_search_history() + @@search_history = [] + end end diff --git a/movie_json.rb b/movie_json.rb index d8a91d7..89cccf3 100644 --- a/movie_json.rb +++ b/movie_json.rb @@ -2,7 +2,7 @@ require_relative "lib/api" 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}" diff --git a/spec/api_spec.rb b/spec/api_spec.rb index e940845..3e20dfe 100644 --- a/spec/api_spec.rb +++ b/spec/api_spec.rb @@ -20,7 +20,7 @@ let(:movie) { Api.search_by_title("Forrest Gump") } before do - #Api.stub(:get_url_as_json) { JSON.parse(File.read("spec/fixtures/forrest.json")) } + Api.stub(:get_url_as_json) { JSON.parse(File.read("spec/fixtures/forrest.json")) } end it "should search for movies" do @@ -38,5 +38,17 @@ it "should return the year" do movie.year.should eq(1994) end + + it "should be able to reset the search history" do + Api.reset_search_history + Api.get_search_history.count.should eq(0) end + end + + context "Search History" do + it "should store latest search history" do + movie = Api.search_by_title("Forrest Gump") + Api.get_search_history.count.should eq(1) + end + end end From d2f4b1494d491be3131c51dad6c4150836fe385a Mon Sep 17 00:00:00 2001 From: Meshed Date: Tue, 27 Nov 2012 22:48:53 -0500 Subject: [PATCH 04/10] Add average movie score --- movie_json.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/movie_json.rb b/movie_json.rb index 89cccf3..48074dc 100644 --- a/movie_json.rb +++ b/movie_json.rb @@ -1,13 +1,17 @@ require_relative "lib/movie" require_relative "lib/api" +require_relative "lib/search_history" def find_movie puts "Add a movie you really like" movie_title = gets movie = Api.search_by_title(movie_title) + @search_history.add_search(movie) puts "Found: #{movie.title}. Score: #{movie.score}" + puts "Average movie score: #{@search_history.average_rating}." end +@search_history = Search_History.new find_movie while true do From 0044fd22ae07956c2659b453fe183c7751d6f9b6 Mon Sep 17 00:00:00 2001 From: Meshed Date: Tue, 27 Nov 2012 23:16:08 -0500 Subject: [PATCH 05/10] Add average year for scores --- movie_json.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/movie_json.rb b/movie_json.rb index 48074dc..ac33f76 100644 --- a/movie_json.rb +++ b/movie_json.rb @@ -9,6 +9,7 @@ def find_movie @search_history.add_search(movie) puts "Found: #{movie.title}. Score: #{movie.score}" puts "Average movie score: #{@search_history.average_rating}." + puts "Average movie year: #{@search_history.average_year_for_ratings}." end @search_history = Search_History.new From 30f5ef9f7efd12a651cf914e127917d3c6495300 Mon Sep 17 00:00:00 2001 From: Meshed Date: Wed, 28 Nov 2012 00:19:47 -0500 Subject: [PATCH 06/10] Determine ratings trend and provide feedback --- movie_json.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/movie_json.rb b/movie_json.rb index ac33f76..6644056 100644 --- a/movie_json.rb +++ b/movie_json.rb @@ -10,6 +10,11 @@ def find_movie puts "Found: #{movie.title}. Score: #{movie.score}" puts "Average movie score: #{@search_history.average_rating}." puts "Average movie year: #{@search_history.average_year_for_ratings}." + if @search_history.ratings_trend > 0 then + puts "You are getting happier" + elsif @search_history.ratings_trend < 0 then + puts "You are getting madder" + end end @search_history = Search_History.new From d84240c81de490065d9ba7563bd298ec4efc35e6 Mon Sep 17 00:00:00 2001 From: Meshed Date: Wed, 28 Nov 2012 00:28:58 -0500 Subject: [PATCH 07/10] Add missing search_history class and spec files --- lib/search_history.rb | 68 +++++++++++++++++++++++++++++++++++++ spec/search_history_spec.rb | 68 +++++++++++++++++++++++++++++++++++++ 2 files changed, 136 insertions(+) create mode 100644 lib/search_history.rb create mode 100644 spec/search_history_spec.rb diff --git a/lib/search_history.rb b/lib/search_history.rb new file mode 100644 index 0000000..487be42 --- /dev/null +++ b/lib/search_history.rb @@ -0,0 +1,68 @@ +require_relative "./movie" + +class Search_History + attr_reader :movies + + def initialize() + @movies = [] + end + + def add_search(movie) + @movies << movie + end + + def average_rating() + average = 0 + @movies.each do |movie| + average += movie.score + end + + return (average/@movies.count) + end + + def average_year_for_ratings() + year = 0 + @movies.each do |movie| + year += movie.year + end + + return (year/@movies.count).ceil + end + + def average_rating_for_year(year) + @rating = 0 + movie_count = 0 + @movies.each do |movie| + if movie.year == year then + @rating += movie.score + movie_count += 1 + end + end + + return (@rating/movie_count) + end + + def average_rating_for_max_year + average_rating_for_year(max_year) + end + + def average_rating_for_min_year + average_rating_for_year(min_year) + end + + def max_year + @movies.max_by(&:year).year + end + + def min_year + @movies.min_by(&:year).year + end + + def ratings_trend + if @movies.count > 1 then + (average_rating_for_max_year - average_rating_for_min_year) / (max_year - min_year) + else + 0 + end + end +end diff --git a/spec/search_history_spec.rb b/spec/search_history_spec.rb new file mode 100644 index 0000000..5006d8c --- /dev/null +++ b/spec/search_history_spec.rb @@ -0,0 +1,68 @@ +require_relative "../lib/search_history" +describe Search_History do + it "should store a search history" do + search_history = Search_History.new + search_history.add_search(Api.search_by_title("Forrest Gump")) + search_history.movies.count.should eq(1) + end + + it "should store latest seach history" do + search_history = Search_History.new + search_history.add_search(Api.search_by_title("Forrest Gump")) + search_history.movies.last.title.should eq("Forrest Gump") + end + + it "should get average rating for search history" do + search_history = Search_History.new + search_history.add_search(Api.search_by_title("Forrest Gump")) + search_history.add_search(Api.search_by_title("Joe Dirt")) + search_history.movies.count.should eq(2) + search_history.average_rating.should eq(41) + end + + it "should have the average year for the ratings" do + search_history = Search_History.new + search_history.add_search(Api.search_by_title("Forrest Gump")) + search_history.add_search(Api.search_by_title("Joe Dirt")) + search_history.average_year_for_ratings.should eq(1997) + end + + it "should have the average rating for the max year" do + search_history = Search_History.new + search_history.add_search(Api.search_by_title("Forrest Gump")) + search_history.add_search(Api.search_by_title("Joe Dirt")) + search_history.average_rating_for_max_year.should eq(11) + end + + it "should have the average rating for the min year" do + search_history = Search_History.new + search_history.add_search(Api.search_by_title("Forrest Gump")) + search_history.add_search(Api.search_by_title("Joe Dirt")) + search_history.add_search(Api.search_by_title("Ace Ventura")) + search_history.average_rating_for_min_year.should eq(58) + end + + it "should have the max year" do + search_history = Search_History.new + search_history.add_search(Api.search_by_title("Forrest Gump")) + search_history.add_search(Api.search_by_title("Joe Dirt")) + search_history.add_search(Api.search_by_title("Ace Ventura")) + search_history.max_year.should eq(2001) + end + + it "should have the min year" do + search_history = Search_History.new + search_history.add_search(Api.search_by_title("Forrest Gump")) + search_history.add_search(Api.search_by_title("Joe Dirt")) + search_history.add_search(Api.search_by_title("Ace Ventura")) + search_history.min_year.should eq(1994) + end + + it "should have the ratings trend" do + search_history = Search_History.new + search_history.add_search(Api.search_by_title("Forrest Gump")) + search_history.add_search(Api.search_by_title("Joe Dirt")) + search_history.add_search(Api.search_by_title("Ace Ventura")) + search_history.ratings_trend.should eq(-7) + end +end From a56c73876ab11704889620284469823c86c671bd Mon Sep 17 00:00:00 2001 From: Meshed Date: Wed, 28 Nov 2012 15:17:23 -0500 Subject: [PATCH 08/10] Clean up search history code --- lib/api.rb | 11 ----------- spec/api_spec.rb | 12 ------------ 2 files changed, 23 deletions(-) diff --git a/lib/api.rb b/lib/api.rb index b882254..37f2d4d 100644 --- a/lib/api.rb +++ b/lib/api.rb @@ -3,8 +3,6 @@ require "ostruct" require_relative "./movie" class Api - @@search_history = [] - APIKEY="4t6456xa33z8qhcqyuqgnkjh" def self.search_by_title(title) @@ -17,7 +15,6 @@ def self.search_by_title(title) year: struct.year, score: struct.ratings["critics_score"] ) - @@search_history << movie return movie else Movie.new(id: 0, @@ -38,12 +35,4 @@ def self.search_by_title(title) def self.get_url_as_json(url) JSON.parse(open(url).read) end - - def self.get_search_history() - @@search_history - end - - def self.reset_search_history() - @@search_history = [] - end end diff --git a/spec/api_spec.rb b/spec/api_spec.rb index 3e20dfe..868e0b7 100644 --- a/spec/api_spec.rb +++ b/spec/api_spec.rb @@ -38,17 +38,5 @@ it "should return the year" do movie.year.should eq(1994) end - - it "should be able to reset the search history" do - Api.reset_search_history - Api.get_search_history.count.should eq(0) - end - end - - context "Search History" do - it "should store latest search history" do - movie = Api.search_by_title("Forrest Gump") - Api.get_search_history.count.should eq(1) - end end end From 5bba2c9c3bd35150920ba5180e4f50de76c35571 Mon Sep 17 00:00:00 2001 From: Meshed Date: Wed, 28 Nov 2012 21:44:49 -0500 Subject: [PATCH 09/10] Prevent adding an invalid movie to the search history --- lib/search_history.rb | 2 +- spec/search_history_spec.rb | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/search_history.rb b/lib/search_history.rb index 487be42..b2c1372 100644 --- a/lib/search_history.rb +++ b/lib/search_history.rb @@ -8,7 +8,7 @@ def initialize() end def add_search(movie) - @movies << movie + @movies << movie if movie.title != "NOTHINGFOUNDHERE" end def average_rating() diff --git a/spec/search_history_spec.rb b/spec/search_history_spec.rb index 5006d8c..59dcb62 100644 --- a/spec/search_history_spec.rb +++ b/spec/search_history_spec.rb @@ -65,4 +65,11 @@ search_history.add_search(Api.search_by_title("Ace Ventura")) search_history.ratings_trend.should eq(-7) end + + it "should not add invalid movies" do + search_history = Search_History.new + search_history.movies.count.should eq(0) + search_history.add_search(Api.search_by_title("Billy Maddison")) + search_history.movies.count.should eq(0) + end end From 1f6416a32828cd79109748728b5a5598bca50276 Mon Sep 17 00:00:00 2001 From: Meshed Date: Wed, 28 Nov 2012 22:02:19 -0500 Subject: [PATCH 10/10] Use nil movie objects --- lib/api.rb | 37 +++++++++++++++---------------------- 1 file changed, 15 insertions(+), 22 deletions(-) diff --git a/lib/api.rb b/lib/api.rb index 37f2d4d..3b4d595 100644 --- a/lib/api.rb +++ b/lib/api.rb @@ -5,30 +5,23 @@ class Api APIKEY="4t6456xa33z8qhcqyuqgnkjh" + def self.nil_movie + Movie.new(id: 0, title: "NOTHINGFOUNDHERE", year: 0, score: 0) + end + def self.search_by_title(title) - if title != "" then - 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) - if struct.ratings.nil? == false then - movie = Movie.new(id: struct.id.to_i, - title: struct.title, - year: struct.year, - score: struct.ratings["critics_score"] - ) - return movie - else - Movie.new(id: 0, - title: "NOTHINGFOUNDHERE", - year: 0, - score: 0 - ) - end + return nil_movie if 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) + if struct.ratings.nil? == false then + movie = Movie.new(id: struct.id.to_i, + title: struct.title, + year: struct.year, + score: struct.ratings["critics_score"] + ) + return movie else - Movie.new(id: 0, - title: "NOTHINGFOUNDHERE", - year: 0, - score: 0 - ) + return nil_movie end end