-
Notifications
You must be signed in to change notification settings - Fork 25
Movie JSON submission #26
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
Open
codeblahblah
wants to merge
23
commits into
RubyoffRails:master
Choose a base branch
from
codeblahblah:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 19 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
745009b
Panda Level complete.
codeblahblah c7a7953
Added unrefactored Tiger Level.
codeblahblah 0c115b4
Added refactored Tiger Level.
codeblahblah 8df31e7
Added RSpec file as #expect syntax was failing. Movie.average_rating …
codeblahblah ccfed8f
Added Gemfiles.
codeblahblah 910010d
Added partial yearly search.
codeblahblah 3b54798
Added MovieLibrary concept.
codeblahblah 32fddc6
Deleted MovieList class.
codeblahblah b770147
Added MovieLibrary#average_rating.
codeblahblah 527e834
Code clean-up.
codeblahblah 048dce5
In Ruby, dividing by zero doesn't always raise a ZeroDivisionError - …
codeblahblah d3a2f34
Removed unnecessary test from movie_spec.rb
codeblahblah 86c4e24
General clean-up and regression testing.
codeblahblah b977711
Added yearly average method.
codeblahblah 12f27b0
Refactored MovieLibrary#average_rating.
codeblahblah 7f81c59
Further refactoring of MovieLibrary#average_rating.
codeblahblah 5b786f0
Add slope calculation.
codeblahblah 78d49fd
Fixed #calculate_slope. Using #catalog to handle all dependencies.
codeblahblah cb1e2c0
Added program summary and one movie slope test.
codeblahblah f9814fd
Removed the use of unless as a 1 statement modifier.
codeblahblah a93af62
Refactored the use of instance variables and rename the catalog method.
codeblahblah 10a00ea
Removed verbose calls to catalog.
codeblahblah 2e367b8
Added fixes inline with renamed methods
codeblahblah File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| source 'http://rubygems.org' | ||
|
|
||
|
|
||
| gem 'rspec' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| class MovieLibrary | ||
|
|
||
| attr_reader :movies, :library, :slope | ||
| def initialize | ||
| @movies = [] | ||
| @library = [] | ||
| @slope = 0 | ||
| end | ||
|
|
||
| def calculate_slope(catalog) | ||
| first = catalog.first | ||
| last = catalog.last | ||
| return 0 if first == last | ||
| (first.score - last.score).to_f / (last.year - first.year).to_f | ||
| end | ||
|
|
||
| def sort_by_year(movies) | ||
| 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 catalog(movie) | ||
| @movies << movie | ||
| @library = sort_by_year(@movies) | ||
| @slope = calculate_slope(@library) | ||
| return @library | ||
| end | ||
|
|
||
| def all_movies | ||
| @movies | ||
| end | ||
|
|
||
| def count | ||
| movies.count | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| unless movie.nil? | ||
| movie_library.catalog(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 | ||
| unless movie.nil? | ||
| movie_library.catalog(movie) | ||
| puts "Found: #{movie.title}. Score: #{movie.score}" | ||
| end | ||
| else | ||
| puts "\n--------------------------" | ||
| puts "Summary for your Movie Library" | ||
| puts "-----------------------------" | ||
| movie_library.library.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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 "#catalog" do | ||
|
|
||
| it 'should add a new movie to the library whenever a search happens' do | ||
| movie_library.catalog(movie) | ||
| expect(movie_library.all_movies).to include (movie) | ||
| end | ||
| it 'should increase the movie count' do | ||
| movie_library.catalog(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.catalog(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.catalog(movie) | ||
| movie_library.catalog(another_movie) | ||
| movie_library.catalog(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.catalog(another_movie) | ||
| movie_library.catalog(yet_another_movie) | ||
| movie_library.library.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.catalog(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.catalog(another_movie) | ||
| movie_library.catalog(yet_another_movie) | ||
| movie_library.slope.should eq(-0.22727272727272727) | ||
| end | ||
|
|
||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are you using instance variables here?