Advertisement
Guest User

feed.rb

a guest
Jan 22nd, 2013
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rails 2.34 KB | None | 0 0
  1. class Feed
  2.  
  3.     attr_accessor :feeds, :feed_urls
  4.     def initialize *args
  5.         @feed_urls = ["http://api.twitter.com/1/statuses/user_timeline.rss?screen_name=healthteacher",
  6.             "http://pinterest.com/youthhealth/feed.rss",
  7.             "http://pipes.yahoo.com/pipes/pipe.run?_id=5600c7a8bccac4645c74f24cb7513046&_render=rss"]
  8.                 if args.present?
  9.                     puts args.join(', ')
  10.                 end
  11.         #@feeds = Feedzirra::Feed.fetch_and_parse(@feed_urls)
  12.     end
  13.        
  14.         def fetch
  15.             sendback = {}
  16.             hydra = Typhoeus::Hydra.new
  17.             #Set behavior of Hydra Cache Getter
  18.             hydra.cache_getter do |request|
  19.              Rails.cache.read(request.cache_key) rescue nil
  20.             end
  21.             #Set behavior of Hydra Cache Setter
  22.             hydra.cache_setter do |request|
  23.              Rails.cache.write(request.cache_key, request.response ,expires_in: request.cache_timeout)
  24.             end
  25.            
  26.             #Iterate through requests
  27.             @feed_urls.each do |r|
  28.                 #Sets the cache key to the api query
  29.                 #Create the request that will use the cached response if Rails.cache exists
  30.                 #otherwise will query the api
  31.                 request = Typhoeus::Request.new(r,cache_timeout: 1.day)
  32.                 request.on_complete do |response|
  33.                    
  34.                     begin
  35.                         f = Feedzirra::Feed.parse(response.body)
  36.                         sendback[r] = f.entries
  37.                     rescue Exception => e
  38.                         sendback[r] = nil
  39.                     end
  40.                 end
  41.                 hydra.queue request
  42.             end
  43.             hydra.run
  44.             @feeds = sendback
  45.         end
  46.    
  47.     def twitter(msg=3)
  48.             unless @feeds['http://api.twitter.com/1/statuses/user_timeline.rss?screen_name=healthteacher'].blank?
  49.             out = []
  50.             msg.times do |x|
  51.                out << @feeds['http://api.twitter.com/1/statuses/user_timeline.rss?screen_name=healthteacher'].entries[x]
  52.             end
  53.                 out
  54.             end
  55.     end
  56.     def pinterest(msg=2)
  57.                 unless @feeds['http://pinterest.com/youthhealth/feed.rss'].blank?
  58.             out = []
  59.             msg.times do |x|        
  60.                 out << @feeds['http://pinterest.com/youthhealth/feed.rss'].entries[x]
  61.             end
  62.                     out
  63.           end
  64.     end
  65.     def facebook(msg=1)
  66.                 unless @feeds['http://pipes.yahoo.com/pipes/pipe.run?_id=5600c7a8bccac4645c74f24cb7513046&_render=rss'].blank?
  67.                     out = []
  68.                 msg.times do |x|        
  69.                 out << @feeds['http://pipes.yahoo.com/pipes/pipe.run?_id=5600c7a8bccac4645c74f24cb7513046&_render=rss'].entries[x]
  70.             end
  71.                     out
  72.           end
  73.     end
  74.    
  75. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement