Module: Rudy::Utils::RSSReader

Extended by:
RSSReader
Included in:
RSSReader
Defined in:
lib/rudy/utils.rb

Overview

Rudy::Utils::RSSReader

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

Instance Method Summary collapse

Instance Method Details

#run(uri) ⇒ Object

Returns a feed as a hash.

  • uri to RSS feed



296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# 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