From 027b52f9ef9804a0772a120a762ad70735eb9d59 Mon Sep 17 00:00:00 2001 From: Anna Vester Date: Wed, 28 Nov 2012 22:29:43 -0600 Subject: [PATCH 1/4] Panda Level --- lib/api.rb | 3 +++ movie_json.rb | 8 +++++++- spec/api_spec.rb | 4 ++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/api.rb b/lib/api.rb index a8d499c..02f3bf4 100644 --- a/lib/api.rb +++ b/lib/api.rb @@ -9,6 +9,9 @@ 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 "Not Found" if struct.id.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..cf8cbd4 100644 --- a/movie_json.rb +++ b/movie_json.rb @@ -5,7 +5,13 @@ 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 == "Not Found" + puts "Not Found" + else + puts "Found: #{movie.title}. Score: #{movie.score}" + end + end find_movie diff --git a/spec/api_spec.rb b/spec/api_spec.rb index 9014106..e2d9fbf 100644 --- a/spec/api_spec.rb +++ b/spec/api_spec.rb @@ -24,4 +24,8 @@ it "should return the year" do movie.year.should eq(1994) end + + it "should not blow up" do + expect { Api.search_by_title("NOTHINGFOUNDHERE") }.to_not raise_error + end end From 22239b4ece92b98bae5c50f5e131cae0a8d67409 Mon Sep 17 00:00:00 2001 From: Anna Vester Date: Thu, 29 Nov 2012 21:18:09 -0600 Subject: [PATCH 2/4] udpated error handling --- lib/api.rb | 18 ++++++++++-------- movie_json.rb | 7 +------ 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/lib/api.rb b/lib/api.rb index 02f3bf4..ea93695 100644 --- a/lib/api.rb +++ b/lib/api.rb @@ -8,18 +8,20 @@ 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 "Not Found" if struct.id.nil? + begin + 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"] - ) + Movie.new(id: struct.id.to_i, + title: struct.title, + year: struct.year, + score: struct.ratings["critics_score"] + ) + rescue NoMethodError + puts "Movie Not Found" + end end - def self.get_url_as_json(url) JSON.parse(open(url).read) end diff --git a/movie_json.rb b/movie_json.rb index cf8cbd4..e47072d 100644 --- a/movie_json.rb +++ b/movie_json.rb @@ -6,12 +6,7 @@ def find_movie movie_title = gets movie = Api.search_by_title(movie_title) - if movie == "Not Found" - puts "Not Found" - else - puts "Found: #{movie.title}. Score: #{movie.score}" - end - + puts "Found: #{movie.title}. Score: #{movie.score}" if !movie.nil? end find_movie From 283f70b1f01c53f94d12bedcc958753e72e9d478 Mon Sep 17 00:00:00 2001 From: Anna Vester Date: Thu, 29 Nov 2012 22:17:43 -0600 Subject: [PATCH 3/4] Tiger Level --- movie_json.rb | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/movie_json.rb b/movie_json.rb index e47072d..899a0f2 100644 --- a/movie_json.rb +++ b/movie_json.rb @@ -1,12 +1,24 @@ require_relative "lib/movie" require_relative "lib/api" +def get_average(movies) + averages = [] + + movies.each do |m| + averages << m.score if !m.nil? + end + + averages.inject{ |sum, el| sum + el }.to_f / averages.size + +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) + @movies = [] if @movies.nil? + @movies << Api.search_by_title(movie_title) - puts "Found: #{movie.title}. Score: #{movie.score}" if !movie.nil? + puts "Found: #{@movies.last.title}. Score: #{@movies.last.score}. Avg. Score: #{get_average(@movies)}" if !@movies.last.nil? end find_movie From 8dff08ddb18163ec85da5aa553325cf2174ac3ee Mon Sep 17 00:00:00 2001 From: Anna Vester Date: Fri, 30 Nov 2012 21:41:10 -0600 Subject: [PATCH 4/4] Updated/improved with custom error. --- lib/api.rb | 16 ++++++++++++---- movie_json.rb | 11 ++++++++--- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/lib/api.rb b/lib/api.rb index ea93695..93c9e44 100644 --- a/lib/api.rb +++ b/lib/api.rb @@ -2,23 +2,31 @@ require "json" require "ostruct" require_relative "./movie" + class Api + class MovieNotFoundError < 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" - begin - struct = OpenStruct.new(get_url_as_json(url).fetch("movies").first) + raw_result_json = get_url_as_json(url) + build_movie_from_result(raw_result_json) + + end + def self.build_movie_from_result(json) + if json["movies"].count > 0 + struct = OpenStruct.new(json.fetch("movies").first) Movie.new(id: struct.id.to_i, title: struct.title, year: struct.year, score: struct.ratings["critics_score"] ) - rescue NoMethodError - puts "Movie Not Found" + else + raise MovieNotFoundError end end diff --git a/movie_json.rb b/movie_json.rb index 899a0f2..598053b 100644 --- a/movie_json.rb +++ b/movie_json.rb @@ -5,7 +5,7 @@ def get_average(movies) averages = [] movies.each do |m| - averages << m.score if !m.nil? + averages << m.score end averages.inject{ |sum, el| sum + el }.to_f / averages.size @@ -16,9 +16,14 @@ def find_movie puts "Add a movie you really like" movie_title = gets @movies = [] if @movies.nil? - @movies << Api.search_by_title(movie_title) - puts "Found: #{@movies.last.title}. Score: #{@movies.last.score}. Avg. Score: #{get_average(@movies)}" if !@movies.last.nil? + begin + @movies << Api.search_by_title(movie_title) + puts "Found: #{@movies.last.title}. Score: #{@movies.last.score}. Avg. Score: #{get_average(@movies)}" + rescue Api::MovieNotFoundError + puts "Movie was not found :( :( " + end + end find_movie