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 5 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
2 changes: 1 addition & 1 deletion lib/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ 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,
Movie.build(id: struct.id.to_i,
title: struct.title,
year: struct.year,
score: struct.ratings["critics_score"]
Expand Down
20 changes: 20 additions & 0 deletions lib/movie.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,30 @@
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)
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
16 changes: 11 additions & 5 deletions movie_json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,25 @@
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}"
rescue NoMethodError
puts "Not found."
end
end

find_movie
puts "The average rating 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 "The average rating is #{Movie.average_rating}."
else
break
end
Expand Down
4 changes: 4 additions & 0 deletions spec/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@
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
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 inherit from vehicle' 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