Skip to content
Browse files

handle movie not found on Wikipedia

  • Loading branch information...
1 parent c08ecfd commit 468f719f042cc9066d6fc0ae27c08d51136d0eb2 @mislav committed Feb 2, 2012
Showing with 54 additions and 6 deletions.
  1. +8 −2 app/controllers/movies_controller.rb
  2. +2 −3 app/models/movie.rb
  3. +1 −1 spec/models/movie_spec.rb
  4. +43 −0 spec/requests/movies_spec.rb
View
10 app/controllers/movies_controller.rb
@@ -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
View
5 app/models/movie.rb
@@ -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
View
2 spec/models/movie_spec.rb
@@ -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
View
43 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.
Something went wrong with that request. Please try again.