Browse Source

Use ActiveRecord style

master
Adrian Short 12 years ago
parent
commit
44e5e17a17
1 changed files with 7 additions and 13 deletions
  1. +7
    -13
      app/models/feed.rb

+ 7
- 13
app/models/feed.rb View File

@@ -2,36 +2,30 @@ class Feed < ActiveRecord::Base
has_many :posts, :dependent => :destroy has_many :posts, :dependent => :destroy
attr_accessible :title, :url, :description, :generator, :last_fetched, :feed_url attr_accessible :title, :url, :description, :generator, :last_fetched, :feed_url


validates :title, :presence => true
validates_format_of :feed_url, :with => URI::regexp(%w(http https)), :message => "must be a valid URL" validates_format_of :feed_url, :with => URI::regexp(%w(http https)), :message => "must be a valid URL"
after_create :get
after_create :fetch after_create :fetch
# Fetch and parse feed contents from web

def self.get_all
Feed.all.each { |f| f.get }
def self.fetch_all def self.fetch_all
Feed.all.each { |f| f.fetch } Feed.all.each { |f| f.fetch }
end end


def get
puts "Fetching feed: #{@url}"
# Fetch and parse feed contents from web
def fetch def fetch
puts "Fetching feed: #{self.feed_url}"
Feedzirra::Feed.add_common_feed_entry_element('georss:point', :as => :point) Feedzirra::Feed.add_common_feed_entry_element('georss:point', :as => :point)
Feedzirra::Feed.add_common_feed_entry_element('geo:lat', :as => :geo_lat) Feedzirra::Feed.add_common_feed_entry_element('geo:lat', :as => :geo_lat)
Feedzirra::Feed.add_common_feed_entry_element('geo:long', :as => :geo_long) Feedzirra::Feed.add_common_feed_entry_element('geo:long', :as => :geo_long)
Feedzirra::Feed.add_common_feed_element('generator', :as => :generator) Feedzirra::Feed.add_common_feed_element('generator', :as => :generator)


feed = Feedzirra::Feed.fetch_and_parse(@feed_url)
feed = Feedzirra::Feed.fetch_and_parse(self.feed_url)


self.set(
self.update_attributes(
:title => feed.title, :title => feed.title,
:url => feed.url, :url => feed.url,
:description => feed.description, :description => feed.description,
:generator => feed.generator, :generator => feed.generator,
:last_fetched => Time.now
:last_fetched => DateTime.now
) )


feed.entries.each do |e| feed.entries.each do |e|
@@ -42,8 +36,8 @@ class Feed < ActiveRecord::Base
latlon = e.point.split(' ') latlon = e.point.split(' ')
else else
next next
end
end
attrs = { attrs = {
:title => e.title, :title => e.title,
:url => e.url, :url => e.url,


Loading…
Cancel
Save