lib/flannel/feed_parser.rb in flannel-0.1.5 vs lib/flannel/feed_parser.rb in flannel-0.2.1

- old
+ new

@@ -1,10 +1,14 @@ require 'hpricot' require 'open-uri' module Flannel class FeedParser + def initialize params={} + @cache = params[:cache] if params.has_key?(:cache) + end + def sub_feeds(url) url = format_url(url) get_news(url) end @@ -13,30 +17,30 @@ url = "http://#{url}" if url[0..6] != "http://" url end def get_document url - open(url) + doc = @cache.retrieve(url) if @cache + doc = open(url) unless doc + @cache.save url, doc if @cache + doc end def format_item(link, title) " <li>\n <a href='#{link}'>#{title}</a>\n </li>\n" end def get_news url item_string = "" - #begin - doc = Hpricot.XML(get_document(url)) - (doc/"item").each do |item| - link = (item/"link").inner_html - title = (item/"title").inner_html - item_string << format_item(link, title) - end - - item_string - #rescue - # "Error retrieving data." - #end + doc = Hpricot.XML(get_document(url)) + + (doc/"item").each do |item| + link = (item/"link").inner_html + title = (item/"title").inner_html + item_string << format_item(link, title) + end + + item_string end end end