From 3327705b0180eb7edb91eb814a42ba4a08930187 Mon Sep 17 00:00:00 2001 From: noahpatterson Date: Sat, 14 Dec 2013 16:11:44 -0500 Subject: [PATCH 1/8] add .gitignore --- .gitignore | 1 + lib/api.rb | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..112a63b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +api_key.rb \ No newline at end of file diff --git a/lib/api.rb b/lib/api.rb index a8d499c..4741787 100644 --- a/lib/api.rb +++ b/lib/api.rb @@ -1,10 +1,11 @@ require "open-uri" require "json" -require "ostruct" require_relative "./movie" +require_relative "./api_key" + class Api - APIKEY="4t6456xa33z8qhcqyuqgnkjh" + APIKEY=API_KEY 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" From db6bd2496086e81725e515e2d3de55a42c75e1a6 Mon Sep 17 00:00:00 2001 From: noahpatterson Date: Sat, 14 Dec 2013 16:53:05 -0500 Subject: [PATCH 2/8] add .rspec, modify api.rb to simplify 'search_by_title', updated my version of movie_json.rb --- .rspec | 2 ++ lib/api.rb | 13 ++++++++----- movie_json.rb | 51 ++++++++++++++++++++++++++++++++++++++++----------- 3 files changed, 50 insertions(+), 16 deletions(-) create mode 100644 .rspec diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..16f9cdb --- /dev/null +++ b/.rspec @@ -0,0 +1,2 @@ +--color +--format documentation diff --git a/lib/api.rb b/lib/api.rb index 4741787..ddbe4b0 100644 --- a/lib/api.rb +++ b/lib/api.rb @@ -10,11 +10,7 @@ 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"] - ) + store_movie(struct) end @@ -22,4 +18,11 @@ def self.get_url_as_json(url) JSON.parse(open(url).read) end + def self.store_movie(movie_struct) + Movie.new(id: movie_struct.id.to_i, + title: movie_struct.title, + year: movie_struct.year, + score: movie_struct.ratings["critics_score"] + ) + end end diff --git a/movie_json.rb b/movie_json.rb index d8a91d7..28cbd88 100644 --- a/movie_json.rb +++ b/movie_json.rb @@ -1,21 +1,50 @@ require_relative "lib/movie" require_relative "lib/api" -def find_movie - puts "OH HAI. Search?" +def movie_search_by_title + puts "OH HAI. Please enter movie title" + print ">>" movie_title = gets - movie = Api.search_by_title(movie_title) - puts "Found: #{movie.title}. Score: #{movie.score}" + puts "Cool, searching for #{movie_title}" + search = Api.search_by_title(movie_title) + puts "#{movie_title} was released in #{search.year}. It recieved a critics score of #{search.score}" end -find_movie - -while true do - puts "Search Again (Y/N)" - answer = gets.upcase[0] - if answer == "Y" - find_movie +def search_again(response) + if response == "yes" + movie_search_by_title + elsif response == "no" + false else + puts "I don't understand that" + end +end + +movie_search_by_title + +while true + puts "Search Again?" + re_search = gets.downcase.chomp + if search_again(re_search) == false break end end + +# def find_movie +# puts "OH HAI. Search?" +# movie_title = gets +# movie = Api.search_by_title(movie_title) +# puts "Found: #{movie.title}. Score: #{movie.score}" +# end + +# find_movie + +# while true do +# puts "Search Again (Y/N)" +# answer = gets.upcase[0] +# if answer == "Y" +# find_movie +# else +# break +# end +# end From 7b24a18d808f4fe6c86ec03c911126b3ecc64e84 Mon Sep 17 00:00:00 2001 From: noahpatterson Date: Thu, 19 Dec 2013 22:41:05 -0500 Subject: [PATCH 3/8] panda level. handle no-search and no-movie --- lib/api.rb | 2 +- movie_json.rb | 12 ++++++++---- spec/api_spec.rb | 14 +++++++++++++- spec/fixtures/no_movie.json | 1 + spec/movie_spec.rb | 8 ++++++++ 5 files changed, 31 insertions(+), 6 deletions(-) create mode 100644 spec/fixtures/no_movie.json diff --git a/lib/api.rb b/lib/api.rb index ddbe4b0..b76eaae 100644 --- a/lib/api.rb +++ b/lib/api.rb @@ -10,7 +10,7 @@ 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) - store_movie(struct) + struct.id.nil? ? "Sorry we could not find that movie" : store_movie(struct) end diff --git a/movie_json.rb b/movie_json.rb index 28cbd88..8ade560 100644 --- a/movie_json.rb +++ b/movie_json.rb @@ -4,10 +4,14 @@ def movie_search_by_title puts "OH HAI. Please enter movie title" print ">>" - movie_title = gets - puts "Cool, searching for #{movie_title}" - search = Api.search_by_title(movie_title) - puts "#{movie_title} was released in #{search.year}. It recieved a critics score of #{search.score}" + movie_title = gets.chomp + if movie_title.empty? + puts "Sorry, but you have to actually enter something to search" + else + puts "Cool, searching for #{movie_title}" + search = Api.search_by_title(movie_title) + puts "#{movie_title} was released in #{search.year}. It recieved a critics score of #{search.score}" + end end def search_again(response) diff --git a/spec/api_spec.rb b/spec/api_spec.rb index 9014106..e34c0da 100644 --- a/spec/api_spec.rb +++ b/spec/api_spec.rb @@ -3,7 +3,7 @@ describe Api do - let(:movie) { Api.search_by_title("Forrest Gump") } + let(:movie) { Api.search_by_title("Forrest Gump") } before do Api.stub(:get_url_as_json) { JSON.parse(File.read("spec/fixtures/forrest.json")) } @@ -24,4 +24,16 @@ it "should return the year" do movie.year.should eq(1994) end + + it "should return a warning statement if movie was not found" do + Api.stub(:get_url_as_json) { JSON.parse(File.read("spec/fixtures/no_movie.json")) } + no_movie = Api.search_by_title("dfsfsfdf") + expect(no_movie.class).to eq(String) + end + + it "should return a warning when no movie is entered into the search" do + Api.stub(:get_url_as_json) { JSON.parse(File.read("spec/fixtures/no_movie.json")) } + no_movie = Api.search_by_title("dfsfsfdf") + expect(no_movie.class).to eq(String) + end end diff --git a/spec/fixtures/no_movie.json b/spec/fixtures/no_movie.json new file mode 100644 index 0000000..4d925e7 --- /dev/null +++ b/spec/fixtures/no_movie.json @@ -0,0 +1 @@ +{"total":0,"movies":[],"links":{"self":"http://api.rottentomatoes.com/api/public/v1.0/movies.json?q=Forrest+Gump&page_limit=1&page=1","next":"http://api.rottentomatoes.com/api/public/v1.0/movies.json?q=Forrest+Gump&page_limit=1&page=2"},"link_template":"http://api.rottentomatoes.com/api/public/v1.0/movies.json?q={search-term}&page_limit={results-per-page}&page={page-number}"} \ No newline at end of file diff --git a/spec/movie_spec.rb b/spec/movie_spec.rb index 088bd37..6cce3be 100644 --- a/spec/movie_spec.rb +++ b/spec/movie_spec.rb @@ -1,4 +1,12 @@ require_relative "../lib/movie" +require_relative "../movie_json" + +# describe "#movie_search_by_title" do +# it "should return a warning when no movie is entered into the search" do +# expect(movie_search_by_title).to eq(String) +# end +# end + describe Movie do it "should store the title, year, and score" do From e44934aebcb8b64f08852bf9645e5c09dfddc783 Mon Sep 17 00:00:00 2001 From: noahpatterson Date: Fri, 20 Dec 2013 23:02:10 -0700 Subject: [PATCH 4/8] Tiger level. --- movie_json.rb | 81 ++++++++++++++++++++++++++++------------- spec/api_spec.rb | 7 +--- spec/movie_json_spec.rb | 43 ++++++++++++++++++++++ spec/movie_spec.rb | 8 +--- 4 files changed, 102 insertions(+), 37 deletions(-) create mode 100644 spec/movie_json_spec.rb diff --git a/movie_json.rb b/movie_json.rb index 8ade560..a8a4ff6 100644 --- a/movie_json.rb +++ b/movie_json.rb @@ -1,39 +1,70 @@ require_relative "lib/movie" require_relative "lib/api" -def movie_search_by_title - puts "OH HAI. Please enter movie title" - print ">>" - movie_title = gets.chomp - if movie_title.empty? - puts "Sorry, but you have to actually enter something to search" - else +class MovieJson + + attr_reader :movies + def initialize + @movies = [] + end + + def run + search + end + + def movie_search_by_title + puts "OH HAI. Please add a movie you like" + print ">>" + movie_title = gets.downcase.chomp + return "Sorry, but you have to actually enter something to search" if movie_title.empty? puts "Cool, searching for #{movie_title}" - search = Api.search_by_title(movie_title) - puts "#{movie_title} was released in #{search.year}. It recieved a critics score of #{search.score}" + search = add_to_movie_list(movie_title) + return search if search.class == String + puts movie_title != search.title ? "This is the closest we could find: #{search.title} was released in #{search.year}. It recieved a critics score of #{search.score}" : "#{search.title} was released in #{search.year}. It recieved a critics score of #{search.score}" end -end -def search_again(response) - if response == "yes" - movie_search_by_title - elsif response == "no" - false - else - puts "I don't understand that" + def add_to_movie_list(movie) + found_movie = Api.search_by_title(movie) + @movies << found_movie + found_movie end -end -movie_search_by_title + def search_again(response) + if response == "yes" + movie_search_by_title + elsif response == "no" + false + else + puts "I don't understand that" + end + end -while true - puts "Search Again?" - re_search = gets.downcase.chomp - if search_again(re_search) == false - break + def search + puts "Would you like to: \n1) Search\n2) Calculate happiness?\n3) Exit?" + print ">>" + u_start = gets.chomp + if u_start == "1" + puts movie_search_by_title + while true + puts "Search Again? Yes or No" + re_search = gets.downcase.chomp + break if search_again(re_search) == false + end + search + elsif u_start == "2" + puts calculate_happiness + search + else + puts "Exiting..." + end end -end + def calculate_happiness + @movies.map {|movie| movie.score}.reduce(:+).to_f / @movies.size + end + +end +MovieJson.new.run if __FILE__ == $PROGRAM_NAME # def find_movie # puts "OH HAI. Search?" # movie_title = gets diff --git a/spec/api_spec.rb b/spec/api_spec.rb index e34c0da..0504b70 100644 --- a/spec/api_spec.rb +++ b/spec/api_spec.rb @@ -3,7 +3,7 @@ describe Api do - let(:movie) { Api.search_by_title("Forrest Gump") } + let(:movie) { Api.search_by_title("Forrest Gump") } before do Api.stub(:get_url_as_json) { JSON.parse(File.read("spec/fixtures/forrest.json")) } @@ -31,9 +31,4 @@ expect(no_movie.class).to eq(String) end - it "should return a warning when no movie is entered into the search" do - Api.stub(:get_url_as_json) { JSON.parse(File.read("spec/fixtures/no_movie.json")) } - no_movie = Api.search_by_title("dfsfsfdf") - expect(no_movie.class).to eq(String) - end end diff --git a/spec/movie_json_spec.rb b/spec/movie_json_spec.rb new file mode 100644 index 0000000..98ce410 --- /dev/null +++ b/spec/movie_json_spec.rb @@ -0,0 +1,43 @@ +require_relative "../movie_json" +require_relative "../lib/api" +require_relative "../lib/movie" + +describe MovieJson do + +let(:movie) { Movie.new(id: "111", + title: "Forrest Gump", + year: 1992, + score: 71) + } + +before do + # Api.stub(:get_url_as_json) { JSON.parse(File.read("spec/fixtures/forrest.json")) } + Api.stub(:search_by_title) {movie} + # MovieJson.stub(:gets).and_return("") +end + +describe "#movie_search_by_title" do + it "should return a warning when no movie is entered into the search" do + movie_search = MovieJson.new + movie_search.stub(:gets).and_return("") + movie_search.movie_search_by_title.should == "Sorry, but you have to actually enter something to search" + end +end + +it "should allow the user to add two movies they like" do + movie_search = MovieJson.new + movie_search.add_to_movie_list(movie) + movie_search.add_to_movie_list(movie) + expect(movie_search.movies.length).to eq(2) +end + +describe "@calculate_happiness" do + it "should give average happiness of the movies in your list" do + movie_search = MovieJson.new + movie_search.add_to_movie_list(movie) + movie_search.add_to_movie_list(movie) + expect(movie_search.calculate_happiness).to eq(71) + end +end + +end \ No newline at end of file diff --git a/spec/movie_spec.rb b/spec/movie_spec.rb index 6cce3be..84a7bfd 100644 --- a/spec/movie_spec.rb +++ b/spec/movie_spec.rb @@ -1,11 +1,7 @@ require_relative "../lib/movie" -require_relative "../movie_json" -# describe "#movie_search_by_title" do -# it "should return a warning when no movie is entered into the search" do -# expect(movie_search_by_title).to eq(String) -# end -# end + + describe Movie do From 5e25e2d4b0e4dc8847e42f56a782c7b09e9edaf4 Mon Sep 17 00:00:00 2001 From: noahpatterson Date: Sat, 21 Dec 2013 17:57:26 -0700 Subject: [PATCH 5/8] Add Tiger level --- movie_json.rb | 33 ++++++++++++++- spec/movie_json_spec.rb | 90 ++++++++++++++++++++++++++++++++--------- 2 files changed, 102 insertions(+), 21 deletions(-) diff --git a/movie_json.rb b/movie_json.rb index a8a4ff6..fe2d147 100644 --- a/movie_json.rb +++ b/movie_json.rb @@ -40,7 +40,7 @@ def search_again(response) end def search - puts "Would you like to: \n1) Search\n2) Calculate happiness?\n3) Exit?" + puts "Would you like to: \n1) Search\n2) Average rating?\n3) Average year?\n4) Calculate movie happiness?\n5) Exit?" print ">>" u_start = gets.chomp if u_start == "1" @@ -52,6 +52,12 @@ def search end search elsif u_start == "2" + puts average_raiting + search + elsif u_start == "3" + puts average_year + search + elsif u_start == "4" puts calculate_happiness search else @@ -59,10 +65,33 @@ def search end end - def calculate_happiness + def average_raiting @movies.map {|movie| movie.score}.reduce(:+).to_f / @movies.size end + def average_year + avg_year = @movies.map { |movie| movie.year}.reduce(:+).to_f / @movies.size + avg_year.to_i + end + + def calculate_happiness + # ratings = @movies.map { |movie| movie.score } + year_avg = {} + @movies.each { |movie| year_avg[movie.year] = [] } + @movies.each { |movie| year_avg[movie.year].push(movie.score) } + years = @movies.map { |movie| movie.year}.sort + oldest_year_avg = year_avg[years[0]].reduce(:+).to_f / year_avg[years[0]].size + youngest_year_avg = year_avg[years[-1]].reduce(:+).to_f / year_avg[years[-1]].size + slope = years[0] - years[-1] != 0 ? (youngest_year_avg - oldest_year_avg).to_f / (years[-1] - years[0]).to_f : 0 + if slope == 0 + return "You're neutral" + elsif slope > 0 + return "You're getting happier!" + else + return "You're getting sadder" + end + end + end MovieJson.new.run if __FILE__ == $PROGRAM_NAME # def find_movie diff --git a/spec/movie_json_spec.rb b/spec/movie_json_spec.rb index 98ce410..73fadd5 100644 --- a/spec/movie_json_spec.rb +++ b/spec/movie_json_spec.rb @@ -4,11 +4,20 @@ describe MovieJson do -let(:movie) { Movie.new(id: "111", - title: "Forrest Gump", - year: 1992, - score: 71) - } +let(:movie) { Movie.new(id: "111", + title: "Forrest Gump", + year: 1994, + score: 71) + } +let(:happy_movie) { Movie.new(id: "112", + title: "Happy movie", + year: 1995, + score: 99) + } +let(:sad_movie) { Movie.new(id: "113", + title: "Sad movie", + year: 1995, + score: 1)} before do # Api.stub(:get_url_as_json) { JSON.parse(File.read("spec/fixtures/forrest.json")) } @@ -18,26 +27,69 @@ describe "#movie_search_by_title" do it "should return a warning when no movie is entered into the search" do - movie_search = MovieJson.new - movie_search.stub(:gets).and_return("") - movie_search.movie_search_by_title.should == "Sorry, but you have to actually enter something to search" + movie_search1 = MovieJson.new + movie_search1.stub(:gets).and_return("") + movie_search1.movie_search_by_title.should == "Sorry, but you have to actually enter something to search" end end it "should allow the user to add two movies they like" do - movie_search = MovieJson.new - movie_search.add_to_movie_list(movie) - movie_search.add_to_movie_list(movie) - expect(movie_search.movies.length).to eq(2) + movie_search2 = MovieJson.new + movie_search2.add_to_movie_list(movie) + movie_search2.add_to_movie_list(movie) + expect(movie_search2.movies.length).to eq(2) end -describe "@calculate_happiness" do - it "should give average happiness of the movies in your list" do - movie_search = MovieJson.new - movie_search.add_to_movie_list(movie) - movie_search.add_to_movie_list(movie) - expect(movie_search.calculate_happiness).to eq(71) +describe "#average_raiting" do + it "should give average rating of the movies in your list" do + movie_search3 = MovieJson.new + movie_search3.add_to_movie_list(movie) + movie_search3.add_to_movie_list(movie) + expect(movie_search3.average_raiting).to eq(71) end end -end \ No newline at end of file +describe "#average_year" do + it "should give the average year of the movies in your list" do + movie_search4 = MovieJson.new + movie_search4.add_to_movie_list(movie) + movie_search4.add_to_movie_list(movie) + expect(movie_search4.average_year).to eq(1994) + end +end + +describe "#calculate_happiness" do + it "should say your neutral at neutral slope" do + movie_search5 = MovieJson.new + movie_search5.add_to_movie_list(movie) + movie_search5.add_to_movie_list(movie) + movie_search5.calculate_happiness.should == "You're neutral" + end + + it "should say your happier at postive slope" do + movie_search6 = MovieJson.new + movie_search6.add_to_movie_list(movie) + Api.stub(:search_by_title) {happy_movie} + movie_search6.add_to_movie_list(happy_movie) + movie_search6.calculate_happiness.should == "You're getting happier!" + end + + it "should say your sadder at negative slope" do + movie_search7 = MovieJson.new + movie_search7.add_to_movie_list(movie) + Api.stub(:search_by_title) {sad_movie} + movie_search7.add_to_movie_list(sad_movie) + movie_search7.calculate_happiness.should == "You're getting sadder" + end + + it "should take the average of years that are the same and determine slope" do + movie_search7 = MovieJson.new + movie_search7.add_to_movie_list(movie) + Api.stub(:search_by_title) {sad_movie} + movie_search7.add_to_movie_list(sad_movie) + Api.stub(:search_by_title) {happy_movie} + movie_search7.add_to_movie_list(happy_movie) + movie_search7.calculate_happiness.should == "You're getting sadder" + end +end +end From f2b49c6abdb360bd30b57dff08208cb72bc170a8 Mon Sep 17 00:00:00 2001 From: noahpatterson Date: Sat, 21 Dec 2013 17:57:26 -0700 Subject: [PATCH 6/8] Add Eagle level --- movie_json.rb | 33 ++++++++++++++- spec/movie_json_spec.rb | 90 ++++++++++++++++++++++++++++++++--------- 2 files changed, 102 insertions(+), 21 deletions(-) diff --git a/movie_json.rb b/movie_json.rb index a8a4ff6..fe2d147 100644 --- a/movie_json.rb +++ b/movie_json.rb @@ -40,7 +40,7 @@ def search_again(response) end def search - puts "Would you like to: \n1) Search\n2) Calculate happiness?\n3) Exit?" + puts "Would you like to: \n1) Search\n2) Average rating?\n3) Average year?\n4) Calculate movie happiness?\n5) Exit?" print ">>" u_start = gets.chomp if u_start == "1" @@ -52,6 +52,12 @@ def search end search elsif u_start == "2" + puts average_raiting + search + elsif u_start == "3" + puts average_year + search + elsif u_start == "4" puts calculate_happiness search else @@ -59,10 +65,33 @@ def search end end - def calculate_happiness + def average_raiting @movies.map {|movie| movie.score}.reduce(:+).to_f / @movies.size end + def average_year + avg_year = @movies.map { |movie| movie.year}.reduce(:+).to_f / @movies.size + avg_year.to_i + end + + def calculate_happiness + # ratings = @movies.map { |movie| movie.score } + year_avg = {} + @movies.each { |movie| year_avg[movie.year] = [] } + @movies.each { |movie| year_avg[movie.year].push(movie.score) } + years = @movies.map { |movie| movie.year}.sort + oldest_year_avg = year_avg[years[0]].reduce(:+).to_f / year_avg[years[0]].size + youngest_year_avg = year_avg[years[-1]].reduce(:+).to_f / year_avg[years[-1]].size + slope = years[0] - years[-1] != 0 ? (youngest_year_avg - oldest_year_avg).to_f / (years[-1] - years[0]).to_f : 0 + if slope == 0 + return "You're neutral" + elsif slope > 0 + return "You're getting happier!" + else + return "You're getting sadder" + end + end + end MovieJson.new.run if __FILE__ == $PROGRAM_NAME # def find_movie diff --git a/spec/movie_json_spec.rb b/spec/movie_json_spec.rb index 98ce410..73fadd5 100644 --- a/spec/movie_json_spec.rb +++ b/spec/movie_json_spec.rb @@ -4,11 +4,20 @@ describe MovieJson do -let(:movie) { Movie.new(id: "111", - title: "Forrest Gump", - year: 1992, - score: 71) - } +let(:movie) { Movie.new(id: "111", + title: "Forrest Gump", + year: 1994, + score: 71) + } +let(:happy_movie) { Movie.new(id: "112", + title: "Happy movie", + year: 1995, + score: 99) + } +let(:sad_movie) { Movie.new(id: "113", + title: "Sad movie", + year: 1995, + score: 1)} before do # Api.stub(:get_url_as_json) { JSON.parse(File.read("spec/fixtures/forrest.json")) } @@ -18,26 +27,69 @@ describe "#movie_search_by_title" do it "should return a warning when no movie is entered into the search" do - movie_search = MovieJson.new - movie_search.stub(:gets).and_return("") - movie_search.movie_search_by_title.should == "Sorry, but you have to actually enter something to search" + movie_search1 = MovieJson.new + movie_search1.stub(:gets).and_return("") + movie_search1.movie_search_by_title.should == "Sorry, but you have to actually enter something to search" end end it "should allow the user to add two movies they like" do - movie_search = MovieJson.new - movie_search.add_to_movie_list(movie) - movie_search.add_to_movie_list(movie) - expect(movie_search.movies.length).to eq(2) + movie_search2 = MovieJson.new + movie_search2.add_to_movie_list(movie) + movie_search2.add_to_movie_list(movie) + expect(movie_search2.movies.length).to eq(2) end -describe "@calculate_happiness" do - it "should give average happiness of the movies in your list" do - movie_search = MovieJson.new - movie_search.add_to_movie_list(movie) - movie_search.add_to_movie_list(movie) - expect(movie_search.calculate_happiness).to eq(71) +describe "#average_raiting" do + it "should give average rating of the movies in your list" do + movie_search3 = MovieJson.new + movie_search3.add_to_movie_list(movie) + movie_search3.add_to_movie_list(movie) + expect(movie_search3.average_raiting).to eq(71) end end -end \ No newline at end of file +describe "#average_year" do + it "should give the average year of the movies in your list" do + movie_search4 = MovieJson.new + movie_search4.add_to_movie_list(movie) + movie_search4.add_to_movie_list(movie) + expect(movie_search4.average_year).to eq(1994) + end +end + +describe "#calculate_happiness" do + it "should say your neutral at neutral slope" do + movie_search5 = MovieJson.new + movie_search5.add_to_movie_list(movie) + movie_search5.add_to_movie_list(movie) + movie_search5.calculate_happiness.should == "You're neutral" + end + + it "should say your happier at postive slope" do + movie_search6 = MovieJson.new + movie_search6.add_to_movie_list(movie) + Api.stub(:search_by_title) {happy_movie} + movie_search6.add_to_movie_list(happy_movie) + movie_search6.calculate_happiness.should == "You're getting happier!" + end + + it "should say your sadder at negative slope" do + movie_search7 = MovieJson.new + movie_search7.add_to_movie_list(movie) + Api.stub(:search_by_title) {sad_movie} + movie_search7.add_to_movie_list(sad_movie) + movie_search7.calculate_happiness.should == "You're getting sadder" + end + + it "should take the average of years that are the same and determine slope" do + movie_search7 = MovieJson.new + movie_search7.add_to_movie_list(movie) + Api.stub(:search_by_title) {sad_movie} + movie_search7.add_to_movie_list(sad_movie) + Api.stub(:search_by_title) {happy_movie} + movie_search7.add_to_movie_list(happy_movie) + movie_search7.calculate_happiness.should == "You're getting sadder" + end +end +end From a14999e1a5eab67f1969f5aa9e57dcb9c2c7409d Mon Sep 17 00:00:00 2001 From: noahpatterson Date: Tue, 24 Dec 2013 08:40:01 -0700 Subject: [PATCH 7/8] fix apikey clarity. Add nullObject with naught. Split movie search and add to movie list methods. fix specs --- lib/api.rb | 9 ++++-- movie_json.rb | 38 +++++++--------------- spec/api_spec.rb | 2 +- spec/movie_json_spec.rb | 70 +++++++++++++++++++++-------------------- 4 files changed, 54 insertions(+), 65 deletions(-) diff --git a/lib/api.rb b/lib/api.rb index b76eaae..780f1e5 100644 --- a/lib/api.rb +++ b/lib/api.rb @@ -1,16 +1,19 @@ require "open-uri" require "json" +require "naught" require_relative "./movie" require_relative "./api_key" -class Api +NullObject = Naught.build do |config| + config.define_explicit_conversions +end - APIKEY=API_KEY +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) - struct.id.nil? ? "Sorry we could not find that movie" : store_movie(struct) + struct.id.nil? ? NullObject.new : store_movie(struct) end diff --git a/movie_json.rb b/movie_json.rb index fe2d147..69a47ac 100644 --- a/movie_json.rb +++ b/movie_json.rb @@ -12,26 +12,29 @@ def run search end - def movie_search_by_title + def movie_search_prompt puts "OH HAI. Please add a movie you like" print ">>" movie_title = gets.downcase.chomp return "Sorry, but you have to actually enter something to search" if movie_title.empty? puts "Cool, searching for #{movie_title}" - search = add_to_movie_list(movie_title) - return search if search.class == String + search = movie_search_by_title(movie_title) + return "Sorry we cannot find that movie" if search.title.nil? + add_to_movie_list(search) puts movie_title != search.title ? "This is the closest we could find: #{search.title} was released in #{search.year}. It recieved a critics score of #{search.score}" : "#{search.title} was released in #{search.year}. It recieved a critics score of #{search.score}" end + def movie_search_by_title(movie_title) + Api.search_by_title(movie_title) + end + def add_to_movie_list(movie) - found_movie = Api.search_by_title(movie) - @movies << found_movie - found_movie + @movies << movie end def search_again(response) if response == "yes" - movie_search_by_title + puts movie_search_prompt elsif response == "no" false else @@ -44,7 +47,7 @@ def search print ">>" u_start = gets.chomp if u_start == "1" - puts movie_search_by_title + puts movie_search_prompt while true puts "Search Again? Yes or No" re_search = gets.downcase.chomp @@ -75,7 +78,6 @@ def average_year end def calculate_happiness - # ratings = @movies.map { |movie| movie.score } year_avg = {} @movies.each { |movie| year_avg[movie.year] = [] } @movies.each { |movie| year_avg[movie.year].push(movie.score) } @@ -94,21 +96,3 @@ def calculate_happiness end MovieJson.new.run if __FILE__ == $PROGRAM_NAME -# def find_movie -# puts "OH HAI. Search?" -# movie_title = gets -# movie = Api.search_by_title(movie_title) -# puts "Found: #{movie.title}. Score: #{movie.score}" -# end - -# find_movie - -# while true do -# puts "Search Again (Y/N)" -# answer = gets.upcase[0] -# if answer == "Y" -# find_movie -# else -# break -# end -# end diff --git a/spec/api_spec.rb b/spec/api_spec.rb index 0504b70..f1d4e0c 100644 --- a/spec/api_spec.rb +++ b/spec/api_spec.rb @@ -28,7 +28,7 @@ it "should return a warning statement if movie was not found" do Api.stub(:get_url_as_json) { JSON.parse(File.read("spec/fixtures/no_movie.json")) } no_movie = Api.search_by_title("dfsfsfdf") - expect(no_movie.class).to eq(String) + expect(no_movie.title).to be_nil end end diff --git a/spec/movie_json_spec.rb b/spec/movie_json_spec.rb index 73fadd5..8c83053 100644 --- a/spec/movie_json_spec.rb +++ b/spec/movie_json_spec.rb @@ -14,10 +14,12 @@ year: 1995, score: 99) } -let(:sad_movie) { Movie.new(id: "113", +let(:sad_movie) { Movie.new(id: "113", title: "Sad movie", year: 1995, - score: 1)} + score: 1) + } +let(:movie_search) { MovieJson.new } before do # Api.stub(:get_url_as_json) { JSON.parse(File.read("spec/fixtures/forrest.json")) } @@ -27,69 +29,69 @@ describe "#movie_search_by_title" do it "should return a warning when no movie is entered into the search" do - movie_search1 = MovieJson.new - movie_search1.stub(:gets).and_return("") - movie_search1.movie_search_by_title.should == "Sorry, but you have to actually enter something to search" + # movie_search1 = MovieJson.new + movie_search.stub(:gets).and_return("") + movie_search.movie_search_prompt.should == "Sorry, but you have to actually enter something to search" end end it "should allow the user to add two movies they like" do - movie_search2 = MovieJson.new - movie_search2.add_to_movie_list(movie) - movie_search2.add_to_movie_list(movie) - expect(movie_search2.movies.length).to eq(2) + # movie_search2 = MovieJson.new + movie_search.add_to_movie_list(movie) + movie_search.add_to_movie_list(movie) + expect(movie_search.movies.length).to eq(2) end describe "#average_raiting" do it "should give average rating of the movies in your list" do - movie_search3 = MovieJson.new - movie_search3.add_to_movie_list(movie) - movie_search3.add_to_movie_list(movie) - expect(movie_search3.average_raiting).to eq(71) + # movie_search3 = MovieJson.new + movie_search.add_to_movie_list(movie) + movie_search.add_to_movie_list(movie) + expect(movie_search.average_raiting).to eq(71) end end describe "#average_year" do it "should give the average year of the movies in your list" do - movie_search4 = MovieJson.new - movie_search4.add_to_movie_list(movie) - movie_search4.add_to_movie_list(movie) - expect(movie_search4.average_year).to eq(1994) + # movie_search4 = MovieJson.new + movie_search.add_to_movie_list(movie) + movie_search.add_to_movie_list(movie) + expect(movie_search.average_year).to eq(1994) end end describe "#calculate_happiness" do it "should say your neutral at neutral slope" do - movie_search5 = MovieJson.new - movie_search5.add_to_movie_list(movie) - movie_search5.add_to_movie_list(movie) - movie_search5.calculate_happiness.should == "You're neutral" + # movie_search5 = MovieJson.new + movie_search.add_to_movie_list(movie) + movie_search.add_to_movie_list(movie) + movie_search.calculate_happiness.should == "You're neutral" end it "should say your happier at postive slope" do - movie_search6 = MovieJson.new - movie_search6.add_to_movie_list(movie) + # movie_search6 = MovieJson.new + movie_search.add_to_movie_list(movie) Api.stub(:search_by_title) {happy_movie} - movie_search6.add_to_movie_list(happy_movie) - movie_search6.calculate_happiness.should == "You're getting happier!" + movie_search.add_to_movie_list(happy_movie) + movie_search.calculate_happiness.should == "You're getting happier!" end it "should say your sadder at negative slope" do - movie_search7 = MovieJson.new - movie_search7.add_to_movie_list(movie) + # movie_search7 = MovieJson.new + movie_search.add_to_movie_list(movie) Api.stub(:search_by_title) {sad_movie} - movie_search7.add_to_movie_list(sad_movie) - movie_search7.calculate_happiness.should == "You're getting sadder" + movie_search.add_to_movie_list(sad_movie) + movie_search.calculate_happiness.should == "You're getting sadder" end it "should take the average of years that are the same and determine slope" do - movie_search7 = MovieJson.new - movie_search7.add_to_movie_list(movie) + # movie_search7 = MovieJson.new + movie_search.add_to_movie_list(movie) Api.stub(:search_by_title) {sad_movie} - movie_search7.add_to_movie_list(sad_movie) + movie_search.add_to_movie_list(sad_movie) Api.stub(:search_by_title) {happy_movie} - movie_search7.add_to_movie_list(happy_movie) - movie_search7.calculate_happiness.should == "You're getting sadder" + movie_search.add_to_movie_list(happy_movie) + movie_search.calculate_happiness.should == "You're getting sadder" end end end From 69448dc15d10850f5b59dd0e81b89c325e86684f Mon Sep 17 00:00:00 2001 From: noahpatterson Date: Wed, 25 Dec 2013 09:05:05 -0700 Subject: [PATCH 8/8] simplify calculate happiness. add sort_years, average_rating_by_year, and movie_rating_slope methods --- movie_json.rb | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/movie_json.rb b/movie_json.rb index 69a47ac..2692db9 100644 --- a/movie_json.rb +++ b/movie_json.rb @@ -77,14 +77,21 @@ def average_year avg_year.to_i end + def sort_years(movies) + movies.map{ |movie| movie.year }.sort + end + + def average_rating_by_year(movie_hash_by_year,year) + movies = movie_hash_by_year[year] + movies.map { |movie| movie.score }.reduce(:+) / movies.length + end + + def movie_rating_slope(years,movie_hash_by_year) + years[0] - years[-1] != 0 ? (average_rating_by_year(movie_hash_by_year,years[-1]) - average_rating_by_year(movie_hash_by_year,years[0])).to_f / (years[-1] - years[0]).to_f : 0 + end + def calculate_happiness - year_avg = {} - @movies.each { |movie| year_avg[movie.year] = [] } - @movies.each { |movie| year_avg[movie.year].push(movie.score) } - years = @movies.map { |movie| movie.year}.sort - oldest_year_avg = year_avg[years[0]].reduce(:+).to_f / year_avg[years[0]].size - youngest_year_avg = year_avg[years[-1]].reduce(:+).to_f / year_avg[years[-1]].size - slope = years[0] - years[-1] != 0 ? (youngest_year_avg - oldest_year_avg).to_f / (years[-1] - years[0]).to_f : 0 + slope = movie_rating_slope(sort_years(@movies),@movies.group_by {|movie| movie.year}) if slope == 0 return "You're neutral" elsif slope > 0