diff --git a/app/models/story_parser.rb b/app/models/story_parser.rb index c36ac0b1e92..d5d30803410 100644 --- a/app/models/story_parser.rb +++ b/app/models/story_parser.rb @@ -248,10 +248,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 0085ec30d63..4213f03f15e 100644 --- a/spec/models/story_parser_spec.rb +++ b/spec/models/story_parser_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' describe StoryParser do - + before(:each) do @sp = StoryParser.new end @@ -94,10 +94,27 @@ # @sp.get_source_if_known(StoryParser::KNOWN_STORY_LOCATIONS, url).should eq("lj") # end end - + # 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 + describe "#parse_common" do it "should convert relative to absolute links" do # This one doesn't work because the sanitizer is converting the & to & @@ -119,5 +136,4 @@ end end end - end