Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Panda + Tiger + Eagle #7

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
41 changes: 33 additions & 8 deletions lib/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,47 @@
require "ostruct"
require_relative "./movie"
class Api
@@search_history = []
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like class variables in most cases... This will prevent you from going multi-threaded in the future (as each thread will overwrite this variable)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, that is a left over artifact from before I implemented the Search_History class.


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"]
)
if title != "" then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could remove some duplication here... if you want to not-search if the user did blank, then maybe you could

def nil_movie
  Movie.new(id: 0, title: "NOTHINGFOUNDHERE", year: 0, score: 0)
end

def self.search_by_title(title)
  return nil_movie if title.blank?
  # regular stuff
  else
    return nil_movie
  end
end

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.ratings.nil? == false then
movie = Movie.new(id: struct.id.to_i,
title: struct.title,
year: struct.year,
score: struct.ratings["critics_score"]
)
@@search_history << movie
return movie
else
Movie.new(id: 0,
title: "NOTHINGFOUNDHERE",
year: 0,
score: 0
)
end
else
Movie.new(id: 0,
title: "NOTHINGFOUNDHERE",
year: 0,
score: 0
)
end
end


def self.get_url_as_json(url)
JSON.parse(open(url).read)
end

def self.get_search_history()
@@search_history
end

def self.reset_search_history()
@@search_history = []
end
end
68 changes: 68 additions & 0 deletions lib/search_history.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
require_relative "./movie"

class Search_History
attr_reader :movies

def initialize()
@movies = []
end

def add_search(movie)
@movies << movie
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
12 changes: 11 additions & 1 deletion movie_json.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
55 changes: 41 additions & 14 deletions spec/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,53 @@
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)
it "should return the year" do
movie.year.should eq(1994)
end

it "should be able to reset the search history" do
Api.reset_search_history
Api.get_search_history.count.should eq(0)
end
end

it "should return the year" do
movie.year.should eq(1994)
context "Search History" do
it "should store latest search history" do
movie = Api.search_by_title("Forrest Gump")
Api.get_search_history.count.should eq(1)
end
end
end
68 changes: 68 additions & 0 deletions spec/search_history_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
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
end