From 19621f9cb79bc97f271a6c8bd0a2ed75522a38fb Mon Sep 17 00:00:00 2001 From: Loc Pru Date: Tue, 23 Jul 2013 00:24:51 -0500 Subject: [PATCH 1/5] Panda level --- lib/api.rb | 25 ++++++++++++++++------ movie_json.rb | 9 ++++---- spec/api_spec.rb | 54 +++++++++++++++++++++++++++++++++++------------- 3 files changed, 63 insertions(+), 25 deletions(-) diff --git a/lib/api.rb b/lib/api.rb index a8d499c..65aa3ef 100644 --- a/lib/api.rb +++ b/lib/api.rb @@ -8,17 +8,30 @@ 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"] - ) + 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 + 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/movie_json.rb b/movie_json.rb index d8a91d7..1971769 100644 --- a/movie_json.rb +++ b/movie_json.rb @@ -3,16 +3,15 @@ def find_movie puts "OH HAI. Search?" - movie_title = gets - movie = Api.search_by_title(movie_title) - puts "Found: #{movie.title}. Score: #{movie.score}" + movie_title = gets.chomp + Api.search_by_title(movie_title) + puts "Search Again? (Y/N)" end find_movie while true do - puts "Search Again (Y/N)" - answer = gets.upcase[0] + answer = gets.chomp.upcase[0] if answer == "Y" find_movie else diff --git a/spec/api_spec.rb b/spec/api_spec.rb index 9014106..462676c 100644 --- a/spec/api_spec.rb +++ b/spec/api_spec.rb @@ -3,25 +3,51 @@ 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 return the score" do - movie.score.should eq(71) - 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 id" do - movie.id.should eq(10036) + it "should return the year" do + movie.year.should eq(1994) + end end - it "should return the year" do - movie.year.should eq(1994) + context "Movie not found" do + + describe "should verify the presence of the movie" 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 + 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 end From de1c958659f429be6a666fb18d1e11d61293b98a Mon Sep 17 00:00:00 2001 From: Loc Pru Date: Tue, 23 Jul 2013 10:42:08 -0500 Subject: [PATCH 2/5] Eagle level --- lib/api.rb | 9 +++++++++ lib/movie.rb | 15 +++++++++++++++ movie_json.rb | 2 +- spec/api_spec.rb | 42 +++++++++++++++++++++++++++++++----------- spec/movie_spec.rb | 21 ++++++++++++++++++++- 5 files changed, 76 insertions(+), 13 deletions(-) diff --git a/lib/api.rb b/lib/api.rb index 65aa3ef..cf41063 100644 --- a/lib/api.rb +++ b/lib/api.rb @@ -2,11 +2,15 @@ require "json" require "ostruct" require_relative "./movie" + class Api APIKEY="4t6456xa33z8qhcqyuqgnkjh" def self.search_by_title(title) + @history ||= [] + @history << title + url = "http://api.rottentomatoes.com/api/public/v1.0/movies.json?apikey=#{APIKEY}&q=#{URI.encode(title)}&page_limit=1" full_json = get_url_as_json(url) @@ -17,6 +21,7 @@ def self.search_by_title(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) @@ -34,4 +39,8 @@ def self.get_url_as_json(url) def self.empty_search( title ) puts "Not found: #{title}" end + + def self.search_history + @history + end end diff --git a/lib/movie.rb b/lib/movie.rb index 167a23e..460b9f3 100644 --- a/lib/movie.rb +++ b/lib/movie.rb @@ -1,6 +1,21 @@ 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 + total_score = movies.inject(0) { | sum, movie | sum + movie.score } + ( total_score / movies.length ).round + end + def initialize(hash={}) @id = hash.fetch(:id) @title = hash.fetch(:title) diff --git a/movie_json.rb b/movie_json.rb index 1971769..9fd18f2 100644 --- a/movie_json.rb +++ b/movie_json.rb @@ -2,7 +2,7 @@ require_relative "lib/api" def find_movie - puts "OH HAI. Search?" + puts "Add a movie you really like" movie_title = gets.chomp Api.search_by_title(movie_title) puts "Search Again? (Y/N)" diff --git a/spec/api_spec.rb b/spec/api_spec.rb index 462676c..afd2150 100644 --- a/spec/api_spec.rb +++ b/spec/api_spec.rb @@ -28,19 +28,16 @@ end end - context "Movie not found" do + context "Movie search" do - describe "should verify the presence of the movie" 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 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 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 @@ -50,4 +47,27 @@ }.to_not raise_error end end + + context "Search History" do + + before do + Api.search_history.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 search history" do + Api.search_history.should eq(@user_searches) + end + end end diff --git a/spec/movie_spec.rb b/spec/movie_spec.rb index 088bd37..8d3168a 100644 --- a/spec/movie_spec.rb +++ b/spec/movie_spec.rb @@ -8,5 +8,24 @@ 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) + movies = [ movie1, movie2, movie3 ] + movies.each { | movie | Movie.add_movies(movie) } + end + + it "should add movies the user likes" do + Movie.movies.length.should eq(3) + end + + it "should calculate the average of the movies" do + Movie.average.should eq(79) + end + end end From d80f58c4b61af69a05f77570b243c0fb8d894595 Mon Sep 17 00:00:00 2001 From: Loc Pru Date: Tue, 23 Jul 2013 10:42:08 -0500 Subject: [PATCH 3/5] Eagle level --- lib/api.rb | 9 +++++++++ lib/movie.rb | 15 +++++++++++++++ movie_json.rb | 2 +- spec/api_spec.rb | 42 +++++++++++++++++++++++++++++++----------- spec/movie_spec.rb | 21 ++++++++++++++++++++- 5 files changed, 76 insertions(+), 13 deletions(-) diff --git a/lib/api.rb b/lib/api.rb index 65aa3ef..cf41063 100644 --- a/lib/api.rb +++ b/lib/api.rb @@ -2,11 +2,15 @@ require "json" require "ostruct" require_relative "./movie" + class Api APIKEY="4t6456xa33z8qhcqyuqgnkjh" def self.search_by_title(title) + @history ||= [] + @history << title + url = "http://api.rottentomatoes.com/api/public/v1.0/movies.json?apikey=#{APIKEY}&q=#{URI.encode(title)}&page_limit=1" full_json = get_url_as_json(url) @@ -17,6 +21,7 @@ def self.search_by_title(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) @@ -34,4 +39,8 @@ def self.get_url_as_json(url) def self.empty_search( title ) puts "Not found: #{title}" end + + def self.search_history + @history + end end diff --git a/lib/movie.rb b/lib/movie.rb index 167a23e..460b9f3 100644 --- a/lib/movie.rb +++ b/lib/movie.rb @@ -1,6 +1,21 @@ 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 + total_score = movies.inject(0) { | sum, movie | sum + movie.score } + ( total_score / movies.length ).round + end + def initialize(hash={}) @id = hash.fetch(:id) @title = hash.fetch(:title) diff --git a/movie_json.rb b/movie_json.rb index 1971769..9fd18f2 100644 --- a/movie_json.rb +++ b/movie_json.rb @@ -2,7 +2,7 @@ require_relative "lib/api" def find_movie - puts "OH HAI. Search?" + puts "Add a movie you really like" movie_title = gets.chomp Api.search_by_title(movie_title) puts "Search Again? (Y/N)" diff --git a/spec/api_spec.rb b/spec/api_spec.rb index 462676c..afd2150 100644 --- a/spec/api_spec.rb +++ b/spec/api_spec.rb @@ -28,19 +28,16 @@ end end - context "Movie not found" do + context "Movie search" do - describe "should verify the presence of the movie" 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 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 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 @@ -50,4 +47,27 @@ }.to_not raise_error end end + + context "Search History" do + + before do + Api.search_history.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 search history" do + Api.search_history.should eq(@user_searches) + end + end end diff --git a/spec/movie_spec.rb b/spec/movie_spec.rb index 088bd37..8d3168a 100644 --- a/spec/movie_spec.rb +++ b/spec/movie_spec.rb @@ -8,5 +8,24 @@ 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) + movies = [ movie1, movie2, movie3 ] + movies.each { | movie | Movie.add_movies(movie) } + end + + it "should add movies the user likes" do + Movie.movies.length.should eq(3) + end + + it "should calculate the average of the movies" do + Movie.average.should eq(79) + end + end end From b29f11b3ac8e17ea7879b719ba436651ee0e254f Mon Sep 17 00:00:00 2001 From: Loc Pru Date: Tue, 23 Jul 2013 21:17:08 -0500 Subject: [PATCH 4/5] Eagle level print average movie Rating on exit --- movie_json.rb | 12 ++++++------ spec/fixtures/loquillo.json | 1 + 2 files changed, 7 insertions(+), 6 deletions(-) create mode 100644 spec/fixtures/loquillo.json diff --git a/movie_json.rb b/movie_json.rb index 9fd18f2..5fbeebe 100644 --- a/movie_json.rb +++ b/movie_json.rb @@ -6,15 +6,15 @@ def find_movie movie_title = gets.chomp Api.search_by_title(movie_title) puts "Search Again? (Y/N)" -end - -find_movie - -while true do answer = gets.chomp.upcase[0] if answer == "Y" find_movie else - break + puts "The average of the movie you add is: #{Movie.average}" + exit end end + +find_movie + + 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 From 8ba3f30e7ad15d25fff0e6dc3c14c7e463826533 Mon Sep 17 00:00:00 2001 From: Loc Pru Date: Wed, 24 Jul 2013 15:21:27 -0500 Subject: [PATCH 5/5] Eagle level completed --- lib/api.rb | 18 +++++++---------- lib/movie.rb | 40 +++++++++++++++++++++++++++++++++++--- lib/movie_history.rb | 11 +++++++++++ lib/slope.rb | 5 +++++ movie_json.rb | 2 +- spec/api_spec.rb | 6 +++--- spec/movie_history_spec.rb | 15 ++++++++++++++ spec/movie_spec.rb | 37 +++++++++++++++++++++++++++++++---- spec/slope_spec.rb | 9 +++++++++ 9 files changed, 121 insertions(+), 22 deletions(-) create mode 100644 lib/movie_history.rb create mode 100644 lib/slope.rb create mode 100644 spec/movie_history_spec.rb create mode 100644 spec/slope_spec.rb diff --git a/lib/api.rb b/lib/api.rb index cf41063..3f0a558 100644 --- a/lib/api.rb +++ b/lib/api.rb @@ -1,25 +1,25 @@ 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) - @history ||= [] - @history << 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" 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"] ) + 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 @@ -39,8 +39,4 @@ def self.get_url_as_json(url) def self.empty_search( title ) puts "Not found: #{title}" end - - def self.search_history - @history - end end diff --git a/lib/movie.rb b/lib/movie.rb index 460b9f3..7b33b5d 100644 --- a/lib/movie.rb +++ b/lib/movie.rb @@ -1,3 +1,5 @@ +require_relative 'slope' + class Movie attr_reader :id, :title, :year, :score @@ -11,9 +13,41 @@ def self.add_movies(movie) movies << movie end - def self.average - total_score = movies.inject(0) { | sum, movie | sum + movie.score } - ( total_score / movies.length ).round + 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={}) 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 5fbeebe..50f9a44 100644 --- a/movie_json.rb +++ b/movie_json.rb @@ -10,7 +10,7 @@ def find_movie if answer == "Y" find_movie else - puts "The average of the movie you add is: #{Movie.average}" + puts "The score average of the movies you added is: #{Movie.average_score}" exit end end diff --git a/spec/api_spec.rb b/spec/api_spec.rb index afd2150..5c5a952 100644 --- a/spec/api_spec.rb +++ b/spec/api_spec.rb @@ -51,7 +51,7 @@ context "Search History" do before do - Api.search_history.clear + 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 | @@ -66,8 +66,8 @@ end end - it "should show the search history" do - Api.search_history.should eq(@user_searches) + it "should show the user's search history" do + MovieHistory.searches.should eq(@user_searches) end end end 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 8d3168a..d8a5b2f 100644 --- a/spec/movie_spec.rb +++ b/spec/movie_spec.rb @@ -16,16 +16,45 @@ 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) - movies = [ movie1, movie2, movie3 ] + 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(3) + 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 calculate the average of the movies" do - Movie.average.should eq(79) + 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