diff --git a/lib/api.rb b/lib/api.rb index a8d499c..9f79334 100644 --- a/lib/api.rb +++ b/lib/api.rb @@ -9,11 +9,15 @@ 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.new(id: struct.id.to_i, - title: struct.title, - year: struct.year, - score: struct.ratings["critics_score"] - ) + if struct.id.nil? + :no_movie_found + else + Movie.new(id: struct.id.to_i, + title: struct.title, + year: struct.year, + score: struct.ratings["critics_score"] + ) + end end diff --git a/lib/text_color.rb b/lib/text_color.rb new file mode 100644 index 0000000..5d2dc30 --- /dev/null +++ b/lib/text_color.rb @@ -0,0 +1,28 @@ +# Found at: http://www.linuxgazette.net/issue65/padala.html + +# ATTRIBUTES +RESET = 0 +BRIGHT = 1 +DIM = 2 +UNDERLINE = 3 +BLINK = 4 +REVERSE = 7 +HIDDEN = 8 + +# COLORS +BLACK = 0 +RED = 1 +GREEN = 2 +YELLOW = 3 +BLUE = 4 +MAGENTA = 5 +CYAN = 6 +WHITE = 7 + +def text_color(attr, color) + command = String.new + + # Command is the control command to the terminal + command << "\e[%d;%dm" % [attr, color + 30] + printf(command) +end diff --git a/movie_json.rb b/movie_json.rb index d8a91d7..5034bc2 100644 --- a/movie_json.rb +++ b/movie_json.rb @@ -1,20 +1,86 @@ require_relative "lib/movie" require_relative "lib/api" +require_relative "lib/text_color" -def find_movie - puts "OH HAI. Search?" - movie_title = gets - movie = Api.search_by_title(movie_title) - puts "Found: #{movie.title}. Score: #{movie.score}" +def movies_average(movies_arr) + scores = [] + movies_arr.each { |movie| scores << movie.score } + scores.inject{ |sum, el| sum + el }.to_f / scores.size end -find_movie +def get_y(sorted_movies, x) + y_arr = [] + sorted_movies.each do |movie| + y_arr << movie if movie.year == x + end + movies_average(y_arr) +end + +def movies_slope(movies_arr) + sorted_movies = movies_arr.sort_by { |movie| movie.year } + x1 = sorted_movies.last.year # latest movie year ex: 2012 + x2 = sorted_movies.first.year # earliest movie year ex: 1990 + y1 = get_y(sorted_movies, x1) # average of the scores from latest year ex: 45 + y2 = get_y(sorted_movies, x2) # average of the scores from earliest year ex: 50 + (y1 - y2).to_f / (x1 - x2).to_f # (45 - 50)to_f / (2012 - 1990).to_f + #=> -0.22727272727272727 +end + +def show_average(movies_arr) + text_color(BRIGHT, BLUE) + printf "Average: #{movies_average(movies_arr)}\n" + text_color(RESET, WHITE) +end + +def show_contentment(movies_arr) + contentment = movies_slope(movies_arr) + if contentment == 0.0 + puts "Perfectly Content" + elsif contentment > 0.0 + text_color(BRIGHT, YELLOW) + puts "Getting Happier" + else + text_color(BRIGHT, MAGENTA) + puts "Getting Madder" + end + text_color(RESET, WHITE) +end + +def find_movie(movies_arr) + print "Search: " + searched_movie = gets.chomp + movie = Api.search_by_title(searched_movie) + if movie == :no_movie_found + text_color(BRIGHT, RED) + printf "\tCould not find #{searched_movie}\n" + else + text_color(BRIGHT, GREEN) + printf "\tFound: #{movie.title}\n" + + "\t Year: #{movie.year}\n" + + "\tScore: #{movie.score}\n" + movies_arr << movie + end + text_color(RESET, WHITE) +end + +movies_arr = [] + +puts "HI! Welcome to rssll's #{$0}." +printf "Search for movies that you really like!\n\n" +find_movie(movies_arr) -while true do - puts "Search Again (Y/N)" - answer = gets.upcase[0] - if answer == "Y" - find_movie +loop do + print "Search Again? (Y/N) " + answer = gets.chomp + if answer =~ /Y/i + find_movie(movies_arr) + if movies_arr.size > 1 + show_average(movies_arr) + # Show contentment unless all the movies are from the same year + show_contentment(movies_arr) unless movies_arr.none? do |movie| + movie.year != movies_arr[0].year + end + end else break end