From 85b78e6dc7318ae283a2714b04e952cf60ee86fe Mon Sep 17 00:00:00 2001 From: Aaron Mueller Date: Sat, 5 Jan 2013 00:28:01 +0100 Subject: [PATCH] Make the feed parsing more robust --- Gemfile.lock | 2 +- app/models/feed.rb | 19 ++++++++++++------- app/models/item.rb | 5 +++-- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index f3c52d4..f801066 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -118,7 +118,7 @@ GEM sprockets (2.1.2) hike (~> 1.2) rack (~> 1.0) - tilt (!= 1.3.0, ~> 1.1) + tilt (~> 1.1, != 1.3.0) sqlite3 (1.3.5) thor (0.14.6) tilt (1.3.3) diff --git a/app/models/feed.rb b/app/models/feed.rb index 24d1203..213a0a2 100644 --- a/app/models/feed.rb +++ b/app/models/feed.rb @@ -1,4 +1,5 @@ require "pp" + class Feed < ActiveRecord::Base has_many :items @@ -10,13 +11,17 @@ class Feed < ActiveRecord::Base def fetch! Feedzirra::Feed.fetch_and_parse(url, :on_success => lambda do |url, feed| - feed.entries.each do |entry| - unless Item.exists?(:url => entry.url) - entry.sanitize! - items << Item.create_from_feed_entry!(entry) - end - end - by_url(url).update_attribute(:has_errors, false) + begin + feed.entries.each do |entry| + unless Item.exists?(:url => entry.url) + entry.sanitize! + items << Item.create_from_feed_entry!(entry) + end + end + by_url(url).update_attribute(:has_errors, false) + rescue Exception => e + pp e + end end, :on_failure => lambda do |url, response_code, response_header, response_body| by_url(url).update_attribute(:has_errors, true) diff --git a/app/models/item.rb b/app/models/item.rb index 6eddd62..176dada 100644 --- a/app/models/item.rb +++ b/app/models/item.rb @@ -9,12 +9,13 @@ class Item < ActiveRecord::Base def self.create_from_feed_entry!(feed_entry) feed_entry.sanitize! + self.create!( :title => feed_entry.title, :url => feed_entry.url, - :author => feed_entry.author, + :author => feed_entry.author || "Anonymous", :published_at => feed_entry.published, - :content => feed_entry.content + :content => feed_entry.content || feed_entry.summary || "No content available" ) end