GeoRSS aggregator and Layar augmented reality server
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

feed.rb 1.7 KiB

12 yıl önce
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. after_create :fetch
  7. # Fetch and parse feed contents from web
  8. def self.get_all
  9. Feed.all.each { |f| f.get }
  10. def self.fetch_all
  11. Feed.all.each { |f| f.fetch }
  12. end
  13. def get
  14. puts "Fetching feed: #{@url}"
  15. def fetch
  16. Feedzirra::Feed.add_common_feed_entry_element('georss:point', :as => :point)
  17. Feedzirra::Feed.add_common_feed_entry_element('geo:lat', :as => :geo_lat)
  18. Feedzirra::Feed.add_common_feed_entry_element('geo:long', :as => :geo_long)
  19. Feedzirra::Feed.add_common_feed_element('generator', :as => :generator)
  20. feed = Feedzirra::Feed.fetch_and_parse(@feed_url)
  21. self.set(
  22. :title => feed.title,
  23. :url => feed.url,
  24. :description => feed.description,
  25. :generator => feed.generator,
  26. :last_fetched => Time.now
  27. )
  28. feed.entries.each do |e|
  29. if e.geo_lat && e.geo_long
  30. latlng = [e.geo_lat, e.geo_long]
  31. elsif e.point
  32. latlng = e.point.split(' ')
  33. else
  34. next
  35. end
  36. attrs = {
  37. :title => e.title,
  38. :url => e.url,
  39. :author => e.author,
  40. :summary => e.summary,
  41. :content => e.content,
  42. :published => e.published,
  43. :guid => e.id,
  44. :loc => {
  45. :lng => latlng[1].to_f,
  46. :lat => latlng[0].to_f
  47. }
  48. }
  49. if Post.where(:url => e.url).size == 0
  50. self.posts << Post.create(attrs)
  51. else
  52. Post.set({:url => e.url}, attrs)
  53. end
  54. end
  55. end
  56. end