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
32 changes: 25 additions & 7 deletions lib/api.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,42 @@
require "open-uri"
require "json"
require "ostruct"
require_relative "./movie"
require_relative "movie"
require_relative "movie_history"

class Api

APIKEY="4t6456xa33z8qhcqyuqgnkjh"

def self.search_by_title(title)
MovieHistory.add_search(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"]
)
full_json = get_url_as_json(url)

if found?(full_json)
struct = OpenStruct.new(full_json.fetch("movies").first)
movie = Movie.new( id: struct.id.to_i,
title: struct.title,
year: struct.year,
score: struct.ratings["critics_score"] )
puts "Found: #{movie.title}. Score: #{movie.score}"
Movie.add_movies(movie)
movie
else
empty_search(title)
end
end

def self.found?( json )
json.fetch("total").zero? ? false : true
end

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

def self.empty_search( title )
puts "Not found: #{title}"
end
end
49 changes: 49 additions & 0 deletions lib/movie.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,55 @@
require_relative 'slope'

class Movie

attr_reader :id, :title, :year, :score

class << self
attr_reader :movies
end

def self.add_movies(movie)
@movies ||= []
movies << movie
end

def self.average(method)
total = movies.inject(0) { | sum, movie | sum + movie.send(method) }
( total / movies.length ).round
end

def self.average_score
average(:score)
end

def self.average_year
average(:year)
end

def self.user_satisfaction
return "Happier" if slope > 0
return "Maddier" if slope < 0
end

def self.slope
Slope.calculate( y1: newest_movie.score,
y2: oldest_movie.score,
x1: newest_movie.year,
x2: oldest_movie.year )
end

def self.oldest_movie
sorted_movies.first
end

def self.newest_movie
sorted_movies.last
end

def self.sorted_movies
sorted_movies = movies.sort_by { | movie | movie.year }
end

def initialize(hash={})
@id = hash.fetch(:id)
@title = hash.fetch(:title)
Expand Down
11 changes: 11 additions & 0 deletions lib/movie_history.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class MovieHistory

class << self
attr_reader :searches
end

def self.add_search(title)
@searches ||= []
@searches << title
end
end
5 changes: 5 additions & 0 deletions lib/slope.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Slope
def self.calculate( values={} )
(( values[:y1] - values[:y2] ) / ( values[:x1] - values[:x2] ).to_f).round(3)
end
end
23 changes: 11 additions & 12 deletions movie_json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@
require_relative "lib/api"

def find_movie
puts "OH HAI. Search?"
movie_title = gets
movie = Api.search_by_title(movie_title)
puts "Found: #{movie.title}. Score: #{movie.score}"
end

find_movie

while true do
puts "Search Again (Y/N)"
answer = gets.upcase[0]
puts "Add a movie you really like"
movie_title = gets.chomp
Api.search_by_title(movie_title)
puts "Search Again? (Y/N)"
answer = gets.chomp.upcase[0]
if answer == "Y"
find_movie
else
break
puts "The score average of the movies you added is: #{Movie.average_score}"
exit
end
end

find_movie


72 changes: 59 additions & 13 deletions spec/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,71 @@

describe Api do

let(:movie) { Api.search_by_title("Forrest Gump") }
context "Existing Movie" do

before do
Api.stub(:get_url_as_json) { JSON.parse(File.read("spec/fixtures/forrest.json")) }
end
let(:movie) { Api.search_by_title("Forrest Gump") }

it "should search for movies" do
movie.title.should eq("Forrest Gump")
end
before do
Api.stub(:get_url_as_json) { JSON.parse(File.read("spec/fixtures/forrest.json")) }
end

it "should search for movies" do
movie.title.should eq("Forrest Gump")
end

it "should return the score" do
movie.score.should eq(71)
it "should return the score" do
movie.score.should eq(71)
end

it "should return the id" do
movie.id.should eq(10036)
end

it "should return the year" do
movie.year.should eq(1994)
end
end

it "should return the id" do
movie.id.should eq(10036)
context "Movie search" do

it "should find the movie" do
json = JSON.parse(File.read("spec/fixtures/forrest.json"))
Api.found?(json).should eq(true)
end

it "should not find the movie" do
json = JSON.parse(File.read("spec/fixtures/loquillo.json"))
Api.found?(json).should eq(false)
end

it "should raise error when empty search" do
Api.stub(:get_url_as_json) { JSON.parse(File.read("spec/fixtures/loquillo.json")) }
expect {
Api.search_by_title("MOVIE NOT FOUND")
}.to_not raise_error
end
end

it "should return the year" do
movie.year.should eq(1994)
context "Search History" do

before do
MovieHistory.searches.clear
Api.stub(:get_url_as_json) { JSON.parse(File.read("spec/fixtures/forrest.json")) }
found_searches = [ "Forrest Gump", "Titanic", "Click" ]
found_searches.each do | title |
Api.search_by_title( title )
end

Api.stub(:get_url_as_json) { JSON.parse(File.read("spec/fixtures/loquillo.json")) }
not_found_searches = [ "Nothing found", "llllll" ]
not_found_searches.each do | title |
Api.search_by_title( title )
@user_searches = found_searches + not_found_searches
end
end

it "should show the user's search history" do
MovieHistory.searches.should eq(@user_searches)
end
end
end
1 change: 1 addition & 0 deletions spec/fixtures/loquillo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"total":0,"movies":[],"links":{"self":"http://api.rottentomatoes.com/api/public/v1.0/movies.json?q=loquillo&page_limit=1&page=1"},"link_template":"http://api.rottentomatoes.com/api/public/v1.0/movies.json?q={search-term}&page_limit={results-per-page}&page={page-number}"}
15 changes: 15 additions & 0 deletions spec/movie_history_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require_relative '../lib/movie_history.rb'

describe MovieHistory do
before do
MovieHistory.searches.clear if MovieHistory.searches
end

it "should return the history" do
user_searches = ["Forrest Gump", "Not Found", "Titanic", "Click"]
user_searches.each do | search |
MovieHistory.add_search(search)
end
MovieHistory.searches.should eq(user_searches)
end
end
50 changes: 49 additions & 1 deletion spec/movie_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,53 @@
movie.year.should eq(1998)
movie.score.should eq(50)
end


context "Adding movies and operations with them" do

before do
Movie.movies.clear if Movie.movies
movie1 = Movie.new(id: "the-id", title: "the-title", year: 1998, score: 50)
movie2 = Movie.new(id: 32 , title: "I'm 132" , year: 2005, score: 100)
movie3 = Movie.new(id: 15500 , title: "Dogs" , year: 2013, score: 88)
movie4 = Movie.new(id: "the-id", title: "Titanic" , year: 1998, score: 110)
movie5 = Movie.new(id: "the-id", title: "2012" , year: 2012, score: 77)

movies = [ movie1, movie2, movie3, movie4, movie5 ]
movies.each { | movie | Movie.add_movies(movie) }
end

it "should add movies the user likes" do
Movie.movies.length.should eq(5)
end

it "should calculate the average of the movies score" do
Movie.average_score.should eq(85)
end

it "should calculate the average of the movies year" do
Movie.average_year.should eq(2005)
end
end

context "First and last year" do
it "should obtain the movie with the oldest year" do
movie = Movie.oldest_movie
movie.title.should eq("the-title")
end

it "should obtain the movie with the newest year" do
movie = Movie.newest_movie
movie.title.should eq("Dogs")
end
end

context "Sloping" do
it "should calculate the slope from the first year to the last year" do
Movie.slope.should eq(2.533)
end

it "should determine if the user is getting happier" do
Movie.user_satisfaction.should eq("Happier")
end
end
end
9 changes: 9 additions & 0 deletions spec/slope_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require_relative '../lib/slope.rb'

describe Slope do
it "should calculate the slope of line" do
values = { x1: 2015, x2: 1990,
y1: 50, y2: 44 }
Slope.calculate(values).should eq(0.24)
end
end