-
Notifications
You must be signed in to change notification settings - Fork 25
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 level finished #18
Open
ardder
wants to merge
3
commits into
RubyoffRails:master
Choose a base branch
from
ardder: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 all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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 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 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,40 @@ | ||
class MovieList | ||
attr_reader :movies | ||
|
||
def initialize(movies = []) | ||
@movies = movies | ||
end | ||
|
||
def add(movie) | ||
if @movies.include?(movie) | ||
false | ||
else | ||
@movies << movie | ||
true | ||
end | ||
end | ||
|
||
def average_score | ||
@movies.inject(0.0) {|sum, movie| sum + movie.score} / @movies.count | ||
end | ||
|
||
def average_year | ||
@movies.inject(0.0) {|sum, movie| sum + movie.year} / @movies.count | ||
end | ||
|
||
def slope | ||
start_year = @movies.min_by {|movie| movie.year}.year | ||
end_year = @movies.max_by {|movie| movie.year}.year | ||
|
||
return nil if start_year == end_year | ||
|
||
movies_in_start_year = @movies.select {|movie| movie.year == start_year} | ||
movies_in_end_year = @movies.select {|movie| movie.year == end_year} | ||
|
||
(MovieList.new(movies_in_end_year).average_score - MovieList.new(movies_in_start_year).average_score) / (end_year - start_year).to_f | ||
end | ||
|
||
def titles | ||
@movies.map(&:title).join(', ') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice! :) |
||
end | ||
end |
This file contains 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,46 @@ | ||
require_relative "lib/movie" | ||
require_relative "lib/movie_list" | ||
require_relative "lib/api" | ||
|
||
def find_movie | ||
puts "OH HAI. Search?" | ||
movie_title = gets | ||
puts "Add a movie you really like:" | ||
movie_title = gets.chomp | ||
movie = Api.search_by_title(movie_title) | ||
puts "Found: #{movie.title}. Score: #{movie.score}" | ||
|
||
if movie | ||
puts "Found: #{movie.title}. Score: #{movie.score}" | ||
movie | ||
else | ||
puts "Movie with title: \"#{movie_title}\" not found" | ||
nil | ||
end | ||
end | ||
|
||
find_movie | ||
movie_list = MovieList.new | ||
|
||
while true do | ||
puts "Search Again (Y/N)" | ||
answer = gets.upcase[0] | ||
if answer == "Y" | ||
find_movie | ||
else | ||
break | ||
movie = find_movie | ||
|
||
if movie | ||
puts "[#{movie.title}] is already on your list!" unless movie_list.add(movie) | ||
|
||
slope = movie_list.slope | ||
mood = if slope == nil || slope == 0 | ||
"I can't determine your mood, maybe add more movie?" | ||
elsif slope < 0 | ||
"I think you are getting madder (index: #{slope.round(2).to_s})" | ||
else | ||
"I think you are getting happier (index: #{slope.round(2).to_s})" | ||
end | ||
|
||
puts "-" * 80 | ||
puts "Your favorite movies: #{movie_list.titles}" | ||
puts "Average score: #{movie_list.average_score.round(2)}" | ||
puts mood | ||
puts "-" * 80 | ||
end | ||
|
||
puts "Add more? (Y/N)" | ||
answer = gets.upcase[0] | ||
break if answer == "N" | ||
end |
This file contains 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 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,10 @@ | ||
{ | ||
"total":0, | ||
"movies":[ | ||
|
||
], | ||
"links":{ | ||
"self":"http://api.rottentomatoes.com/api/public/v1.0/movies.json?q=blahblah&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}" | ||
} |
This file contains 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,62 @@ | ||
require_relative '../lib/movie' | ||
require_relative '../lib/movie_list' | ||
|
||
describe MovieList do | ||
|
||
before(:each) do | ||
@movie_list = MovieList.new | ||
@movie_1 = Movie.new(id: '1', title: "movie-1", year: 2000, score: 50) | ||
@movie_2 = Movie.new(id: '2', title: "movie-2", year: 2001, score: 60) | ||
@movie_3 = Movie.new(id: '3', title: "movie-3", year: 2002, score: 85) | ||
|
||
@movie_list.add(@movie_1) | ||
@movie_list.add(@movie_2) | ||
@movie_list.add(@movie_3) | ||
end | ||
|
||
it "should keep track of the movies we added" do | ||
@movie_list.movies.should eq([@movie_1, @movie_2, @movie_3]) | ||
end | ||
|
||
it "should calculate the average score" do | ||
@movie_list.average_score.should eq((50+60+85).to_f/3) | ||
end | ||
|
||
it "should calculate the average year" do | ||
@movie_list.average_year.should eq((2000+2001+2002).to_f/3) | ||
end | ||
|
||
it "should display movie titles" do | ||
@movie_list.titles.should eq("movie-1, movie-2, movie-3") | ||
end | ||
|
||
it "should not add duplicated movie" do | ||
@movie_list.add(@movie_1) | ||
@movie_list.movies.count.should eq(3) | ||
end | ||
|
||
describe "#slope" do | ||
it "should calculate the slope of rating correctly" do | ||
@movie_4 = Movie.new(id: '4', title: 'movie-4', year: 2000, score: 60) | ||
@movie_5 = Movie.new(id: '5', title: 'movie-5', year: 2010, score: 80) | ||
@movie_6 = Movie.new(id: '6', title: 'movie-6', year: 2010, score: 90) | ||
|
||
@movie_list.add(@movie_4) | ||
@movie_list.add(@movie_5) | ||
@movie_list.add(@movie_6) | ||
|
||
score_of_2000 = (50+60).to_f / 2 | ||
score_of_2010 = (80+90).to_f / 2 | ||
|
||
@movie_list.slope.should eq((score_of_2010 - score_of_2000) / (2010 - 2000).to_f) | ||
end | ||
|
||
it "should return nil if all the movies are in the same year" do | ||
movie_1 = Movie.new(id: '1', title: "movie-1", year: 2000, score: 50) | ||
movie_2 = Movie.new(id: '2', title: "movie-2", year: 2000, score: 60) | ||
movie_list = MovieList.new([movie_1, movie_2]) | ||
|
||
movie_list.slope.should be_nil | ||
end | ||
end | ||
end |
This file contains 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
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.
Later we use this method (add) as:
This means this method is responsible for two things:
There are exceptions to this rule, but I think Command/Query separation is a good one to follow. More: http://martinfowler.com/bliki/CommandQuerySeparation.html & http://tony.pitluga.com/2011/08/04/command-query-separation-in-ruby.html
In this specific case, I might add a "contains?" method
thoughts? what do you think?
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.
Thanks for the links about Command/Query separation!
The idea of
MovieList#add
is actually derived fromActiveRecord::Base#save
. I want to use the return value (true/false) to let the user know if he adds the movie successfully or not.After reading the links, I think that separating the two idea is a better and cleaner design. But inside the
MovieList#add
, I still want to check if the argument is duplicated or not. Because I believe thatMovieList#add
should have the responsibility to protect the internal data from messed up by the user.When I try to implement the new method, how to handle bad argument comes into question. Should I just neglect it or throw an exception? Which one will you choose? Or, maybe you have a better idea? 😄
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.
sure, I can buy that -- if you want
MovieList#add
to not:in
MovieList#add
I would raise an exception if it is contained when you try to add it.