From 603427309af9ee726c77a2cd9324489e3afe1111 Mon Sep 17 00:00:00 2001 From: Patil Varvarian Date: Thu, 10 Oct 2013 12:48:11 -0700 Subject: [PATCH] I am done: P,T and E --- lib/api.rb | 32 ++++++++++--------- lib/movie.rb | 16 ++++++---- movie_json.rb | 64 ++++++++++++++++++++++++++++++-------- spec/api_spec.rb | 46 +++++++++++++++++---------- spec/fixtures/forrest.json | 2 ++ spec/movie_spec.rb | 20 ++++++------ 6 files changed, 118 insertions(+), 62 deletions(-) diff --git a/lib/api.rb b/lib/api.rb index a8d499c..46e1061 100644 --- a/lib/api.rb +++ b/lib/api.rb @@ -4,21 +4,23 @@ require_relative "./movie" class Api - APIKEY="4t6456xa33z8qhcqyuqgnkjh" + APIKEY="4t6456xa33z8qhcqyuqgnkjh" - 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"] - ) - end - - - def self.get_url_as_json(url) - JSON.parse(open(url).read) - end + 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) + if struct.title != nil + Movie.new(id: struct.id.to_i, + title: struct.title, + year: struct.year, + score: struct.ratings["critics_score"] ) + else + + end +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..2c3f892 100644 --- a/lib/movie.rb +++ b/lib/movie.rb @@ -1,10 +1,14 @@ + class Movie - attr_reader :id, :title, :year, :score +attr_reader :id, :title, :year, :score + def initialize(hash={}) - @id = hash.fetch(:id) - @title = hash.fetch(:title) - @year = hash.fetch(:year) - @score = hash.fetch(:score) - end + @id = hash.fetch(:id) + @title = hash.fetch(:title) + @year = hash.fetch(:year) + @score = hash.fetch(:score) + end + end + diff --git a/movie_json.rb b/movie_json.rb index d8a91d7..b36bf00 100644 --- a/movie_json.rb +++ b/movie_json.rb @@ -1,21 +1,59 @@ require_relative "lib/movie" require_relative "lib/api" + +def average(movies, keys) + data = movies.map(&keys) + average = data.inject { |sum, rating| sum + rating }.to_f/data.size +end + +def calculate_slope(movies) + movies = movies.sort_by { |i| i.year } + slope = (movies.last.score - movies.first.score).to_f / + (movies.last.year - movies.first.year).to_f + if slope < 0 + puts slope + puts "You're getting madder" + elsif slope > 0 + puts slope + puts "You're getting better" + 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}" + puts "Hello name me a movie" + title = gets + movie = Api.search_by_title(title) + if movie != nil + puts "Found: #{movie.title}. Score: #{movie.score}" + movie + else + puts "sorry try again" + end end -find_movie +movies = [] + + while true + movies << find_movie + if movies.last == nil + movies.pop + else + puts "Average_Score: #{average(movies, :score)}, Average_Years: #{average(movies, :year)}" + end -while true do - puts "Search Again (Y/N)" - answer = gets.upcase[0] - if answer == "Y" - find_movie - else - break - end +puts "Search Again (Y/N)" +answer = gets.upcase[0] + if answer != "Y" + puts calculate_slope(movies) + break + end end + + + + + + + + diff --git a/spec/api_spec.rb b/spec/api_spec.rb index 9014106..6a5f724 100644 --- a/spec/api_spec.rb +++ b/spec/api_spec.rb @@ -1,27 +1,39 @@ require_relative "../lib/api" require "ostruct" -describe Api do +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")) } - 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 = Api.search_by_title("Forrest Gump") + 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 = Api.search_by_title("Forrest Gump") + 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 = Api.search_by_title("Forrest Gump") + movie.id.should eq(10036) + end + + + it "should return the year" do + movie = Api.search_by_title("Forrest Gump") + movie.year.should eq(1994) + end + + it "should return piggy when wrong movie" do + movie = Api.search_by_title("wofjewjfwpe") + movie.should eq(nil) + end - it "should return the year" do - movie.year.should eq(1994) - end end + diff --git a/spec/fixtures/forrest.json b/spec/fixtures/forrest.json index cbff0a3..f5136e6 100644 --- a/spec/fixtures/forrest.json +++ b/spec/fixtures/forrest.json @@ -25,6 +25,8 @@ + + diff --git a/spec/movie_spec.rb b/spec/movie_spec.rb index 088bd37..8383b36 100644 --- a/spec/movie_spec.rb +++ b/spec/movie_spec.rb @@ -1,12 +1,10 @@ require_relative "../lib/movie" -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") - movie.title.should eq("the-title") - movie.year.should eq(1998) - movie.score.should eq(50) - end - -end +describe Movie do + it "should return a movie when searched" do + movie = Movie.new(id: "the-id", title: "the-title", year: 1998, score: 50) + movie.id.should eq("the-id") + movie.title.should eq("the-title") + movie.year.should eq(1998) + movie.score.should eq(50) + end +end \ No newline at end of file