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

Panda level finished #18

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 5 additions & 1 deletion lib/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ 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_json = get_url_as_json(url).fetch("movies").first

return nil unless movie_json

struct = OpenStruct.new(movie_json)
Movie.new(id: struct.id.to_i,
title: struct.title,
year: struct.year,
Expand Down
4 changes: 4 additions & 0 deletions lib/movie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ def initialize(hash={})
@year = hash.fetch(:year)
@score = hash.fetch(:score)
end

def ==(movie)
return id == movie.id && title == movie.title && year == movie.year && score == movie.score
end
end
40 changes: 40 additions & 0 deletions lib/movie_list.rb
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)
Copy link
Member

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:

puts "[#{movie.title}] is already on your list!" unless movie_list.add(movie)

This means this method is responsible for two things:

  1. keeping the movies unique (prevent dupes)
  2. telling you if the movie was a dupe

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

if movie_list.contains? movie
  puts "[#{movie.title}] is already on your list!"
else
  movie_list.add movie
end

thoughts? what do you think?

Copy link
Author

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 from ActiveRecord::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 that MovieList#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? 😄

Copy link
Member

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:

  1. not add the item if it exists
  2. tell the program something bad happened if someone tried to add a dupe:

in MovieList#add I would raise an exception if it is contained when you try to add it.

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(', ')
Copy link
Member

Choose a reason for hiding this comment

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

nice! :)

end
end
45 changes: 35 additions & 10 deletions movie_json.rb
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
47 changes: 33 additions & 14 deletions spec/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,45 @@
require "ostruct"

describe Api do
context "Movie existed" 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)
it "should return the year" do
movie.year.should eq(1994)
end
end

it "should return the year" do
movie.year.should eq(1994)
context "Movie not existed" do

before do
Api.stub(:get_url_as_json) { JSON.parse(File.read("spec/fixtures/no_result.json")) }
end

it "should not raise exception" do
expect {
Api.search_by_title("NOTHINGFOUNDHERE")
}.to_not raise_error
end

it "should return nil" do
movie = Api.search_by_title("NOTHINGFOUNDHERE")
movie.should be_nil
end
end
end
10 changes: 10 additions & 0 deletions spec/fixtures/no_result.json
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}"
}
62 changes: 62 additions & 0 deletions spec/movie_list_spec.rb
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
6 changes: 6 additions & 0 deletions spec/movie_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,10 @@
movie.score.should eq(50)
end

it "should be equal to other movie with the same id, title, year, score" do
movie_1 = Movie.new(id: "1", title: "title1", year: 1998, score: 50)
movie_2 = Movie.new(id: "1", title: "title1", year: 1998, score: 50)

(movie_1 == movie_2).should be_true
end
end