Sha256: f260ae43940ce32168a23c0a20099af9bec73f53385f1b7886a6b377c57f4a22

Contents?: true

Size: 976 Bytes

Versions: 1

Compression:

Stored size: 976 Bytes

Contents

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
    
    def format_url url
      url.strip!
      url = "http://#{url}" if url[0..6] != "http://"
      url
    end
    
    def get_document url
      doc = open(url)
      doc
    end
    
    def format_item(link, title)
      "  <li>\n    <a href='#{link}'>#{title}</a>\n  </li>\n"
    end

    def get_news url
      item_string = nil
      item_string = @cache.retrieve(url) if @cache
      
      unless item_string 
	item_string = ""
	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
	
	@cache.save url, item_string if @cache
      end
      
      item_string
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
flannel-0.2.3 lib/flannel/feed_parser.rb