-
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 - fix search results to not throw an error if movie not found #25
base: master
Are you sure you want to change the base?
Changes from all commits
83a9562
6243578
125667b
0f4b354
fbd0c61
95e7612
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
require_relative "api" | ||
|
||
class MovieClerk | ||
attr_reader :welcome_message, :taste_measurement | ||
|
||
def initialize | ||
@movies = [] | ||
@welcome_message = "Welcome to The Movie Clerk!" | ||
end | ||
|
||
def find_movie | ||
puts "Add a movie you like:" | ||
movie_title = gets | ||
search_by_title(movie_title) | ||
send_movie_details(@movies.last) | ||
end | ||
|
||
def analyze_taste | ||
last_two_movies = @movies.last(2) | ||
first_movie = last_two_movies.first | ||
last_movie = last_two_movies.last | ||
@taste_measurement = (first_movie.score - last_movie.score).to_f / (first_movie.year - last_movie.year).to_f | ||
if @taste_measurement < 0 | ||
puts "Your taste is diminishing over time." | ||
else | ||
puts "Your taste is improving over time." | ||
end | ||
end | ||
|
||
def perform_taste_analysis? | ||
@movies.size == 2 | ||
end | ||
|
||
def search_by_title(movie_title) | ||
@movies << Api.search_by_title(movie_title) | ||
end | ||
|
||
def send_movie_details(movie) | ||
puts "Found: #{movie.title}. Score: #{movie.score}" | ||
puts "Average score of #{movie_score_average.round(2)} for #{@movies.count} movies on your list." | ||
end | ||
|
||
def movie_score_average | ||
scores = @movies.map {|m| m.score} | ||
scores.inject(:+).to_f / scores.size | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,16 @@ | ||
require_relative "lib/movie" | ||
require_relative "lib/api" | ||
require_relative "lib/movie_clerk" | ||
|
||
def find_movie | ||
puts "OH HAI. Search?" | ||
movie_title = gets | ||
movie = Api.search_by_title(movie_title) | ||
puts "Found: #{movie.title}. Score: #{movie.score}" | ||
end | ||
|
||
find_movie | ||
clerk = MovieClerk.new | ||
puts clerk.welcome_message | ||
clerk.find_movie | ||
|
||
while true do | ||
puts "Search Again (Y/N)" | ||
puts "Search Again (Y/N)" | ||
answer = gets.upcase[0] | ||
if answer == "Y" | ||
find_movie | ||
else | ||
if answer == "N" | ||
break | ||
else | ||
clerk.find_movie | ||
clerk.analyze_taste if clerk.perform_taste_analysis? | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"total": 0, | ||
"movies": [], | ||
"links": { | ||
"self": "http://api.rottentomatoes.com/api/public/v1.0/movies.json?q=asfasffsafsafa&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}" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
require_relative '../lib/movie_clerk' | ||
|
||
describe MovieClerk do | ||
it "finds a movie" do | ||
clerk = MovieClerk.new | ||
clerk.search_by_title("pulp fiction").first.title.should eq("Pulp Fiction") | ||
end | ||
|
||
it "should provide the average score from the movie list" do | ||
clerk = MovieClerk.new | ||
clerk.search_by_title("baseketball") | ||
clerk.search_by_title("goonies") | ||
clerk.movie_score_average.should eq(54.5) | ||
end | ||
|
||
describe "#analyze_taste" do | ||
it "should tell the user their taste is improving over time" do | ||
clerk = MovieClerk.new | ||
clerk.search_by_title("fargo") | ||
clerk.search_by_title("spaceballs") | ||
clerk.analyze_taste | ||
clerk.taste_measurement.should be_within(0.01).of(4.44) | ||
end | ||
it "should tell the user their taste is diminishing over time" do | ||
clerk = MovieClerk.new | ||
clerk.search_by_title("gigli") | ||
clerk.search_by_title("fargo") | ||
clerk.analyze_taste | ||
clerk.taste_measurement.should be_within(0.01).of(-12.57) | ||
end | ||
end | ||
|
||
$stdout = StringIO.new | ||
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. good stuff on $stdout.... usually, when you re-assign stdout, you want to only do it temporarily, so you can do your thing, check your thing, and reset your thing. 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. Maybe use this technique on http://thinkingdigitally.com/archive/capturing-output-from-puts-in-ruby/ require 'stringio'
module Kernel
def capture_stdout
out = StringIO.new
$stdout = out
yield
return out
ensure
$stdout = STDOUT
end
end
results = capture_stdout do
puts "hi!"
end
results.string
=> "hi!\n" Only real tricky part is the "\n".... if you hate that, you could change to results = capture_stdout do
print "hi"
end
results.string
=> "hi" |
||
end |
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.
:)