Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
85 changes: 74 additions & 11 deletions movie_json.rb
Original file line number Diff line number Diff line change
@@ -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|
Copy link
Member

Choose a reason for hiding this comment

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

I would rather you use map here; though I'm having a bit of trouble reading what you're doing here. I THINK you're trying to get the first year and last year of movies in the movies, right? If so, maybe try sorting them by year and pulling the first and last.

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}."
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(movies_arr)
end
else
break
end
Expand Down