From eb51e8b3838bfa3fce186a418dade30c77fcb97a Mon Sep 17 00:00:00 2001 From: rssll Date: Sun, 10 Mar 2013 23:54:26 -0500 Subject: [PATCH 1/3] Implement: Panda, Tiger & Eagle Levels --- lib/api.rb | 14 ++++++--- movie_json.rb | 85 ++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 83 insertions(+), 16 deletions(-) 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/movie_json.rb b/movie_json.rb index d8a91d7..661ae7a 100644 --- a/movie_json.rb +++ b/movie_json.rb @@ -1,20 +1,83 @@ 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 movies_slope(movies_arr) + movies_by_year = movies_arr.sort_by! { |movie| movie.year } + y2_arr = [] + y1_arr = [] + movies_by_year.each do |movie| + if movie.year == movies_by_year.first.year + y2_arr << movie + end + end + movies_by_year.each do |movie| + if movie.year == movies_by_year.last.year + y1_arr << movie + end + end + y1 = movies_average(y1_arr) + y2 = movies_average(y2_arr) + x1 = movies_by_year.last.year + x2 = movies_by_year.first.year + (y1 - y2).to_f / (x1 - x2).to_f +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) + if movies_slope(movies_arr) == 0 + puts "Staying Content" + elsif movies_slope(movies_arr) > 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\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(movies_arr) + end else break end From ba18d010375f42a3aadf299d32800246ae28776b Mon Sep 17 00:00:00 2001 From: rssll Date: Sun, 10 Mar 2013 23:56:38 -0500 Subject: [PATCH 2/3] Added text_color.rb to lib --- lib/text_color.rb | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 lib/text_color.rb 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 From ce0813d47601f8d227a73af40bc52c4f9f7ceb18 Mon Sep 17 00:00:00 2001 From: rssll Date: Fri, 15 Mar 2013 11:01:04 -0500 Subject: [PATCH 3/3] Refactor: DRY up movie_slope, add robustness --- movie_json.rb | 49 ++++++++++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/movie_json.rb b/movie_json.rb index 661ae7a..5034bc2 100644 --- a/movie_json.rb +++ b/movie_json.rb @@ -8,25 +8,22 @@ def movies_average(movies_arr) scores.inject{ |sum, el| sum + el }.to_f / scores.size end -def movies_slope(movies_arr) - movies_by_year = movies_arr.sort_by! { |movie| movie.year } - y2_arr = [] - y1_arr = [] - movies_by_year.each do |movie| - if movie.year == movies_by_year.first.year - y2_arr << movie - end - end - movies_by_year.each do |movie| - if movie.year == movies_by_year.last.year - y1_arr << movie - end +def get_y(sorted_movies, x) + y_arr = [] + sorted_movies.each do |movie| + y_arr << movie if movie.year == x end - y1 = movies_average(y1_arr) - y2 = movies_average(y2_arr) - x1 = movies_by_year.last.year - x2 = movies_by_year.first.year - (y1 - y2).to_f / (x1 - x2).to_f + 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) @@ -36,9 +33,10 @@ def show_average(movies_arr) end def show_contentment(movies_arr) - if movies_slope(movies_arr) == 0 - puts "Staying Content" - elsif movies_slope(movies_arr) > 0 + contentment = movies_slope(movies_arr) + if contentment == 0.0 + puts "Perfectly Content" + elsif contentment > 0.0 text_color(BRIGHT, YELLOW) puts "Getting Happier" else @@ -57,7 +55,9 @@ def find_movie(movies_arr) printf "\tCould not find #{searched_movie}\n" else text_color(BRIGHT, GREEN) - printf "\tFound: #{movie.title}\n\tScore: #{movie.score}\n" + printf "\tFound: #{movie.title}\n" + + "\t Year: #{movie.year}\n" + + "\tScore: #{movie.score}\n" movies_arr << movie end text_color(RESET, WHITE) @@ -76,7 +76,10 @@ def find_movie(movies_arr) find_movie(movies_arr) if movies_arr.size > 1 show_average(movies_arr) - show_contentment(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