diff --git a/lib/api.rb b/lib/api.rb index a8d499c..0e81495 100644 --- a/lib/api.rb +++ b/lib/api.rb @@ -9,14 +9,21 @@ 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 + Movie.new(id: 1, + title: "No movie by that title", + year: 1111, + score: 0 + ) + else + 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) JSON.parse(open(url).read) end diff --git a/movie_json.rb b/movie_json.rb index d8a91d7..f03ee78 100644 --- a/movie_json.rb +++ b/movie_json.rb @@ -1,20 +1,32 @@ require_relative "lib/movie" require_relative "lib/api" -def find_movie - puts "OH HAI. Search?" + + +def find_movie() + puts "Add a movie you really like" movie_title = gets movie = Api.search_by_title(movie_title) puts "Found: #{movie.title}. Score: #{movie.score}" + movie.score +end + +def calculate_average(array_of_numbers) + array_of_numbers.inject(0.0) { |sum, el| sum + el } / array_of_numbers.size end -find_movie + +movies = [] + +movies << find_movie while true do puts "Search Again (Y/N)" answer = gets.upcase[0] + if answer == "Y" - find_movie + movies << find_movie + puts "The average score of your movies is: " + calculate_average(movies).to_s else break end diff --git a/spec/api_spec.rb b/spec/api_spec.rb index 9014106..9b496fc 100644 --- a/spec/api_spec.rb +++ b/spec/api_spec.rb @@ -3,25 +3,45 @@ describe Api do - let(:movie) { Api.search_by_title("Forrest Gump") } + describe "this is what a search that returns a movie should do" do - before do - Api.stub(:get_url_as_json) { JSON.parse(File.read("spec/fixtures/forrest.json")) } - end - it "should search for movies" do - movie.title.should eq("Forrest Gump") - end + 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 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 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 "this is how a search that does not find a movie should work" do + + let(:movie) { Api.search_by_title("Noodle Weird") } + + before do + Api.stub(:get_url_as_json) { JSON.parse(File.read("spec/fixtures/noodle.json")) } + end + + it "should not crash if a search returns no movie" do + movie.title.should eq("No movie by that title") + end + end + end diff --git a/spec/fixtures/noodle.json b/spec/fixtures/noodle.json new file mode 100644 index 0000000..167ac4d --- /dev/null +++ b/spec/fixtures/noodle.json @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +{"total":0,"movies":[],"links":{"self":"http://api.rottentomatoes.com/api/public/v1.0/movies.json?q=Noodle+Weird&page_limit=1&page=1"},"link_template":"http://api.rottentomatoes.com/api/public/v1.0/movies.json?q={search-term}&page_limit={results-per-page}&page={page-number}"}