Skip to content

Commit

Permalink
dsl-ified feed definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
nakajima committed Sep 23, 2008
1 parent 7eb5061 commit 9df647d
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 42 deletions.
17 changes: 6 additions & 11 deletions 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
14 changes: 5 additions & 9 deletions 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
23 changes: 10 additions & 13 deletions 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
12 changes: 4 additions & 8 deletions 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
7 changes: 6 additions & 1 deletion config/initializers/feedable.rb
Expand Up @@ -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

Expand Down

0 comments on commit 9df647d

Please sign in to comment.