From 60049ad0a15a436099f2d2884d3fd027c7849c15 Mon Sep 17 00:00:00 2001 From: Jen Dockter Date: Tue, 12 Mar 2013 10:25:14 -0700 Subject: [PATCH] Panda Level--no exceptions --- lib/api.rb | 13 ++++++++----- movie_json.rb | 40 +++++++++++++++++++++++++++++++--------- spec/api_spec.rb | 5 +++++ spec/movie_spec.rb | 1 + 4 files changed, 45 insertions(+), 14 deletions(-) diff --git a/lib/api.rb b/lib/api.rb index a8d499c..09bf21a 100644 --- a/lib/api.rb +++ b/lib/api.rb @@ -9,11 +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"] - ) + if struct.title.nil? + else + Movie.new(id: struct.id.to_i, + title: struct.title, + year: struct.year, + score: struct.ratings["critics_score"] + ) + end end diff --git a/movie_json.rb b/movie_json.rb index d8a91d7..1c3e463 100644 --- a/movie_json.rb +++ b/movie_json.rb @@ -1,21 +1,43 @@ require_relative "lib/movie" require_relative "lib/api" + +def search_again + while true do + puts "Search Again (Y/N)" + answer = gets.upcase[0] + if answer == "Y" + find_movie + else + break + end + end +end + def find_movie puts "OH HAI. Search?" movie_title = gets movie = Api.search_by_title(movie_title) + if movie puts "Found: #{movie.title}. Score: #{movie.score}" + else + puts "#{movie_title} was not found." + end + search_again end find_movie -while true do - puts "Search Again (Y/N)" - answer = gets.upcase[0] - if answer == "Y" - find_movie - else - break - end -end +#So we want to do something after movie = Api.search_by_title(movie_title) like if movie == false {puts "#{movie.title} not found, Search Again (Y/N)?" -then basically the while loop below--} +#if movie == false + # puts "#{movie.title} was not found. Search again? (Y/N)" + #answer = gets.upcase[0] + #if answer =="Y" + # find_movie + #end +#end + + + + + diff --git a/spec/api_spec.rb b/spec/api_spec.rb index 9014106..4f3f488 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 return a nil response if no movie title is found" do + expect { Api.search_by_title("NOTHINGFOUNDHERE") }.to_not raise_error + end + end diff --git a/spec/movie_spec.rb b/spec/movie_spec.rb index 088bd37..8b9774f 100644 --- a/spec/movie_spec.rb +++ b/spec/movie_spec.rb @@ -9,4 +9,5 @@ movie.score.should eq(50) end + end