Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions lib/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ 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)
return false if struct.title == nil
Copy link
Member

Choose a reason for hiding this comment

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

Minor note: I don't love the use of a false here to indicate that nothing was found. This could be referred to as "primitive obsession". It gets the job done, but a different class like NilMovie might be "better"

Movie.new(id: struct.id.to_i,
title: struct.title,
year: struct.year,
Expand Down
55 changes: 51 additions & 4 deletions movie_json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,63 @@
require_relative "lib/api"

def find_movie
puts "OH HAI. Search?"
puts 'Add a movie you really like:'
movie_title = gets
movie = Api.search_by_title(movie_title)
puts "Found: #{movie.title}. Score: #{movie.score}"
if !(current_movie = Api.search_by_title(movie_title))
puts 'Oops. Something went wrong or title not found.'
else
@movies << current_movie
puts "Found: #{@movies.last.title}. Score: #{@movies.last.score} Year: #{@movies.last.year}"
if @movies.length > 1
puts "Average movie score: #{get_average(@movies, "score", "float")}"
puts "Average movie year: #{get_average(@movies, "year", "integer")}"
puts get_satisfaction(@movies)
end
end
end

def get_average(movies, attribute, type)
attribute_sum = 0
movies.each { |rating| attribute_sum += rating.send(attribute) }
if type == "float"
return attribute_sum / movies.length.to_f
else
return attribute_sum / movies.length
end
end

def get_satisfaction(movies)
Copy link
Member

Choose a reason for hiding this comment

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

This method is probably a bit too long. Good guideline: 3,4,5 lines are all your methods "should" be.

min_movie = movies.min_by(&:year)
max_movie = movies.max_by(&:year)
return "neutral" if min_movie == max_movie
max_movie_count, max_movie_score_total = 0, 0
min_movie_count, min_movie_score_total = 0, 0
movies.each do |movie|
if movie.year == max_movie.year
max_movie_count += 1
max_movie_score_total += movie.score
end
if movie.year == min_movie.year
min_movie_count += 1
min_movie_score_total += movie.score
end
end
user_slope = ((max_movie_score_total/max_movie_count).to_f - (min_movie_score_total/min_movie_count).to_f) / (max_movie.year - min_movie.year).to_f
if user_slope == 0
return "neutral"
elsif user_slope > 0
return "getting happier"
else
return "getting madder"
end
end

@movies = []

find_movie

while true do
puts "Search Again (Y/N)"
puts 'Search Again (Y/N)'
answer = gets.upcase[0]
if answer == "Y"
find_movie
Expand Down