From 9df647d20a17dab967d35b3d6b6b40814e862672 Mon Sep 17 00:00:00 2001 From: Pat Nakajima Date: Tue, 23 Sep 2008 00:32:13 -0400 Subject: [PATCH] dsl-ified feed definitions --- app/models/feeds/blog.rb | 17 ++++++----------- app/models/feeds/delicious.rb | 14 +++++--------- app/models/feeds/flickr.rb | 23 ++++++++++------------- app/models/feeds/twitter.rb | 12 ++++-------- config/initializers/feedable.rb | 7 ++++++- 5 files changed, 31 insertions(+), 42 deletions(-) diff --git a/app/models/feeds/blog.rb b/app/models/feeds/blog.rb index ac8f915..6c9e2e8 100644 --- a/app/models/feeds/blog.rb +++ b/app/models/feeds/blog.rb @@ -1,16 +1,11 @@ class Blog < Feed - entries_become :articles - - def refresh! - entries.each do |entry| - article = articles.build :content => entry.content, :header => entry.title - article.permalink = entry.urls.first - article.created_at = entry.try(:date_published) - article.updated_at = entry.try(:last_updated) - article.save - end - update_attribute :updated_at, Time.now + entries_become :articles do |entry| + article = articles.build :content => entry.content, :header => entry.title + article.permalink = entry.urls.first + article.created_at = entry.try(:date_published) + article.updated_at = entry.try(:last_updated) + article.save end end \ No newline at end of file diff --git a/app/models/feeds/delicious.rb b/app/models/feeds/delicious.rb index 08ad7b8..6813ad3 100644 --- a/app/models/feeds/delicious.rb +++ b/app/models/feeds/delicious.rb @@ -1,14 +1,10 @@ class Delicious < Feed - entries_become :links - - def refresh! - entries.each do |entry| - logger.debug '=> adding entry' - link = links.build :permalink => entry.urls.first, :header => entry.title, :content => entry.content - link.created_at = entry.date_published - link.save - end + entries_become :links do |entry| + logger.debug '=> adding entry' + link = links.build :permalink => entry.urls.first, :header => entry.title, :content => entry.content + link.created_at = entry.date_published + link.save end end \ No newline at end of file diff --git a/app/models/feeds/flickr.rb b/app/models/feeds/flickr.rb index 584dbe8..eda3f66 100644 --- a/app/models/feeds/flickr.rb +++ b/app/models/feeds/flickr.rb @@ -1,17 +1,14 @@ class Flickr < Feed - entries_become :pictures - def refresh! - entries.each do |entry| - doc = Hpricot(entry.content) - picture = pictures.build \ - :permalink => (doc/"img").first['src'].gsub(/_m/, ''), - :header => entry.title, - :cite => entry.urls.first - # picture.created_at = entry.try(:date_published) - picture.updated_at = entry.try(:last_updated) - picture.save - end - update_attribute :updated_at, Time.now + entries_become :pictures do |entry| + doc = Hpricot(entry.content) + picture = pictures.build \ + :permalink => (doc/"img").first['src'].gsub(/_m/, ''), + :header => entry.title, + :cite => entry.urls.first + # picture.created_at = entry.try(:date_published) + picture.updated_at = entry.try(:last_updated) + picture.save end + end \ No newline at end of file diff --git a/app/models/feeds/twitter.rb b/app/models/feeds/twitter.rb index b5391cb..7660f62 100644 --- a/app/models/feeds/twitter.rb +++ b/app/models/feeds/twitter.rb @@ -1,13 +1,9 @@ class Twitter < Feed - entries_become :tweets - - def refresh! - entries.each do |entry| - tweet = tweets.build :permalink => entry.urls.first, :content => entry.content.gsub(/\A\w*:?\s/, '') - tweet.created_at = entry.try(:date_published) - tweet.save ? tweet : tweet.destroy - end + entries_become :tweets do |entry| + tweet = tweets.build :permalink => entry.urls.first, :content => entry.content.gsub(/\A\w*:?\s/, '') + tweet.created_at = entry.try(:date_published) + tweet.save ? tweet : tweet.destroy end end \ No newline at end of file diff --git a/config/initializers/feedable.rb b/config/initializers/feedable.rb index 7cc0fc0..2dbf3b9 100644 --- a/config/initializers/feedable.rb +++ b/config/initializers/feedable.rb @@ -18,9 +18,14 @@ def acts_as_feed end module ClassMethods - def entries_become(post_type) + def entries_become(post_type, &block) self.entry_type = post_type has_many post_type.to_sym, :foreign_key => :feed_id, :dependent => :destroy + define_method(:refresh!) do + entries.each(&block.bind(self)) + self.updated_at = Time.now + self.save + end end end