GeoRSS aggregator and Layar augmented reality server
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 

63 řádky
1.9 KiB

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