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.
 
 
 
 
 

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