Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated models and added acts_as_auditable link to current work
- Loading branch information
Joshua Clayton
committed
Aug 3, 2008
1 parent
7d724e8
commit 5d0ebe0
Showing
8 changed files
with
55 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,58 @@ | ||
class Feed < ActiveRecord::Base | ||
has_many :feed_statuses | ||
has_many :feed_items | ||
|
||
validates_uniqueness_of :url | ||
|
||
def feed; self.body_from(self.url); end | ||
|
||
def parse_feed | ||
rss = Hash.from_xml(self.feed) | ||
feed_items = self.source == "LastFM" ? rss["recenttracks"]["track"] : rss["rss"]["channel"]["item"] | ||
feed_items.each do |feed_item| | ||
item = initialize_feed_item(feed_item) | ||
item.save if item.valid? | ||
item.save_with_validation(false) if item.is_a?(LastFM) | ||
module Feeds | ||
module Parsing | ||
def self.included(base) | ||
base.send :include, Feeds::Parsing::InstanceMethods | ||
base.extend Feeds::Parsing::ClassMethods | ||
end | ||
end | ||
|
||
class << self | ||
def sources; %w(Twitter LastFM Tumblr Flickr); end | ||
def parse_all | ||
Feed.find(:all).map(&:parse_feed) | ||
module InstanceMethods | ||
def feed | ||
self.body_from(self.url) | ||
end | ||
|
||
def parse_feed | ||
rss = Hash.from_xml(self.feed) | ||
feed_items = self.source == "LastFM" ? rss["recenttracks"]["track"] : rss["rss"]["channel"]["item"] | ||
feed_items.each do |feed_item| | ||
item = initialize_feed_item(feed_item) | ||
item.save if item.valid? | ||
item.save_without_validation if item.is_a?(LastFM) | ||
end | ||
end | ||
|
||
protected | ||
|
||
def body_from(u) | ||
url = URI.parse(u) | ||
req = Net::HTTP::Get.new("#{url.path}?#{url.query}") | ||
res = Net::HTTP.start(url.host, url.port) {|http| http.request(req) } | ||
FeedStatus.create(:response_code => res.code.to_i, :feed => self) | ||
res.body | ||
end | ||
|
||
private | ||
|
||
def initialize_feed_item(feed_item) | ||
item = Object.const_get(self.source).new(feed_item) | ||
item.feed, item.data = self, feed_item | ||
item | ||
end | ||
end | ||
module ClassMethods | ||
def parse_all | ||
Feed.all.map(&:parse_feed) | ||
end | ||
end | ||
end | ||
end | ||
|
||
class Feed < ActiveRecord::Base | ||
include Feeds::Parsing | ||
|
||
protected | ||
SOURCES = %w(Twitter LastFM Tumblr Flickr) | ||
|
||
def body_from(u) | ||
url = URI.parse(u) | ||
req = Net::HTTP::Get.new("#{url.path}?#{url.query}") | ||
res = Net::HTTP.start(url.host, url.port) {|http| http.request(req) } | ||
FeedStatus.create(:response_code => res.code.to_i, :feed => self) | ||
res.body | ||
end | ||
|
||
private | ||
has_many :feed_statuses | ||
has_many :feed_items | ||
|
||
def initialize_feed_item(feed_item) | ||
item = Object.const_get(self.source).new(feed_item) | ||
item.feed, item.data = self, feed_item | ||
item | ||
end | ||
validates_uniqueness_of :url | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,6 @@ def date=(time) | |
end | ||
|
||
def self.types | ||
Feed.sources | ||
Feed::SOURCES | ||
end | ||
end |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters