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
41 changes: 33 additions & 8 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/api"

def get_average(movies, attribute)
if attribute == "score"
Copy link
Member

Choose a reason for hiding this comment

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

A cool thing in ruby --- you can tell movies what attribute you want it to collect. (collect is an alias for map, so I'm going to use map below)

data = movies.map(&:attribute)
data.inject(0.0) { |sum, rating| sum + rating } / data.size

Copy link
Author

Choose a reason for hiding this comment

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

I thought there would likely be a cool Ruby thing that would do this in a generic way, but I'm unfamiliar with the &:attribute notation, so I couldn't get it to work. I need to spend some time going through the Enumerable module.

data = movies.collect { |i| i.score }
elsif attribute == "year"
data = movies.collect { |i| i.year }
else
data = []
end
average = data.inject(0.0) { |sum, rating| sum + rating } / data.size
end

def calculate_slope(movies)
movies = movies.sort_by { |i| i.year }
slope = (movies.last.score - movies.first.score).to_f /
(movies.last.year - movies.first.year).to_f
if slope < 0
puts "You are getting madder."
elsif slope > 0
puts "You are getting happier."
end
end

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}"
movie
rescue
puts "Movie not found."
end

find_movie

while true do
puts "Search Again (Y/N)"
movies = []
while true
movies << find_movie
puts "Average rating: #{get_average(movies, "score")}. Average year: #{get_average(movies, "year")}"
puts "Search Again (Y/N)"
answer = gets.upcase[0]
if answer == "Y"
find_movie
else
if answer != "Y"
calculate_slope(movies) if movies.count > 1
break
end
end
6 changes: 6 additions & 0 deletions spec/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,10 @@
it "should return the year" do
movie.year.should eq(1994)
end

it "should not throw an error" do
expect {
Api.search_by_title("NOTHINGFOUNDHERE")
}.to_not raise_error
end
end