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 and some tiger level #9

Open
wants to merge 1 commit 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
34 changes: 34 additions & 0 deletions 3:w
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require "open-uri"
require "json"
require "ostruct"
require_relative "./movie"
require_relative "../movie-stats"
class Api
@movies = []

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)
movies << Movie.new(id: struct.id.to_i,
title: struct.title,
year: struct.year,
score: struct.ratings["critics_score"])
end


def self.get_url_as_json(url)
JSON.parse(open(url).read)
end

def self.validate_title(movie_title)
while movie_title.empty? or movie_title.nil?
puts "Invalid Input:\nEnter another movie."
movie_title = gets.chomp
end
movie_title
end

end

14 changes: 12 additions & 2 deletions lib/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,33 @@
require "json"
require "ostruct"
require_relative "./movie"
require_relative "../movie-stats"
class Api

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)

Movie.new(id: struct.id.to_i,
title: struct.title,
year: struct.year,
score: struct.ratings["critics_score"]
)
score: struct.ratings["critics_score"])
end


def self.get_url_as_json(url)
JSON.parse(open(url).read)
end

def self.validate_title(movie_title)
while movie_title.empty? or movie_title.nil?
puts "Invalid Input:\nEnter another movie."
movie_title = gets.chomp
end
movie_title
end

end

36 changes: 36 additions & 0 deletions movie-stats.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class MovieStats
attr_reader :data
attr_accessor :scores

def initialize
@data = []
end

def collect_movies(movie)
data << movie
end

def average_rating
sum = 0
Copy link
Member

Choose a reason for hiding this comment

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

FYI -- one easy way in ruby to sum an array:

data.inject(:+)

data.each do |m|
sum += m.score
end
puts "The average rating is: #{ sum.to_f/data.length }"
end

def happiness
# years = []
# scores = []

# data.each do |m|
# years << m.year
# scores << m.score
# end

# data.each do |m|
# scores << m.score
# end
# binding.pry
# puts "The slope is: #{(scores.max - scores.min)/( years.max - years.min )}"
end
end
11 changes: 9 additions & 2 deletions movie_json.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
require_relative "lib/movie"
require_relative "lib/api"
require_relative "movie-stats"
require "pry"

def find_movie
puts "OH HAI. Search?"
movie_title = gets
title = gets.chomp
movie_title = Api.validate_title(title)
movie = Api.search_by_title(movie_title)
# binding.pry
@stats.collect_movies(movie)
@stats.average_rating
@stats.happiness
Copy link
Member

Choose a reason for hiding this comment

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

This does nothing though, right? happiness is commented out?

puts "Found: #{movie.title}. Score: #{movie.score}"
end

@stats = MovieStats.new
find_movie

while true do
Expand Down