GeoRSS aggregator and Layar augmented reality server
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 

67 lignes
1.7 KiB

  1. class Feed < ActiveRecord::Base
  2. has_many :posts, :dependent => :destroy
  3. validates :title, :presence => true
  4. validates_format_of :feed_url, :with => URI::regexp(%w(http https)), :message => "must be a valid URL"
  5. after_create :get
  6. # Fetch and parse feed contents from web
  7. def self.get_all
  8. Feed.all.each { |f| f.get }
  9. end
  10. def get
  11. puts "Fetching 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(@feed_url)
  17. self.set(
  18. :title => feed.title,
  19. :url => feed.url,
  20. :description => feed.description,
  21. :generator => feed.generator,
  22. :last_fetched => Time.now
  23. )
  24. feed.entries.each do |e|
  25. if e.geo_lat && e.geo_long
  26. latlng = [e.geo_lat, e.geo_long]
  27. elsif e.point
  28. latlng = 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. :loc => {
  41. :lng => latlng[1].to_f,
  42. :lat => latlng[0].to_f
  43. }
  44. }
  45. if Post.where(:url => e.url).size == 0
  46. self.posts << Post.create(attrs)
  47. else
  48. Post.set({:url => e.url}, attrs)
  49. end
  50. end
  51. end
  52. end