Skip to content

Commit

Permalink
Updated models and added acts_as_auditable link to current work
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua Clayton committed Aug 3, 2008
1 parent 7d724e8 commit 5d0ebe0
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 41 deletions.
85 changes: 50 additions & 35 deletions app/models/feed.rb
@@ -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
2 changes: 1 addition & 1 deletion app/models/feed_item.rb
Expand Up @@ -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.
4 changes: 3 additions & 1 deletion app/views/home/index.html.erb
Expand Up @@ -71,11 +71,13 @@
<dd>Ruby on Rails plugin for easy querying of ActiveRecord models by a string</dd>
<dt><%= link_to "acts_as_archivable", "http://github.com/joshuaclayton/acts_as_archivable/" %></dt>
<dd>Ruby on Rails plugin for easy querying of ActiveRecord models by a date</dd>
<dt><%= link_to "acts_as_auditable", "http://github.com/joshuaclayton/acts_as_auditable/" %></dt>
<dd>Ruby on Rails plugin for easy auditing (not versioning) of ActiveRecord models</dd>
<dt><%= link_to "Alpha Heroes", "http://www.alphaheroes.org/" %></dt>
<dd>Website dedicated to five Marines Alpha Co. 1/24 lost during their tour to Fallujah, Iraq</dd>
</dl>
<% end %>
<% content :seven_twentyfourths, prepend_one_twentyfourth(), append_one_twentyfourth(), :last do %>
<% content :seven_twentyfourths, prepend_one_twentyfourth, append_one_twentyfourth, :last do %>
<h2>Social Networking</h2>
<ul>
<li><%= link_to "GitHub", "http://github.com/joshuaclayton" %></li>
Expand Down
5 changes: 1 addition & 4 deletions config/environment.rb
@@ -1,6 +1,3 @@
RAILS_GEM_VERSION = '2.0.2' unless defined? RAILS_GEM_VERSION

# Bootstrap the Rails environment, frameworks, and default configuration
require File.join(File.dirname(__FILE__), 'boot')

Rails::Initializer.run do |config|
Expand All @@ -9,7 +6,7 @@
:session_key => '_jdclayton_session',
:secret => 'bab6dfb8ac69435a9a1b398c3856c2e563ddaa5409c2a7ca6db1ef55d8cb062af61d57c1d79eae30ad6f7c97a7b56f34452d63c546f269b86d7073dd679cfc5a'
}

config.load_paths += %W( #{RAILS_ROOT}/app/models/feed_items )
config.action_controller.session_store = :active_record_store
config.action_controller.page_cache_directory = "#{RAILS_ROOT}/public/cache/"
end

0 comments on commit 5d0ebe0

Please sign in to comment.