module Rudy::Utils::RSSReader

Rudy::Utils::RSSReader

A rudimentary way to read an RSS feed as a hash. Adapted from: snippets.dzone.com/posts/show/68

Public Instance Methods

run(uri) click to toggle source

Returns a feed as a hash.

  • uri to RSS feed

# File lib/rudy/utils.rb, line 296
def run(uri)
  begin
    xmlstr = Net::HTTP.get(URI.parse(uri))
  rescue SocketError, Errno::ETIMEDOUT
    Rudy::Huxtable.le "Connection Error. Check your internets!"
  end
  
  xml = REXML::Document.new xmlstr
  
  data = { :items => [] }
  xml.elements.each '//channel' do |item|
    item.elements.each do |e| 
      n = e.name.downcase.gsub(/^dc:(\w)/,"\1").to_sym
      next if n == :item
      data[n] = e.text
    end
  end
  
  #data = {
  #  :title    => xml.root.elements['channel/title'].text,
  #  :link => xml.root.elements['channel/link'].text,
  #  :updated => xml.root.elements['channel/lastBuildDate'].text,
  #  :uri  => uri,
  #  :items    => []
  #}
  #data[:updated] &&= DateTime.parse(data[:updated])
  
  xml.elements.each '//item' do |item|
    new_items = {} and item.elements.each do |e| 
      n = e.name.downcase.gsub(/^dc:(\w)/,"\1").to_sym
      new_items[n] = e.text
    end
    data[:items] << new_items
  end
  data
end