From 9f9e608fc875e7c7fc8ff3be5be77e62bd8a4ec4 Mon Sep 17 00:00:00 2001 From: joshua stein Date: Thu, 3 Dec 2015 14:54:55 -0600 Subject: [PATCH] fix fetched_attributes fallout --- app/controllers/stories_controller.rb | 5 +---- app/models/story.rb | 20 +++++++++++--------- spec/models/story_spec.rb | 4 ++-- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/app/controllers/stories_controller.rb b/app/controllers/stories_controller.rb index 2e74543..f4a4e96 100644 --- a/app/controllers/stories_controller.rb +++ b/app/controllers/stories_controller.rb @@ -62,10 +62,7 @@ class StoriesController < ApplicationController s.fetching_ip = request.remote_ip s.url = params[:fetch_url] - sattrs = s.fetched_attributes - sattrs.delete(:content) - - return render :json => sattrs + return render :json => s.fetched_attributes end def new diff --git a/app/models/story.rb b/app/models/story.rb index 3a01524..343adf0 100644 --- a/app/models/story.rb +++ b/app/models/story.rb @@ -727,17 +727,19 @@ class Story < ActiveRecord::Base :title => "", } - begin - s = Sponge.new - s.timeout = 3 - @fetched_attributes[:content] = s.fetch(self.url, :get, nil, nil, { - "User-agent" => "#{Rails.application.domain} for #{self.fetching_ip}" - }, 3) - rescue - return @fetched_attributes + if !@fetched_content + begin + s = Sponge.new + s.timeout = 3 + @fetched_content = s.fetch(self.url, :get, nil, nil, { + "User-agent" => "#{Rails.application.domain} for #{self.fetching_ip}" + }, 3) + rescue + return @fetched_attributes + end end - parsed = Nokogiri::HTML(@fetched_attributes[:content].to_s) + parsed = Nokogiri::HTML(@fetched_content.to_s) # parse best title from html tags # try first, it probably won't have the site diff --git a/spec/models/story_spec.rb b/spec/models/story_spec.rb index 7d1de25..b1eecc8 100644 --- a/spec/models/story_spec.rb +++ b/spec/models/story_spec.rb @@ -93,12 +93,12 @@ describe Story do s = Story.make s.fetched_content = File.read(Rails.root + "spec/fixtures/story_pages/1.html") - s.fetched_title.should == "B2G demo & quick hack // by Paul Rouget" + s.fetched_attributes[:title].should == "B2G demo & quick hack // by Paul Rouget" s = Story.make s.fetched_content = File.read(Rails.root + "spec/fixtures/story_pages/2.html") - s.fetched_title.should == "Google" + s.fetched_attributes[:title].should == "Google" end it "has an underscored url from its title" do