Skip to content
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
14 changes: 9 additions & 5 deletions lib/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
28 changes: 28 additions & 0 deletions lib/text_color.rb
Original file line number Diff line number Diff line change
@@ -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
88 changes: 77 additions & 11 deletions movie_json.rb
Original file line number Diff line number Diff line change
@@ -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}."
Copy link
Member

Choose a reason for hiding this comment

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

You don't see many $0 lately. Awesome to use it in this context.

printf "Search for movies that you really like!\n\n"
Copy link
Member

Choose a reason for hiding this comment

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

Generally you wouldn't use printf, just stick to puts for the most part. Nothing wrong, of course, with using API's that are there for you.

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
Expand Down