Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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.respond_to?(:title)
Copy link
Member

Choose a reason for hiding this comment

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

Interesting use of respond_to here

Movie.new(id: struct.id.to_i,
title: struct.title,
year: struct.year,
score: struct.ratings["critics_score"]
)
else
Movie.new(id: 0,
Copy link
Member

Choose a reason for hiding this comment

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

I'd rather see a "NullMovie" or "NotFoundMovie" object returned here --- something that you can test for, rather than testing if the movie's id == 0.

But, this works!

If you did the null movie, it could look like

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"
  build_move get_url_as_json(url).fetch("movies").first
end

def build_movie(json)
  struct = OpenStruct.new(json)
  if json["ratings"]
    Movie.new() # id/title/year
  else
    NotFoundMovie.new
end

title: "no results",
year: "",
score: "N/A"
)
end
end


def self.get_url_as_json(url)
JSON.parse(open(url).read)
end
Expand Down
41 changes: 26 additions & 15 deletions spec/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,37 @@
require "ostruct"

describe Api do
describe ".search_by_title" do
context "when title found" do
let(:movie) { Api.search_by_title("Forrest Gump") }

let(:movie) { Api.search_by_title("Forrest Gump") }
before do
Api.stub(:get_url_as_json) { JSON.parse(File.read("spec/fixtures/forrest.json")) }
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 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 score" do
movie.score.should eq(71)
end
it "should return the id" do
movie.id.should eq(10036)
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

context "when title not found" do
let(:movie) { Api.search_by_title("asdfasdfasdf") }

it "should return the year" do
movie.year.should eq(1994)
it "should not raise an error" do
movie.title.should_not raise_error
end
end
end
end