diff --git a/lib/api.rb b/lib/api.rb index a8d499c..3f0a558 100644 --- a/lib/api.rb +++ b/lib/api.rb @@ -1,24 +1,42 @@ require "open-uri" require "json" require "ostruct" -require_relative "./movie" +require_relative "movie" +require_relative "movie_history" + class Api APIKEY="4t6456xa33z8qhcqyuqgnkjh" def self.search_by_title(title) + MovieHistory.add_search(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"] - ) + full_json = get_url_as_json(url) + + if found?(full_json) + struct = OpenStruct.new(full_json.fetch("movies").first) + movie = Movie.new( id: struct.id.to_i, + title: struct.title, + year: struct.year, + score: struct.ratings["critics_score"] ) + puts "Found: #{movie.title}. Score: #{movie.score}" + Movie.add_movies(movie) + movie + else + empty_search(title) + end end + def self.found?( json ) + json.fetch("total").zero? ? false : true + end def self.get_url_as_json(url) JSON.parse(open(url).read) end + def self.empty_search( title ) + puts "Not found: #{title}" + end end diff --git a/lib/movie.rb b/lib/movie.rb index 167a23e..7b33b5d 100644 --- a/lib/movie.rb +++ b/lib/movie.rb @@ -1,6 +1,55 @@ +require_relative 'slope' + class Movie attr_reader :id, :title, :year, :score + + class << self + attr_reader :movies + end + + def self.add_movies(movie) + @movies ||= [] + movies << movie + end + + def self.average(method) + total = movies.inject(0) { | sum, movie | sum + movie.send(method) } + ( total / movies.length ).round + end + + def self.average_score + average(:score) + end + + def self.average_year + average(:year) + end + + def self.user_satisfaction + return "Happier" if slope > 0 + return "Maddier" if slope < 0 + end + + def self.slope + Slope.calculate( y1: newest_movie.score, + y2: oldest_movie.score, + x1: newest_movie.year, + x2: oldest_movie.year ) + end + + def self.oldest_movie + sorted_movies.first + end + + def self.newest_movie + sorted_movies.last + end + + def self.sorted_movies + sorted_movies = movies.sort_by { | movie | movie.year } + end + def initialize(hash={}) @id = hash.fetch(:id) @title = hash.fetch(:title) diff --git a/lib/movie_history.rb b/lib/movie_history.rb new file mode 100644 index 0000000..c206d61 --- /dev/null +++ b/lib/movie_history.rb @@ -0,0 +1,11 @@ +class MovieHistory + + class << self + attr_reader :searches + end + + def self.add_search(title) + @searches ||= [] + @searches << title + end +end \ No newline at end of file diff --git a/lib/slope.rb b/lib/slope.rb new file mode 100644 index 0000000..0bd56f2 --- /dev/null +++ b/lib/slope.rb @@ -0,0 +1,5 @@ +class Slope + def self.calculate( values={} ) + (( values[:y1] - values[:y2] ) / ( values[:x1] - values[:x2] ).to_f).round(3) + end +end \ No newline at end of file diff --git a/movie_json.rb b/movie_json.rb index d8a91d7..50f9a44 100644 --- a/movie_json.rb +++ b/movie_json.rb @@ -2,20 +2,19 @@ require_relative "lib/api" 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] + puts "Add a movie you really like" + movie_title = gets.chomp + Api.search_by_title(movie_title) + puts "Search Again? (Y/N)" + answer = gets.chomp.upcase[0] if answer == "Y" find_movie else - break + puts "The score average of the movies you added is: #{Movie.average_score}" + exit end end + +find_movie + + diff --git a/spec/api_spec.rb b/spec/api_spec.rb index 9014106..5c5a952 100644 --- a/spec/api_spec.rb +++ b/spec/api_spec.rb @@ -3,25 +3,71 @@ describe Api do - let(:movie) { Api.search_by_title("Forrest Gump") } + context "Existing Movie" do - before do - Api.stub(:get_url_as_json) { JSON.parse(File.read("spec/fixtures/forrest.json")) } - end + let(:movie) { Api.search_by_title("Forrest Gump") } - it "should search for movies" do - movie.title.should eq("Forrest Gump") - 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) + 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 end - it "should return the id" do - movie.id.should eq(10036) + context "Movie search" do + + it "should find the movie" do + json = JSON.parse(File.read("spec/fixtures/forrest.json")) + Api.found?(json).should eq(true) + end + + it "should not find the movie" do + json = JSON.parse(File.read("spec/fixtures/loquillo.json")) + Api.found?(json).should eq(false) + end + + it "should raise error when empty search" do + Api.stub(:get_url_as_json) { JSON.parse(File.read("spec/fixtures/loquillo.json")) } + expect { + Api.search_by_title("MOVIE NOT FOUND") + }.to_not raise_error + end end - it "should return the year" do - movie.year.should eq(1994) + context "Search History" do + + before do + MovieHistory.searches.clear + Api.stub(:get_url_as_json) { JSON.parse(File.read("spec/fixtures/forrest.json")) } + found_searches = [ "Forrest Gump", "Titanic", "Click" ] + found_searches.each do | title | + Api.search_by_title( title ) + end + + Api.stub(:get_url_as_json) { JSON.parse(File.read("spec/fixtures/loquillo.json")) } + not_found_searches = [ "Nothing found", "llllll" ] + not_found_searches.each do | title | + Api.search_by_title( title ) + @user_searches = found_searches + not_found_searches + end + end + + it "should show the user's search history" do + MovieHistory.searches.should eq(@user_searches) + end end end diff --git a/spec/fixtures/loquillo.json b/spec/fixtures/loquillo.json new file mode 100644 index 0000000..876aff7 --- /dev/null +++ b/spec/fixtures/loquillo.json @@ -0,0 +1 @@ +{"total":0,"movies":[],"links":{"self":"http://api.rottentomatoes.com/api/public/v1.0/movies.json?q=loquillo&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}"} \ No newline at end of file diff --git a/spec/movie_history_spec.rb b/spec/movie_history_spec.rb new file mode 100644 index 0000000..0981061 --- /dev/null +++ b/spec/movie_history_spec.rb @@ -0,0 +1,15 @@ +require_relative '../lib/movie_history.rb' + +describe MovieHistory do + before do + MovieHistory.searches.clear if MovieHistory.searches + end + + it "should return the history" do + user_searches = ["Forrest Gump", "Not Found", "Titanic", "Click"] + user_searches.each do | search | + MovieHistory.add_search(search) + end + MovieHistory.searches.should eq(user_searches) + end +end \ No newline at end of file diff --git a/spec/movie_spec.rb b/spec/movie_spec.rb index 088bd37..d8a5b2f 100644 --- a/spec/movie_spec.rb +++ b/spec/movie_spec.rb @@ -8,5 +8,53 @@ movie.year.should eq(1998) movie.score.should eq(50) end - + + context "Adding movies and operations with them" do + + before do + Movie.movies.clear if Movie.movies + movie1 = Movie.new(id: "the-id", title: "the-title", year: 1998, score: 50) + movie2 = Movie.new(id: 32 , title: "I'm 132" , year: 2005, score: 100) + movie3 = Movie.new(id: 15500 , title: "Dogs" , year: 2013, score: 88) + movie4 = Movie.new(id: "the-id", title: "Titanic" , year: 1998, score: 110) + movie5 = Movie.new(id: "the-id", title: "2012" , year: 2012, score: 77) + + movies = [ movie1, movie2, movie3, movie4, movie5 ] + movies.each { | movie | Movie.add_movies(movie) } + end + + it "should add movies the user likes" do + Movie.movies.length.should eq(5) + end + + it "should calculate the average of the movies score" do + Movie.average_score.should eq(85) + end + + it "should calculate the average of the movies year" do + Movie.average_year.should eq(2005) + end + end + + context "First and last year" do + it "should obtain the movie with the oldest year" do + movie = Movie.oldest_movie + movie.title.should eq("the-title") + end + + it "should obtain the movie with the newest year" do + movie = Movie.newest_movie + movie.title.should eq("Dogs") + end + end + + context "Sloping" do + it "should calculate the slope from the first year to the last year" do + Movie.slope.should eq(2.533) + end + + it "should determine if the user is getting happier" do + Movie.user_satisfaction.should eq("Happier") + end + end end diff --git a/spec/slope_spec.rb b/spec/slope_spec.rb new file mode 100644 index 0000000..6a0ea2e --- /dev/null +++ b/spec/slope_spec.rb @@ -0,0 +1,9 @@ +require_relative '../lib/slope.rb' + +describe Slope do + it "should calculate the slope of line" do + values = { x1: 2015, x2: 1990, + y1: 50, y2: 44 } + Slope.calculate(values).should eq(0.24) + end +end \ No newline at end of file