From a1d5c4b404721353ba69f29bc5da4cb99c5ac0d5 Mon Sep 17 00:00:00 2001 From: "J. Michael Kasiewicz" Date: Sat, 24 Nov 2012 10:43:14 -0500 Subject: [PATCH 1/5] Add error handling when searching for non-existant movies and keep a running average of movie ratings. --- lib/api.rb | 1 + movie_json.rb | 20 ++++++++++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/api.rb b/lib/api.rb index a8d499c..1a53250 100644 --- a/lib/api.rb +++ b/lib/api.rb @@ -9,6 +9,7 @@ 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) + return false if struct.title == nil Movie.new(id: struct.id.to_i, title: struct.title, year: struct.year, diff --git a/movie_json.rb b/movie_json.rb index d8a91d7..d6c6959 100644 --- a/movie_json.rb +++ b/movie_json.rb @@ -2,16 +2,28 @@ require_relative "lib/api" def find_movie - puts "OH HAI. Search?" + @movies = [] if @movies == nil + puts 'Add a movie you really like:' movie_title = gets - movie = Api.search_by_title(movie_title) - puts "Found: #{movie.title}. Score: #{movie.score}" + if !(current_movie = Api.search_by_title(movie_title)) + puts 'Oops. Something went wrong or title not found.' + else + @movies << current_movie + puts "Found: #{@movies.last.title}. Score: #{@movies.last.score}" + puts "Average movie score: #{get_average_rating(@movies)}" + end +end + +def get_average_rating(movies) + all_ratings = 0 + movies.each { |rating| all_ratings += rating.score } + all_ratings / movies.length.to_f end find_movie while true do - puts "Search Again (Y/N)" + puts 'Search Again (Y/N)' answer = gets.upcase[0] if answer == "Y" find_movie From 2f03808f549573f9276d5f4971764a22dd53c8ec Mon Sep 17 00:00:00 2001 From: "J. Michael Kasiewicz" Date: Sat, 24 Nov 2012 12:32:40 -0500 Subject: [PATCH 2/5] Show average year of movies and determine user temperament based on searched movie stats --- movie_json.rb | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/movie_json.rb b/movie_json.rb index d6c6959..9e72054 100644 --- a/movie_json.rb +++ b/movie_json.rb @@ -2,24 +2,43 @@ require_relative "lib/api" def find_movie - @movies = [] if @movies == nil puts 'Add a movie you really like:' movie_title = gets if !(current_movie = Api.search_by_title(movie_title)) puts 'Oops. Something went wrong or title not found.' else @movies << current_movie - puts "Found: #{@movies.last.title}. Score: #{@movies.last.score}" - puts "Average movie score: #{get_average_rating(@movies)}" + puts "Found: #{@movies.last.title}. Score: #{@movies.last.score} Year: #{@movies.last.year}" + if @movies.length > 1 + puts "Average movie score: #{get_average(@movies, "score", "float")}" + puts "Average movie year: #{get_average(@movies, "year", "integer")}" + puts get_satisfaction(@movies) + end end end -def get_average_rating(movies) - all_ratings = 0 - movies.each { |rating| all_ratings += rating.score } - all_ratings / movies.length.to_f +def get_average(movies, attribute, type) + attribute_sum = 0 + movies.each { |rating| attribute_sum += rating.send(attribute) } + if type == "float" + return attribute_sum / movies.length.to_f + else + return attribute_sum / movies.length + end end +def get_satisfaction(movies) + min_movie = movies.min_by(&:year) + max_movie = movies.max_by(&:year) + if (max_movie.score - min_movie.score).to_f / (max_movie.year - min_movie.year).to_f > 0 + return "getting happier" + else + return "getting madder" + end +end + +@movies = [] + find_movie while true do From 409a9afad60127b5c1ff7b87bff19b0e06b2c861 Mon Sep 17 00:00:00 2001 From: "J. Michael Kasiewicz" Date: Sat, 24 Nov 2012 12:40:56 -0500 Subject: [PATCH 3/5] Add message if best and worst rating score is the same. --- movie_json.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/movie_json.rb b/movie_json.rb index 9e72054..527b959 100644 --- a/movie_json.rb +++ b/movie_json.rb @@ -30,7 +30,10 @@ def get_average(movies, attribute, type) def get_satisfaction(movies) min_movie = movies.min_by(&:year) max_movie = movies.max_by(&:year) - if (max_movie.score - min_movie.score).to_f / (max_movie.year - min_movie.year).to_f > 0 + user_slope = (max_movie.score - min_movie.score).to_f / (max_movie.year - min_movie.year).to_f + if user_slope == 0 + return "neutral" + elsif user_slope > 0 return "getting happier" else return "getting madder" From f8eed78cc9b0938ce1376c072707701954d886fe Mon Sep 17 00:00:00 2001 From: "J. Michael Kasiewicz" Date: Mon, 26 Nov 2012 12:51:09 -0500 Subject: [PATCH 4/5] Find average of all movies in max and min years and use results to find overall satisfaction --- movie_json.rb | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/movie_json.rb b/movie_json.rb index 527b959..911fae7 100644 --- a/movie_json.rb +++ b/movie_json.rb @@ -30,7 +30,20 @@ def get_average(movies, attribute, type) def get_satisfaction(movies) min_movie = movies.min_by(&:year) max_movie = movies.max_by(&:year) - user_slope = (max_movie.score - min_movie.score).to_f / (max_movie.year - min_movie.year).to_f + return "neutral" if min_movie == max_movie + max_movie_count, max_movie_score_total = 0, 0 + min_movie_count, min_movie_score_total = 0, 0 + movies.each do |movie| + if movie.year == max_movie.year + max_movie_count += 1 + max_movie_score_total += movie.score + end + if movie.year == min_movie.year + min_movie_count += 1 + min_movie_score_total += movie.score + end + end + user_slope = ((max_movie_score_total/max_movie_count).to_f - (min_movie_score_total/min_movie_count).to_f) / (max_movie.year - min_movie.year).to_f if user_slope == 0 return "neutral" elsif user_slope > 0 From 83e7d9a6a58eeeeec1e59b0de3e49f2fcc073cef Mon Sep 17 00:00:00 2001 From: "J. Michael Kasiewicz" Date: Tue, 4 Dec 2012 13:00:07 -0500 Subject: [PATCH 5/5] Improve exception handling --- lib/api.rb | 5 ++++- movie_json.rb | 19 +++++++++---------- spec/api_spec.rb | 5 +++++ 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/lib/api.rb b/lib/api.rb index 1a53250..1879377 100644 --- a/lib/api.rb +++ b/lib/api.rb @@ -2,14 +2,17 @@ require "json" require "ostruct" require_relative "./movie" + class Api + class NilMovie < StandardError; end + 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) - return false if struct.title == nil + raise NilMovie if struct.title == nil Movie.new(id: struct.id.to_i, title: struct.title, year: struct.year, diff --git a/movie_json.rb b/movie_json.rb index 911fae7..9880d2c 100644 --- a/movie_json.rb +++ b/movie_json.rb @@ -4,17 +4,16 @@ def find_movie puts 'Add a movie you really like:' movie_title = gets - if !(current_movie = Api.search_by_title(movie_title)) - puts 'Oops. Something went wrong or title not found.' - else - @movies << current_movie - puts "Found: #{@movies.last.title}. Score: #{@movies.last.score} Year: #{@movies.last.year}" - if @movies.length > 1 - puts "Average movie score: #{get_average(@movies, "score", "float")}" - puts "Average movie year: #{get_average(@movies, "year", "integer")}" - puts get_satisfaction(@movies) - end + current_movie = Api.search_by_title(movie_title) + @movies << current_movie + puts "Found: #{@movies.last.title}. Score: #{@movies.last.score} Year: #{@movies.last.year}" + if @movies.length > 1 + puts "Average movie score: #{get_average(@movies, "score", "float")}" + puts "Average movie year: #{get_average(@movies, "year", "integer")}" + puts get_satisfaction(@movies) end + rescue Api::NilMovie + puts 'Oops. Something went wrong or title not found.' end def get_average(movies, attribute, type) diff --git a/spec/api_spec.rb b/spec/api_spec.rb index 9014106..46729d9 100644 --- a/spec/api_spec.rb +++ b/spec/api_spec.rb @@ -24,4 +24,9 @@ it "should return the year" do movie.year.should eq(1994) end + + it "should not raise error if title not found" do + expect{Api.search_by_title("NOTHINGFOUNDHERE")}.to_not raise_error + end + end