GeoRSS aggregator and Layar augmented reality server
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 

64 rindas
1.9 KiB

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