Skip to content

Commit

Permalink
handle movie not found on Wikipedia
Browse files Browse the repository at this point in the history
  • Loading branch information
mislav committed Feb 2, 2012
1 parent c08ecfd commit 468f719
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 6 deletions.
10 changes: 8 additions & 2 deletions app/controllers/movies_controller.rb
Expand Up @@ -92,8 +92,14 @@ def remove_from_watched
end

def wikipedia
@movie.get_wikipedia_title unless @movie.wikipedia_title
redirect_to @movie.wikipedia_url
@movie.update_wikipedia_url! unless @movie.wikipedia_url.present?

if @movie.wikipedia_url.present?
redirect_to @movie.wikipedia_url, status: 301
else
@message = "Can't find this movie on Wikipedia."
render 'shared/not_found', :status => 404
end
end

def change_plot_field
Expand Down
5 changes: 2 additions & 3 deletions app/models/movie.rb
Expand Up @@ -242,10 +242,9 @@ def wikipedia_title=(str)
def wikipedia_url
WIKIPEDIA_PREFIX + wikipedia_title.tr(' ', '_') if wikipedia_title
end
def get_wikipedia_title

def update_wikipedia_url!
self.wikipedia_title = Wikipedia.find_title("#{self.title} #{self.year}")
self.save
self.wikipedia_title
end
end
2 changes: 1 addition & 1 deletion spec/models/movie_spec.rb
Expand Up @@ -216,7 +216,7 @@ def create(attributes)
)

movie = build title: 'Misery', year: 1990
movie.get_wikipedia_title.should == 'Misery (1990 film)'
movie.update_wikipedia_url!
movie.wikipedia_url.should == 'http://en.wikipedia.org/wiki/Misery_(1990_film)'
end
end
Expand Down
43 changes: 43 additions & 0 deletions spec/requests/movies_spec.rb
@@ -0,0 +1,43 @@
require 'spec_helper'

describe "Movies" do
describe "GET /[movie]/wikipedia" do
before do
@movie = Movie.create title: 'Misery', year: 1990
end

it "fetches wikipedia URL from its API" do
stub_request(:get, 'en.wikipedia.org/w/api.php?format=json&action=query&list=search&srsearch=Misery%201990').
to_return(
body: {query: {search:[{title: 'Misery (1990 film)'}]}}.to_json,
status: 200,
headers: {'content-type' => 'application/json'}
)

get wikipedia_movie_path(@movie)
response.status.should be(301)
response.should redirect_to 'http://en.wikipedia.org/wiki/Misery_(1990_film)'
end

it "handles movie not found" do
stub_request(:get, 'en.wikipedia.org/w/api.php?format=json&action=query&list=search&srsearch=Misery%201990').
to_return(
body: {query: {search:[]}}.to_json,
status: 200,
headers: {'content-type' => 'application/json'}
)

get wikipedia_movie_path(@movie)
response.status.should be(404)
end

it "uses stored wikipedia URL" do
@movie.wikipedia_title = 'http://en.wikipedia.org/wiki/Misery_(1990_film)'
@movie.save

get wikipedia_movie_path(@movie)
response.status.should be(301)
response.should redirect_to @movie.wikipedia_url
end
end
end

0 comments on commit 468f719

Please sign in to comment.