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 all 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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Slope of line is `(y1 - y2) / (x1 - x2)` ... In this case, if my average rating

```
(45 - 50).to_f / (2012 - 1990).to_f
=> -0.22727272727272727
=> -0.22727272727272727
# getting madder
```

Expand Down
2 changes: 1 addition & 1 deletion lib/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require "json"
require "ostruct"
require_relative "./movie"
require_relative "./movie_library"
class Api

APIKEY="4t6456xa33z8qhcqyuqgnkjh"
Expand All @@ -16,7 +17,6 @@ def self.search_by_title(title)
)
end


def self.get_url_as_json(url)
JSON.parse(open(url).read)
end
Expand Down
43 changes: 43 additions & 0 deletions lib/movie_library.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class MovieLibrary

attr_reader :movies, :catalog, :slope
attr_accessor :slope
def initialize
@movies = []
@catalog = []
@slope = 0
end

def calculate_slope(catalog)
first = catalog.first
last = catalog.last
return 0 if catalog.count <= 1
@slope = (first.score - last.score).to_f / (last.year - first.year).to_f
end

def sort_by_year(movies)
@catalog = movies.sort { |movie,another_movie| movie.year <=> another_movie.year }
end

def average_rating_by_year(year)
average_rating all_movies.select {|movie| movie.year == year}
end

def average_rating(movies)
return 0 if count == 0
movies.inject(0.0) { |sum, movie| sum + movie.score } / movies.size
end

def add(movie)
movies << movie
calculate_slope sort_by_year(movies)
end

def all_movies
@movies
end

def count
movies.count
end
end
35 changes: 28 additions & 7 deletions movie_json.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,42 @@
require_relative "lib/movie"
require_relative "lib/movie_library"
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)
rescue NoMethodError
puts "Not found."
end
end

find_movie
movie_library = MovieLibrary.new
movie = find_movie
if movie
movie_library.add(movie)
puts "Found: #{movie.title}. Score: #{movie.score}"
end

while true do
puts "Search Again (Y/N)"
puts "Search Again (Y/N)"
answer = gets.upcase[0]
if answer == "Y"
find_movie
movie = find_movie
if movie
movie_library.add(movie)
puts "Found: #{movie.title}. Score: #{movie.score}"
end
else
puts "\n--------------------------"
puts "Summary for your Movie Library"
puts "-----------------------------"
movie_library.all_movies.each do |movie|
puts "ID: #{movie.id}\tMovie: #{movie.title}\tYear: #{movie.year} Score: #{movie.score}\tAverage: #{movie_library.average_rating_by_year(movie.year)}"
end
puts "Your library has a Happiness Index of: #{movie_library.slope}"
puts "Goodbye!!!"
break
end
end
37 changes: 21 additions & 16 deletions spec/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,30 @@

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.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 not raise an error when searching for a non-existent movie" do
expect { Api.search_by_title("NOTHINGFOUNDHERE") }.to_not raise_error
end

it "should return the year" do
movie.year.should eq(1994)
end
end
33 changes: 33 additions & 0 deletions spec/fixtures/2010.json

Large diffs are not rendered by default.

81 changes: 81 additions & 0 deletions spec/movie_library_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
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)}
let (:another_movie){Movie.new(id: "the-id", title: "the-title", year: 2012, score: 50)}
let (:yet_another_movie){Movie.new(id: "the-id", title: "the-title", year: 1990, score: 45)}
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 "#add" do

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

describe "#average_rating" do

it "should have no average rating when no movies are cataloged" do
movie_library.average_rating(Array.new).should eq(0)
end

it 'should calculate the average rating' do
movie_library.add(movie)
movie_library.average_rating(movie_library.all_movies).should eq(50)
end
end

describe "#average_rating_by_year" do

it 'should have no average yearly rating when no movies are cataloged' do
movie_library.average_rating_by_year(movie.year).should eq(0)
end

it 'should calculate the average rating by year when the library has' do
movie_library.add(movie)
movie_library.add(another_movie)
movie_library.add(yet_another_movie)
movie_library.average_rating_by_year(1998).should eq(50)
end
end

describe "#sort_by_year" do

it 'should sort the movies by year' do
movie_library.add(another_movie)
movie_library.add(yet_another_movie)
movie_library.catalog.should == [yet_another_movie, another_movie]
end
end

describe "#calculate_slope" do

it "should have no slope when the library only contains one movie" do
movie_library.add(movie)
movie_library.slope.should eq(0)
end
it 'should calculate the slope of the average ratings from the first year to the last year' do
movie_library.add(another_movie)
movie_library.add(yet_another_movie)
movie_library.slope.should eq(-0.22727272727272727)
end

end
end
25 changes: 17 additions & 8 deletions spec/movie_spec.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
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)
end

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
end

end