From 764933ee86c68e3a4c11594fd96aeea68a01c946 Mon Sep 17 00:00:00 2001 From: Enigel Date: Sun, 2 Feb 2014 20:24:19 +0000 Subject: [PATCH] Use our nifty URL detector library to check for previous import Our `find_by_url` already checks for www and non-www, encoded and non-encoded, etc versions of the URL. Let's use it. With spec! --- app/models/story_parser.rb | 5 ++--- spec/models/story_parser_spec.rb | 18 +++++++++++++++++- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/app/models/story_parser.rb b/app/models/story_parser.rb index 0adff35c2cb..b89a2ae19ac 100644 --- a/app/models/story_parser.rb +++ b/app/models/story_parser.rb @@ -243,10 +243,9 @@ def download_and_parse_chaptered_story(source, location, options = {}) end - #Updated as elz suggested now getting www and non www, Stephanie 1-11-2014 + # our custom url finder checks for previously imported URL in almost any format it may have been presented def check_for_previous_import(location) - urls = [location, location.gsub('www.', '')].uniq - if Work.where(imported_from_url: urls).exists? + if Work.find_by_url(location).present? raise Error, "A work has already been imported from #{location}." end end diff --git a/spec/models/story_parser_spec.rb b/spec/models/story_parser_spec.rb index 9837dc118c3..588f3a1517c 100644 --- a/spec/models/story_parser_spec.rb +++ b/spec/models/story_parser_spec.rb @@ -97,5 +97,21 @@ # TODO: KNOWN_STORY_PARSERS end - + + describe "check_for_previous_import" do + let(:location_with_www) { "http://www.testme.org/welcome_to_test_vale.html" } + let(:location_no_www) { "http://testme.org/welcome_to_test_vale.html" } + + it "should recognise previously imported www. works" do + @work = FactoryGirl.create(:work, imported_from_url: location_with_www) + + expect { @sp.check_for_previous_import(location_no_www) }.to raise_exception + end + + it "should recognise previously imported non-www. works" do + @work = FactoryGirl.create(:work, imported_from_url: location_no_www) + + expect { @sp.check_for_previous_import(location_with_www) }.to raise_exception + end + end end