diff --git a/3:w b/3:w new file mode 100644 index 0000000..d057b18 --- /dev/null +++ b/3:w @@ -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 + diff --git a/lib/api.rb b/lib/api.rb index a8d499c..c6892af 100644 --- a/lib/api.rb +++ b/lib/api.rb @@ -2,6 +2,7 @@ require "json" require "ostruct" require_relative "./movie" +require_relative "../movie-stats" class Api APIKEY="4t6456xa33z8qhcqyuqgnkjh" @@ -9,11 +10,11 @@ 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"] - ) + score: struct.ratings["critics_score"]) end @@ -21,4 +22,13 @@ 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 + diff --git a/movie-stats.rb b/movie-stats.rb new file mode 100644 index 0000000..8accd67 --- /dev/null +++ b/movie-stats.rb @@ -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 + 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 diff --git a/movie_json.rb b/movie_json.rb index d8a91d7..e605b6b 100644 --- a/movie_json.rb +++ b/movie_json.rb @@ -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 puts "Found: #{movie.title}. Score: #{movie.score}" end - +@stats = MovieStats.new find_movie while true do