From a46fa90bee10632d215a0e63469702b7c656ebb3 Mon Sep 17 00:00:00 2001 From: dharini Date: Sun, 10 Mar 2013 01:31:58 -0600 Subject: [PATCH 1/3] working version of panda level; testing of raising error does not work yet --- lib/api.rb | 16 ++++++++++------ movie_json.rb | 10 +++++++--- spec/api_spec.rb | 46 +++++++++++++++++++++++++++++++++------------- 3 files changed, 50 insertions(+), 22 deletions(-) diff --git a/lib/api.rb b/lib/api.rb index a8d499c..ebd67a6 100644 --- a/lib/api.rb +++ b/lib/api.rb @@ -8,12 +8,16 @@ 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"] - ) + 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"] + ) + rescue + raise ArgumentError, "invalid movie #{title.strip} given" + end end diff --git a/movie_json.rb b/movie_json.rb index d8a91d7..aa27712 100644 --- a/movie_json.rb +++ b/movie_json.rb @@ -2,10 +2,14 @@ require_relative "lib/api" def find_movie - puts "OH HAI. Search?" + puts "OH HI. Search?" movie_title = gets - movie = Api.search_by_title(movie_title) - puts "Found: #{movie.title}. Score: #{movie.score}" + begin + movie = Api.search_by_title(movie_title) + puts "Found: #{movie.title}. Score: #{movie.score}" + rescue ArgumentError => a + puts a.message + end end find_movie diff --git a/spec/api_spec.rb b/spec/api_spec.rb index 9014106..d741d3c 100644 --- a/spec/api_spec.rb +++ b/spec/api_spec.rb @@ -3,25 +3,45 @@ describe Api do + describe "valid movie title" 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")) } - end + before do + expect { + Api.stub(:get_url_as_json) { JSON.parse(File.read("spec/fixtures/forrest.json")) } + }.to_not raise_error + end - it "should search for movies" do - movie.title.should eq("Forrest Gump") - end + it "should search for movies" do + movie.title.should eq("Forrest Gump") + end - it "should return the score" do - movie.score.should eq(71) - end + 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 year" do + movie.year.should eq(1994) + end - it "should return the id" do - movie.id.should eq(10036) end - it "should return the year" do - movie.year.should eq(1994) + describe "invalid movie title" do + let(:movie) { Api.search_by_title("hgh") } + + it "should raise ArgumentError" do + expect { + Api.stub(:get_url_as_json) { JSON.parse(File.read("spec/fixtures/invalid.json")) } + }.to raise_error + movie.year.should eq(1994) + end + end + + end From c588dcee3dbb0b941451d080d16b3265250110f3 Mon Sep 17 00:00:00 2001 From: dharini Date: Mon, 11 Mar 2013 01:00:24 -0500 Subject: [PATCH 2/3] return :no_movie_found when invalid movie title entered instead of raising an exception --- lib/api.rb | 3 ++- movie_json.rb | 8 ++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/api.rb b/lib/api.rb index ebd67a6..6cdcbdc 100644 --- a/lib/api.rb +++ b/lib/api.rb @@ -16,7 +16,8 @@ def self.search_by_title(title) score: struct.ratings["critics_score"] ) rescue - raise ArgumentError, "invalid movie #{title.strip} given" + #raise ArgumentError, "invalid movie #{title.strip} given" + return :no_movie_found end end diff --git a/movie_json.rb b/movie_json.rb index aa27712..361b2c0 100644 --- a/movie_json.rb +++ b/movie_json.rb @@ -4,11 +4,11 @@ def find_movie puts "OH HI. Search?" movie_title = gets - begin - movie = Api.search_by_title(movie_title) + movie = Api.search_by_title(movie_title) + if movie == :no_movie_found + puts "Invalid movie title : #{movie_title.strip}" + else puts "Found: #{movie.title}. Score: #{movie.score}" - rescue ArgumentError => a - puts a.message end end From 0b4b21490160168f1086fd63aaa2e70f6980297d Mon Sep 17 00:00:00 2001 From: dharini Date: Mon, 11 Mar 2013 01:12:31 -0500 Subject: [PATCH 3/3] fix test for invalid movie title --- spec/api_spec.rb | 55 +++++++++++++++++++++++++----------------------- 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/spec/api_spec.rb b/spec/api_spec.rb index d741d3c..7af3668 100644 --- a/spec/api_spec.rb +++ b/spec/api_spec.rb @@ -3,43 +3,46 @@ describe Api do - describe "valid movie title" do + context "valid movie title" do - let(:movie) { Api.search_by_title("Forrest Gump") } + let(:movie) { Api.search_by_title("Forrest Gump") } - before do - expect { - Api.stub(:get_url_as_json) { JSON.parse(File.read("spec/fixtures/forrest.json")) } - }.to_not raise_error - end + before do + expect { + Api.stub(:get_url_as_json) { JSON.parse(File.read("spec/fixtures/forrest.json")) } + }.to_not raise_error + end - it "should search for movies" do - movie.title.should eq("Forrest Gump") - end + it "should search for movies" do + movie.title.should eq("Forrest Gump") + end - it "should return the score" do - movie.score.should eq(71) - end + 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) + end - it "should return the year" do - movie.year.should eq(1994) - end + it "should return the year" do + movie.year.should eq(1994) + end end - describe "invalid movie title" do + context "invalid movie title" do let(:movie) { Api.search_by_title("hgh") } - it "should raise ArgumentError" do - expect { - Api.stub(:get_url_as_json) { JSON.parse(File.read("spec/fixtures/invalid.json")) } - }.to raise_error - movie.year.should eq(1994) - end + before do + expect { + Api.stub(:get_url_as_json) { JSON.parse(File.read("spec/fixtures/invalid.json")) } + }.to_not raise_error + end + + it "should return :no_movie_found" do + movie.should eq(:no_movie_found) + end end