diff --git a/lib/api.rb b/lib/api.rb index a8d499c..3b4d595 100644 --- a/lib/api.rb +++ b/lib/api.rb @@ -3,22 +3,29 @@ require "ostruct" require_relative "./movie" class Api - APIKEY="4t6456xa33z8qhcqyuqgnkjh" + def self.nil_movie + Movie.new(id: 0, title: "NOTHINGFOUNDHERE", year: 0, score: 0) + end + def self.search_by_title(title) + return nil_movie if 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.ratings.nil? == false then + movie = Movie.new(id: struct.id.to_i, + title: struct.title, + year: struct.year, + score: struct.ratings["critics_score"] + ) + return movie + else + return nil_movie + end end - def self.get_url_as_json(url) JSON.parse(open(url).read) end - end diff --git a/lib/search_history.rb b/lib/search_history.rb new file mode 100644 index 0000000..b2c1372 --- /dev/null +++ b/lib/search_history.rb @@ -0,0 +1,68 @@ +require_relative "./movie" + +class Search_History + attr_reader :movies + + def initialize() + @movies = [] + end + + def add_search(movie) + @movies << movie if movie.title != "NOTHINGFOUNDHERE" + end + + def average_rating() + average = 0 + @movies.each do |movie| + average += movie.score + end + + return (average/@movies.count) + end + + def average_year_for_ratings() + year = 0 + @movies.each do |movie| + year += movie.year + end + + return (year/@movies.count).ceil + end + + def average_rating_for_year(year) + @rating = 0 + movie_count = 0 + @movies.each do |movie| + if movie.year == year then + @rating += movie.score + movie_count += 1 + end + end + + return (@rating/movie_count) + end + + def average_rating_for_max_year + average_rating_for_year(max_year) + end + + def average_rating_for_min_year + average_rating_for_year(min_year) + end + + def max_year + @movies.max_by(&:year).year + end + + def min_year + @movies.min_by(&:year).year + end + + def ratings_trend + if @movies.count > 1 then + (average_rating_for_max_year - average_rating_for_min_year) / (max_year - min_year) + else + 0 + end + end +end diff --git a/movie_json.rb b/movie_json.rb index d8a91d7..6644056 100644 --- a/movie_json.rb +++ b/movie_json.rb @@ -1,13 +1,23 @@ require_relative "lib/movie" require_relative "lib/api" +require_relative "lib/search_history" def find_movie - puts "OH HAI. Search?" + puts "Add a movie you really like" movie_title = gets movie = Api.search_by_title(movie_title) + @search_history.add_search(movie) puts "Found: #{movie.title}. Score: #{movie.score}" + puts "Average movie score: #{@search_history.average_rating}." + puts "Average movie year: #{@search_history.average_year_for_ratings}." + if @search_history.ratings_trend > 0 then + puts "You are getting happier" + elsif @search_history.ratings_trend < 0 then + puts "You are getting madder" + end end +@search_history = Search_History.new find_movie while true do diff --git a/spec/api_spec.rb b/spec/api_spec.rb index 9014106..868e0b7 100644 --- a/spec/api_spec.rb +++ b/spec/api_spec.rb @@ -2,26 +2,41 @@ require "ostruct" describe Api do + context "Missing Movie" do + describe "Empty Search" do + it "should not throw an error" do + expect { + Api.search_by_title("NOTHINGFOUNDHERE") + }.to_not raise_error + end + it "should have the title 'NOTHINGFOUNDHERE'" do + movie = Api.search_by_title("") + movie.title.should eq("NOTHINGFOUNDHERE") + end + end + end - let(:movie) { Api.search_by_title("Forrest Gump") } + context "Valid Movie" 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 + 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 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) + it "should return the year" do + movie.year.should eq(1994) + end end end diff --git a/spec/search_history_spec.rb b/spec/search_history_spec.rb new file mode 100644 index 0000000..59dcb62 --- /dev/null +++ b/spec/search_history_spec.rb @@ -0,0 +1,75 @@ +require_relative "../lib/search_history" +describe Search_History do + it "should store a search history" do + search_history = Search_History.new + search_history.add_search(Api.search_by_title("Forrest Gump")) + search_history.movies.count.should eq(1) + end + + it "should store latest seach history" do + search_history = Search_History.new + search_history.add_search(Api.search_by_title("Forrest Gump")) + search_history.movies.last.title.should eq("Forrest Gump") + end + + it "should get average rating for search history" do + search_history = Search_History.new + search_history.add_search(Api.search_by_title("Forrest Gump")) + search_history.add_search(Api.search_by_title("Joe Dirt")) + search_history.movies.count.should eq(2) + search_history.average_rating.should eq(41) + end + + it "should have the average year for the ratings" do + search_history = Search_History.new + search_history.add_search(Api.search_by_title("Forrest Gump")) + search_history.add_search(Api.search_by_title("Joe Dirt")) + search_history.average_year_for_ratings.should eq(1997) + end + + it "should have the average rating for the max year" do + search_history = Search_History.new + search_history.add_search(Api.search_by_title("Forrest Gump")) + search_history.add_search(Api.search_by_title("Joe Dirt")) + search_history.average_rating_for_max_year.should eq(11) + end + + it "should have the average rating for the min year" do + search_history = Search_History.new + search_history.add_search(Api.search_by_title("Forrest Gump")) + search_history.add_search(Api.search_by_title("Joe Dirt")) + search_history.add_search(Api.search_by_title("Ace Ventura")) + search_history.average_rating_for_min_year.should eq(58) + end + + it "should have the max year" do + search_history = Search_History.new + search_history.add_search(Api.search_by_title("Forrest Gump")) + search_history.add_search(Api.search_by_title("Joe Dirt")) + search_history.add_search(Api.search_by_title("Ace Ventura")) + search_history.max_year.should eq(2001) + end + + it "should have the min year" do + search_history = Search_History.new + search_history.add_search(Api.search_by_title("Forrest Gump")) + search_history.add_search(Api.search_by_title("Joe Dirt")) + search_history.add_search(Api.search_by_title("Ace Ventura")) + search_history.min_year.should eq(1994) + end + + it "should have the ratings trend" do + search_history = Search_History.new + search_history.add_search(Api.search_by_title("Forrest Gump")) + search_history.add_search(Api.search_by_title("Joe Dirt")) + search_history.add_search(Api.search_by_title("Ace Ventura")) + search_history.ratings_trend.should eq(-7) + end + + it "should not add invalid movies" do + search_history = Search_History.new + search_history.movies.count.should eq(0) + search_history.add_search(Api.search_by_title("Billy Maddison")) + search_history.movies.count.should eq(0) + end +end