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 level complete #10

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
18 changes: 11 additions & 7 deletions lib/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,25 @@
require_relative "./movie"
class Api

APIKEY="4t6456xa33z8qhcqyuqgnkjh"
APIKEY="u2wh4hkgt7xkuqh6bhgm7f83"

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"]
)
begin
Movie.new(id: struct.id.to_i,
title: struct.title,
year: struct.year,
score: struct.ratings["critics_score"]
)
rescue
Copy link
Member

Choose a reason for hiding this comment

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

I'd rather you be more explicit about catching that there were no movies returned. Here you are returning nil if there was any exception.

nil
end
end


def self.get_url_as_json(url)
JSON.parse(open(url).read)
JSON.parse(open(url, 'User-Agent' => 'Ruby').read)
end

end
22 changes: 22 additions & 0 deletions lib/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class User
attr_reader :name, :searches

def initialize(name)
@name = name
@searches = []
end

def add_to_searches(movie)
@searches << movie
end

def rating
movie_scores = searches.map { |movie| movie.score }
movie_scores.inject { |sum, el| sum + el } / searches.size
end

def average_year
movie_years = searches.map { |movie| movie.year }
movie_years.inject { |sum, el| sum + el } / searches.size
end
end
27 changes: 25 additions & 2 deletions movie_json.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,35 @@
require_relative "lib/movie"
require_relative "lib/api"
require_relative "lib/user"

def start_movie_finder
puts "OH HAI. What's your name?"
@user = User.new(gets)
end

def find_movie
puts "OH HAI. Search?"
puts "Add a movie you really like, #{@user.name}"
movie_title = gets
movie = Api.search_by_title(movie_title)
puts "Found: #{movie.title}. Score: #{movie.score}"
@user.add_to_searches(movie)
if movie
puts "Found: #{movie.title}. Score: #{movie.score}"
puts "Your average movie rating is: #{@user.rating}"
else
puts "Movie not found."
end
end

def show_movie_searches
puts "============================"
puts "Your final average movie rating score is: #{@user.rating}"
puts "The average year for the movies you have liked is: #{@user.average_year}"
puts "============================"
puts "Your search history:"
@user.searches.each { |movie| puts movie.title }
end

start_movie_finder
find_movie

while true do
Expand All @@ -16,6 +38,7 @@ def find_movie
if answer == "Y"
find_movie
else
show_movie_searches
break
end
end
31 changes: 24 additions & 7 deletions spec/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,32 @@
movie.title.should eq("Forrest Gump")
end

it "should return the score" do
movie.score.should eq(71)
end
context "when movie is found" do

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 "when movie is not found" do

before do
Api.stub(:get_url_as_json) { JSON.parse(File.read("spec/fixtures/no_movie.json")) }
end

let(:no_movie_found) { Api.search_by_title("No movies exists") }

it "doesn't raise an error" do
expect{:no_movie_found}.to_not raise_error(NoMethodError)
end
end

end
1 change: 1 addition & 0 deletions spec/fixtures/no_movie.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"total":0,"movies":[],"links":{"self":"http://api.rottentomatoes.com/api/public/v1.0/movies.json?q=top+tomatoes&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}"}
45 changes: 45 additions & 0 deletions spec/user_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
require_relative "../lib/user"
require_relative "../lib/movie"
describe User do

let(:user) { User.new("Ralph") }
let(:movie) { Movie.new(id: "1", title: "Some movie", year: 1985, score: 80) }
let(:another_movie) { Movie.new(id: "2", title: "Another movie", year: 2000, score: 60) }

context "when user is initialized" do

it "has a name" do
expect(user.name).to eq("Ralph")
end

it "has an empty list of movie searches" do
expect(user.searches).to eq([])
end

it "can calculate an average movie rating score" do
user.add_to_searches(movie)
user.add_to_searches(another_movie)
expect(user.rating).to eq(70)
end

it "can calculate the average year of movies liked" do
user.add_to_searches(movie)
user.add_to_searches(another_movie)
expect(user.average_year).to eq(1992)
end
end

context "when a searching" do

it "records a user's search" do
user.add_to_searches(movie)
expect(user.searches.first).to eq(movie)
end

it "records multiple searches" do
user.add_to_searches(movie)
user.add_to_searches(another_movie)
expect(user.searches).to eq([movie, another_movie])
end
end
end