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 Level--No exceptions #16

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
13 changes: 8 additions & 5 deletions lib/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ 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?
else
Copy link
Member

Choose a reason for hiding this comment

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

here you're returning nil if the struct has no title. One other way you can do this in ruby:

unless struct.title
  Movie.new # continue rest
end

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


Expand Down
40 changes: 31 additions & 9 deletions movie_json.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,43 @@
require_relative "lib/movie"
require_relative "lib/api"


def search_again
while true do
puts "Search Again (Y/N)"
answer = gets.upcase[0]
if answer == "Y"
find_movie
else
break
end
end
end

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

find_movie

while true do
puts "Search Again (Y/N)"
answer = gets.upcase[0]
if answer == "Y"
find_movie
else
break
end
end
#So we want to do something after movie = Api.search_by_title(movie_title) like if movie == false {puts "#{movie.title} not found, Search Again (Y/N)?" -then basically the while loop below--}
#if movie == false
# puts "#{movie.title} was not found. Search again? (Y/N)"
#answer = gets.upcase[0]
#if answer =="Y"
# find_movie
#end
#end





5 changes: 5 additions & 0 deletions spec/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,9 @@
it "should return the year" do
movie.year.should eq(1994)
end

it "should return a nil response if no movie title is found" do
expect { Api.search_by_title("NOTHINGFOUNDHERE") }.to_not raise_error
end

end
1 change: 1 addition & 0 deletions spec/movie_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
movie.score.should eq(50)
end


end