GeoRSS aggregator and Layar augmented reality server
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

61 lines
1.7 KiB

  1. class Feed < ActiveRecord::Base
  2. has_many :posts, :dependent => :destroy
  3. attr_accessible :title, :url, :description, :generator, :last_fetched, :feed_url
  4. validates_format_of :feed_url, :with => URI::regexp(%w(http https)), :message => "must be a valid URL"
  5. after_create :fetch
  6. def self.fetch_all
  7. Feed.all.each { |f| f.fetch }
  8. end
  9. # Fetch and parse feed contents from web
  10. def fetch
  11. puts "Fetching feed: #{self.feed_url}"
  12. Feedzirra::Feed.add_common_feed_entry_element('georss:point', :as => :point)
  13. Feedzirra::Feed.add_common_feed_entry_element('geo:lat', :as => :geo_lat)
  14. Feedzirra::Feed.add_common_feed_entry_element('geo:long', :as => :geo_long)
  15. Feedzirra::Feed.add_common_feed_element('generator', :as => :generator)
  16. feed = Feedzirra::Feed.fetch_and_parse(self.feed_url)
  17. self.update_attributes(
  18. :title => feed.title,
  19. :url => feed.url,
  20. :description => feed.description,
  21. :generator => feed.generator,
  22. :last_fetched => DateTime.now
  23. )
  24. feed.entries.each do |e|
  25. if e.geo_lat && e.geo_long
  26. latlon = [e.geo_lat, e.geo_long]
  27. elsif e.point
  28. latlon = e.point.split(' ')
  29. else
  30. next
  31. end
  32. attrs = {
  33. :title => e.title,
  34. :url => e.url,
  35. :author => e.author,
  36. :summary => e.summary,
  37. :content => e.content,
  38. :published => e.published,
  39. :guid => e.id,
  40. :lon => latlon[1].to_f,
  41. :lat => latlon[0].to_f
  42. }
  43. # Create a new post or update an existing one
  44. post = Post.find_or_initialize_by_url(e.url)
  45. post.feed = self
  46. post.assign_attributes(attrs)
  47. post.save
  48. end
  49. end
  50. end