rss.rb 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. require 'rss'
  2. class Sessions::Backend::Rss
  3. def initialize( user, client, client_id )
  4. @user = user
  5. @client = client
  6. @client_id = client_id
  7. end
  8. def collection_key
  9. "rss::load::#{ self.class.to_s }::#{ @user.id }"
  10. end
  11. def load
  12. # check timeout
  13. cache = Sessions::CacheIn.get( self.collection_key )
  14. return cache if cache
  15. url = 'http://www.heise.de/newsticker/heise-atom.xml'
  16. rss_items = Rss.fetch( url, 8 )
  17. # set new timeout
  18. Sessions::CacheIn.set( self.collection_key, rss_items, { :expires_in => 1.hours } )
  19. rss_items
  20. end
  21. def client_key
  22. "rss::load::#{ self.class.to_s }::#{ @user.id }::#{ @client_id }"
  23. end
  24. def push
  25. # check timeout
  26. timeout = Sessions::CacheIn.get( self.client_key )
  27. return if timeout
  28. # set new timeout
  29. Sessions::CacheIn.set( self.client_key, true, { :expires_in => 5.minutes } )
  30. data = self.load
  31. return if !data||data.empty?
  32. if !@client
  33. return {
  34. :event => 'rss_rebuild',
  35. :collection => 'dashboard_rss',
  36. :data => data,
  37. }
  38. end
  39. @client.log 'notify', "push rss for user #{@user.id}"
  40. @client.send({
  41. :event => 'rss_rebuild',
  42. :collection => 'dashboard_rss',
  43. :data => data,
  44. })
  45. end
  46. end