From a6a247e38ca1f8b7dabd72aed81e5cce6d980db5 Mon Sep 17 00:00:00 2001 From: Elle Yoko Suzuki Date: Tue, 27 Nov 2012 21:42:23 -0800 Subject: [PATCH 1/3] customized formatting --- lib/api.rb | 5 ++--- lib/movie.rb | 2 +- spec/api_spec.rb | 2 +- spec/movie_spec.rb | 3 +-- 4 files changed, 5 insertions(+), 7 deletions(-) diff --git a/lib/api.rb b/lib/api.rb index a8d499c..3a75d0a 100644 --- a/lib/api.rb +++ b/lib/api.rb @@ -1,9 +1,10 @@ require "open-uri" require "json" require "ostruct" + require_relative "./movie" -class Api +class Api APIKEY="4t6456xa33z8qhcqyuqgnkjh" def self.search_by_title(title) @@ -16,9 +17,7 @@ def self.search_by_title(title) ) end - def self.get_url_as_json(url) JSON.parse(open(url).read) end - end diff --git a/lib/movie.rb b/lib/movie.rb index 167a23e..6293b0a 100644 --- a/lib/movie.rb +++ b/lib/movie.rb @@ -1,6 +1,6 @@ class Movie - attr_reader :id, :title, :year, :score + def initialize(hash={}) @id = hash.fetch(:id) @title = hash.fetch(:title) diff --git a/spec/api_spec.rb b/spec/api_spec.rb index 9014106..bab9791 100644 --- a/spec/api_spec.rb +++ b/spec/api_spec.rb @@ -1,8 +1,8 @@ require_relative "../lib/api" + require "ostruct" describe Api do - let(:movie) { Api.search_by_title("Forrest Gump") } before do diff --git a/spec/movie_spec.rb b/spec/movie_spec.rb index 088bd37..ec485d7 100644 --- a/spec/movie_spec.rb +++ b/spec/movie_spec.rb @@ -1,6 +1,6 @@ require_relative "../lib/movie" -describe Movie do +describe Movie do it "should store the title, year, and score" do movie = Movie.new(id: "the-id", title: "the-title", year: 1998, score: 50) movie.id.should eq("the-id") @@ -8,5 +8,4 @@ movie.year.should eq(1998) movie.score.should eq(50) end - end From 81cac474c774c90974b2f5266aa17df6a4bc478a Mon Sep 17 00:00:00 2001 From: Elle Yoko Suzuki Date: Wed, 28 Nov 2012 00:05:19 -0800 Subject: [PATCH 2/3] code no longer blows up when movie not found; handles empty search --- lib/api.rb | 24 ++++++++--- movie_json.rb | 8 +++- spec/api_spec.rb | 80 ++++++++++++++++++++++++++++-------- spec/fixtures/no_val.json | 3 ++ spec/fixtures/not_found.json | 10 +++++ 5 files changed, 101 insertions(+), 24 deletions(-) create mode 100644 spec/fixtures/no_val.json create mode 100644 spec/fixtures/not_found.json diff --git a/lib/api.rb b/lib/api.rb index 3a75d0a..cf0ccef 100644 --- a/lib/api.rb +++ b/lib/api.rb @@ -9,14 +9,26 @@ 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"] - ) + + result = get_url_as_json(url) + + if found?(result) + struct = OpenStruct.new(result.fetch('movies').first) + + Movie.new(id: struct.id.to_i, + title: struct.title, + year: struct.year, + score: struct.ratings["critics_score"] + ) + else + nil + end end + def self.found?(result) + result.has_key?('movies') && !(result.fetch('movies').empty?) + 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..2b127d3 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 HAI. Search:" #changed from 'Search?' to 'Search:' as former suggests 'Y/N' response rather than film name movie_title = gets movie = Api.search_by_title(movie_title) - puts "Found: #{movie.title}. Score: #{movie.score}" + if !movie.nil? + puts "Found: #{movie.title}. Score: #{movie.score}" + else + puts 'Movie not found' + end end find_movie diff --git a/spec/api_spec.rb b/spec/api_spec.rb index bab9791..5908681 100644 --- a/spec/api_spec.rb +++ b/spec/api_spec.rb @@ -3,25 +3,73 @@ require "ostruct" describe Api do - let(:movie) { Api.search_by_title("Forrest Gump") } + context '#movie not found' do + let(:movie) { Api.search_by_title('abcdkdjfakdj') } - before do - Api.stub(:get_url_as_json) { JSON.parse(File.read("spec/fixtures/forrest.json")) } - end + before do + Api.stub(:get_url_as_json) { + JSON.parse(File.read('spec/fixtures/not_found.json')) + } + end - it "should search for movies" do - movie.title.should eq("Forrest Gump") - end + it 'should search for movie and return nothing' do + movie.should be_nil + end + + it 'should have found nothing' do + result = {'movies' => []} + Api.found?(result).should be_false + end + 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 + context '#movie not found when no search term' do + let(:movie) { Api.search_by_title('') } + + before do + Api.stub(:get_url_as_json) { + JSON.parse(File.read('spec/fixtures/no_val.json')) + } + end + + it 'should search for movie and return nothing' do + movie.should be_nil + end + + it 'should have found nothing' do + result = {} + Api.found?(result).should be_false + end + end + + + context '#movie found' 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 + + 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 have found a movie' do + result = {'movies' => [{"id"=>"470554171", "title"=>"Ratatouille", "year"=>2007,"ratings"=>{"critics_rating"=>"Certified Fresh", "critics_score"=>96}}]} + Api.found?(result).should be_true + end + end - it "should return the year" do - movie.year.should eq(1994) - end end diff --git a/spec/fixtures/no_val.json b/spec/fixtures/no_val.json new file mode 100644 index 0000000..4e8aecd --- /dev/null +++ b/spec/fixtures/no_val.json @@ -0,0 +1,3 @@ +{ + "link_template": "http://api.rottentomatoes.com/api/public/v1.0/movies.json?q={search-term}&page_limit={results-per-page}&page={page-number}" +} diff --git a/spec/fixtures/not_found.json b/spec/fixtures/not_found.json new file mode 100644 index 0000000..4077be7 --- /dev/null +++ b/spec/fixtures/not_found.json @@ -0,0 +1,10 @@ +{ + "link_template": "http://api.rottentomatoes.com/api/public/v1.0/movies.json?q={search-term}&page_limit={results-per-page}&page={page-number}", + "total": 0, + "movies": [ + + ], + "links": { + "self": "http://api.rottentomatoes.com/api/public/v1.0/movies.json?q=ABDCadskfjask&page_limit=1&page=1" + } +} From f475d243a9927eab4664a9d0fd4180fc2499f23e Mon Sep 17 00:00:00 2001 From: Elle Yoko Suzuki Date: Mon, 10 Dec 2012 13:12:58 -0800 Subject: [PATCH 3/3] tracks searches user enters (and finds); rewording of inquiry; average score of movies found --- movie_json.rb | 39 +++++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/movie_json.rb b/movie_json.rb index 2b127d3..01c1d68 100644 --- a/movie_json.rb +++ b/movie_json.rb @@ -1,25 +1,56 @@ require_relative "lib/movie" require_relative "lib/api" +# function definitions def find_movie - puts "OH HAI. Search:" #changed from 'Search?' to 'Search:' as former suggests 'Y/N' response rather than film name + puts "OH HAI. Add a movie you really like:" movie_title = gets movie = Api.search_by_title(movie_title) + if !movie.nil? puts "Found: #{movie.title}. Score: #{movie.score}" else puts 'Movie not found' end + + movie +end + +def get_average(movies) + scores = [] + movies.each do |movie| + scores << movie.score if !movie.nil? + end + total = scores.inject { |sum,n| sum+n } + total.to_f/scores.size end -find_movie + + +# main +movies = [] + +movie = find_movie +movies << movie if !movie.nil? while true do - puts "Search Again (Y/N)" + puts "Add another? (Y/N)" answer = gets.upcase[0] if answer == "Y" - find_movie + movie = find_movie + movies << movie if !movie.nil? else + puts + # assuming movies that were actually found + puts 'Movies searched and found: ' + if (movies.nil? || movies.empty?) + puts 'None' + else + movies.each { |movie| puts movie.title } + puts + puts 'The average score of movies searched and found: ' + puts get_average(movies) + end break end end