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

Movie JSON Wrapper Pull Request - Tiger Level #5

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
19 changes: 13 additions & 6 deletions lib/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,21 @@ 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.title == nil
Movie.new(id: 1,
title: "No movie by that title",
year: 1111,
score: 0
)
else
Movie.new(id: struct.id.to_i,
title: struct.title,
year: struct.year,
score: struct.ratings["critics_score"]
)
end
end


def self.get_url_as_json(url)
JSON.parse(open(url).read)
end
Expand Down
20 changes: 16 additions & 4 deletions movie_json.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
require_relative "lib/movie"
require_relative "lib/api"

def find_movie
puts "OH HAI. Search?"


def find_movie()
puts "Add a movie you really like"
movie_title = gets
movie = Api.search_by_title(movie_title)
puts "Found: #{movie.title}. Score: #{movie.score}"
movie.score
end

def calculate_average(array_of_numbers)
array_of_numbers.inject(0.0) { |sum, el| sum + el } / array_of_numbers.size
end

find_movie

movies = []

movies << find_movie

while true do
puts "Search Again (Y/N)"
answer = gets.upcase[0]

if answer == "Y"
find_movie
movies << find_movie
puts "The average score of your movies is: " + calculate_average(movies).to_s
else
break
end
Expand Down
48 changes: 34 additions & 14 deletions spec/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,45 @@

describe Api do

let(:movie) { Api.search_by_title("Forrest Gump") }
describe "this is what a search that returns a movie should do" do

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
let(:movie) { Api.search_by_title("Forrest Gump") }

it "should return the score" do
movie.score.should eq(71)
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)
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

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

it "should return the year" do
movie.year.should eq(1994)
describe "this is how a search that does not find a movie should work" do

let(:movie) { Api.search_by_title("Noodle Weird") }

before do
Api.stub(:get_url_as_json) { JSON.parse(File.read("spec/fixtures/noodle.json")) }
end

it "should not crash if a search returns no movie" do
movie.title.should eq("No movie by that title")
end

end

end
31 changes: 31 additions & 0 deletions spec/fixtures/noodle.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@






























{"total":0,"movies":[],"links":{"self":"http://api.rottentomatoes.com/api/public/v1.0/movies.json?q=Noodle+Weird&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}"}