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

Movie JSON submission #26

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
745009b
Panda Level complete.
codeblahblah Feb 19, 2014
c7a7953
Added unrefactored Tiger Level.
codeblahblah Feb 19, 2014
0c115b4
Added refactored Tiger Level.
codeblahblah Feb 19, 2014
8df31e7
Added RSpec file as #expect syntax was failing. Movie.average_rating …
codeblahblah Feb 20, 2014
ccfed8f
Added Gemfiles.
codeblahblah Feb 20, 2014
910010d
Added partial yearly search.
codeblahblah Feb 20, 2014
3b54798
Added MovieLibrary concept.
codeblahblah Feb 20, 2014
32fddc6
Deleted MovieList class.
codeblahblah Feb 20, 2014
b770147
Added MovieLibrary#average_rating.
codeblahblah Feb 20, 2014
527e834
Code clean-up.
codeblahblah Feb 20, 2014
048dce5
In Ruby, dividing by zero doesn't always raise a ZeroDivisionError - …
codeblahblah Feb 20, 2014
d3a2f34
Removed unnecessary test from movie_spec.rb
codeblahblah Feb 20, 2014
86c4e24
General clean-up and regression testing.
codeblahblah Feb 20, 2014
b977711
Added yearly average method.
codeblahblah Feb 20, 2014
12f27b0
Refactored MovieLibrary#average_rating.
codeblahblah Feb 20, 2014
7f81c59
Further refactoring of MovieLibrary#average_rating.
codeblahblah Feb 21, 2014
5b786f0
Add slope calculation.
codeblahblah Feb 21, 2014
78d49fd
Fixed #calculate_slope. Using #catalog to handle all dependencies.
codeblahblah Feb 22, 2014
cb1e2c0
Added program summary and one movie slope test.
codeblahblah Feb 22, 2014
f9814fd
Removed the use of unless as a 1 statement modifier.
codeblahblah Feb 25, 2014
a93af62
Refactored the use of instance variables and rename the catalog method.
codeblahblah Feb 25, 2014
10a00ea
Removed verbose calls to catalog.
codeblahblah Feb 25, 2014
2e367b8
Added fixes inline with renamed methods
codeblahblah Apr 3, 2014
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source 'http://rubygems.org'


gem 'rspec'
18 changes: 18 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
GEM
remote: http://rubygems.org/
specs:
diff-lcs (1.2.5)
rspec (2.14.1)
rspec-core (~> 2.14.0)
rspec-expectations (~> 2.14.0)
rspec-mocks (~> 2.14.0)
rspec-core (2.14.7)
rspec-expectations (2.14.5)
diff-lcs (>= 1.1.3, < 2.0)
rspec-mocks (2.14.5)

PLATFORMS
ruby

DEPENDENCIES
rspec
20 changes: 20 additions & 0 deletions lib/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,40 @@
require "json"
require "ostruct"
require_relative "./movie"
require_relative "./movie_list"
class Api

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)
search_by_year(struct.year)
Movie.new(id: struct.id.to_i,
title: struct.title,
year: struct.year,
score: struct.ratings["critics_score"]
)
end

def self.search_by_year(year)
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 really know why this method exists.

url = "http://api.rottentomatoes.com/api/public/v1.0/movies.json?apikey=#{APIKEY}&q=#{URI.encode(year.to_s)}&page_limit=49"
movies_in_year = get_url_as_json(url)
movies_in_year.fetch('movies').each do |movie|
# Movie.build(id: movie.fetch('id').to_i,
# title: movie.fetch('title'),
# year: movie.fetch('year'),
# score: movie.fetch('ratings').fetch('critics_score')
# )
# end
if movie.fetch('year') == year
puts "ID: #{movie.fetch('id').to_i}"
puts "Title: #{movie.fetch('title')}"
puts "Year: #{movie.fetch('year')}"
puts "Rating: #{movie.fetch('ratings').fetch('critics_score')}"
end
end
end

def self.get_url_as_json(url)
JSON.parse(open(url).read)
Expand Down
22 changes: 22 additions & 0 deletions lib/movie.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
require_relative './movie_library'
class Movie

@@movies = []
Copy link
Member

Choose a reason for hiding this comment

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

In this case, I don't think using a class variable makes 100% sense.

Instead, you could have the caller keep track of the MovieLibrary and help simplify this code and your tests.

Copy link
Member

Choose a reason for hiding this comment

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

(actually, looks like you just need to remove old code from here)

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)
MovieLibrary.new.register(self)
end

def self.build(args)
movie = Movie.new(args)
@@movies << movie
movie
end

def self.average_rating
all_movies.inject(0.0) { |sum, movie| sum + movie.score } / all_movies.size
end

def self.count
@@movies.count
end

def self.all_movies
@@movies
end

end
23 changes: 23 additions & 0 deletions lib/movie_library.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class MovieLibrary

attr_reader :movies
def initialize
@movies = []
end

def average_rating
all_movies.inject(0.0) { |sum, movie| sum + movie.score } / all_movies.size
end

def register(movie)
@movies << movie
end

def all_movies
@movies
end

def count
movies.count
end
end
17 changes: 12 additions & 5 deletions movie_json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,26 @@
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}"
begin
puts "OH HAI. Add a movie you really like?"
movie_title = gets
movie = Api.search_by_title(movie_title)
puts "Added: #{movie.title}. Score: #{movie.score}"
puts "Average score for the year: #{Movie.average_rating}"
rescue NoMethodError
puts "Not found."
end
end

find_movie
puts "The average rating for all movies searched is #{Movie.average_rating}."

while true do
puts "Search Again (Y/N)"
puts "Search Again (Y/N)"
answer = gets.upcase[0]
if answer == "Y"
find_movie
puts "Average score for the year: #{Movie.average_rating}"
else
break
end
Expand Down
40 changes: 24 additions & 16 deletions spec/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,33 @@

describe Api do

let(:movie) { Api.search_by_title("Forrest Gump") }
context "A 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)
end

it "should return the year" do
movie.year.should eq(1994)
end
it "should return the year" do
movie.year.should eq(1994)
end

it "should not raise an error when searching for a non-existent movie" do
expect { Api.search_by_title("NOTHINGFOUNDHERE") }.to_not raise_error
end
end

context ""
end
33 changes: 33 additions & 0 deletions spec/fixtures/2010.json

Large diffs are not rendered by default.

40 changes: 40 additions & 0 deletions spec/movie_library_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require 'rspec'
require_relative "../lib/movie"
require_relative "../lib/movie_library"
describe MovieLibrary do

let(:movie_library) {MovieLibrary.new}
let (:movie){Movie.new(id: "the-id", title: "the-title", year: 1998, score: 50)}
describe "#new" do

it 'should be a movie library object ' do
movie_library.is_a?(MovieLibrary).should be_true
end

it 'should have no movies when initialized' do
movie_library.count.should eq(0)
end
end

describe "#register" do

it 'should add a new movie to the library whenever a search happens' do
movie_library.register(movie)
expect(movie_library.all_movies).to include (movie)
end
it 'should increase the movie count' do
movie_library.register(movie)
movie_library.count.should eq(1)
end
end

describe "#average_rating" do
it "should have no average rating initially" do
movie_library.average_rating.should eq(0)
end

xit 'should calculate the average rating' do

end
end
end
52 changes: 45 additions & 7 deletions spec/movie_spec.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,50 @@
require 'rspec'
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)
describe "#new" do

let (:movie){Movie.new(id: "the-id", title: "the-title", year: 1998, score: 50)}

it "should store the title, year, and score" do
movie.id.should eq("the-id")
movie.title.should eq("the-title")
movie.year.should eq(1998)
movie.score.should eq(50)
end

it 'should a movie object' do
movie.is_a?(Movie).should be_true
end

it 'should not change the movie count' do
Movie.count.should eq(0)
end
end

describe ".build" do

it "records the movie when I build it" do
movie = Movie.build(id: 10020, title: 'Pirates of the Caribbean: The Curse of the Black Pearl', year: 2003, score: 79)
expect(Movie.all_movies).to include (movie)
end

it "should increment the movie count" do
expect{Movie.build(id: 10036, title: 'Forrest Gump', year: 1994, score: 71)}.to change{Movie.count}.from(1).to(2)
end
end

describe ".average_rating" do
it "should have no average rating initially" do
#how do you mock this scenario
end

it 'should calculate the average rating while ignoring the Movie#build calls above' do
#how do you change scope for RSpec object. Or rather how do you reset the Movie object instance variables?
Copy link
Member

Choose a reason for hiding this comment

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

Movie.new will always give you a new object.

end

it "should calculate the average rating for Forrest Gump and Pirates of the...as 75 " do
Movie.average_rating.should eq(75)
end
end

end