lib/flannel/feed_parser.rb in flannel-0.2.6 vs lib/flannel/feed_parser.rb in flannel-0.2.7

- old
+ new

@@ -1,6 +1,5 @@ -require 'hpricot' require 'open-uri' module Flannel class FeedParser def initialize cache=nil @@ -17,12 +16,11 @@ url = "http://#{url}" if url[0..6] != "http://" url end def get_document url - doc = open(url) - doc + URI.parse(url).read end def format_item(link, title) " <li>\n <a href='#{link}'>#{title}</a>\n </li>\n" end @@ -31,20 +29,38 @@ item_string = nil item_string = @cache.retrieve(url) if @cache unless item_string item_string = "" - doc = Hpricot.XML(get_document(url)) + doc = get_document(url) + items = get_items(doc) - (doc/"item").each do |item| - link = (item/"link").inner_html - title = (item/"title").inner_html + items.each do |item| + link = inner_html(item, "link") + title = inner_html(item, "title") item_string << format_item(link, title) end @cache.save url, item_string if @cache end item_string + end + + def get_items text + items = text[/<item>.*<\/item>/mi] + + return [] unless items + + items.split(/<\/?item>/).reject { |item| /\A\s*\z/ =~ item } + end + + def inner_html text, tag + regex = Regexp.compile "<#{tag}>(.*)<\/#{tag}>?" + + matches = regex.match text + return "" unless matches + + matches.captures[0] end end end