Module: Rudy::Utils::RSSReader

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

Overview

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



329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# File 'lib/rudy/utils.rb', line 329

def run(uri)
  begin
    xmlstr = Net::HTTP.get(URI.parse(uri))
  rescue SocketError, Errno::ETIMEDOUT
    STDERR.puts "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