Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Feed
- attr_accessor :feeds, :feed_urls
- def initialize *args
- @feed_urls = ["http://api.twitter.com/1/statuses/user_timeline.rss?screen_name=healthteacher",
- "http://pinterest.com/youthhealth/feed.rss",
- "http://pipes.yahoo.com/pipes/pipe.run?_id=5600c7a8bccac4645c74f24cb7513046&_render=rss"]
- if args.present?
- puts args.join(', ')
- end
- #@feeds = Feedzirra::Feed.fetch_and_parse(@feed_urls)
- end
- def fetch
- sendback = {}
- hydra = Typhoeus::Hydra.new
- #Set behavior of Hydra Cache Getter
- hydra.cache_getter do |request|
- Rails.cache.read(request.cache_key) rescue nil
- end
- #Set behavior of Hydra Cache Setter
- hydra.cache_setter do |request|
- Rails.cache.write(request.cache_key, request.response ,expires_in: request.cache_timeout)
- end
- #Iterate through requests
- @feed_urls.each do |r|
- #Sets the cache key to the api query
- #Create the request that will use the cached response if Rails.cache exists
- #otherwise will query the api
- request = Typhoeus::Request.new(r,cache_timeout: 1.day)
- request.on_complete do |response|
- begin
- f = Feedzirra::Feed.parse(response.body)
- sendback[r] = f.entries
- rescue Exception => e
- sendback[r] = nil
- end
- end
- hydra.queue request
- end
- hydra.run
- @feeds = sendback
- end
- def twitter(msg=3)
- unless @feeds['http://api.twitter.com/1/statuses/user_timeline.rss?screen_name=healthteacher'].blank?
- out = []
- msg.times do |x|
- out << @feeds['http://api.twitter.com/1/statuses/user_timeline.rss?screen_name=healthteacher'].entries[x]
- end
- out
- end
- end
- def pinterest(msg=2)
- unless @feeds['http://pinterest.com/youthhealth/feed.rss'].blank?
- out = []
- msg.times do |x|
- out << @feeds['http://pinterest.com/youthhealth/feed.rss'].entries[x]
- end
- out
- end
- end
- def facebook(msg=1)
- unless @feeds['http://pipes.yahoo.com/pipes/pipe.run?_id=5600c7a8bccac4645c74f24cb7513046&_render=rss'].blank?
- out = []
- msg.times do |x|
- out << @feeds['http://pipes.yahoo.com/pipes/pipe.run?_id=5600c7a8bccac4645c74f24cb7513046&_render=rss'].entries[x]
- end
- out
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement