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 Tiger Eagle #6

Open
wants to merge 5 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
4 changes: 4 additions & 0 deletions lib/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@
require "json"
require "ostruct"
require_relative "./movie"

class Api

class NilMovie < StandardError; end

APIKEY="4t6456xa33z8qhcqyuqgnkjh"

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)
raise NilMovie if struct.title == nil
Movie.new(id: struct.id.to_i,
title: struct.title,
year: struct.year,
Expand Down
54 changes: 50 additions & 4 deletions movie_json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,62 @@
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}"
current_movie = Api.search_by_title(movie_title)
@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
rescue Api::NilMovie
puts 'Oops. Something went wrong or title not found.'
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
5 changes: 5 additions & 0 deletions spec/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,9 @@
it "should return the year" do
movie.year.should eq(1994)
end

it "should not raise error if title not found" do
expect{Api.search_by_title("NOTHINGFOUNDHERE")}.to_not raise_error
end

end