Module Rudy::Utils::RSSReader
  1. lib/rudy/utils.rb

Rudy::Utils::RSSReader

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

Methods

public instance

  1. run

Public instance methods

run (uri)

Returns a feed as a hash.

  • uri to RSS feed
[show source]
     # File lib/rudy/utils.rb, line 296
296:   def run(uri)
297:     begin
298:       xmlstr = Net::HTTP.get(URI.parse(uri))
299:     rescue SocketError, Errno::ETIMEDOUT
300:       Rudy::Huxtable.le "Connection Error. Check your internets!"
301:     end
302:     
303:     xml = REXML::Document.new xmlstr
304:     
305:     data = { :items => [] }
306:     xml.elements.each '//channel' do |item|
307:       item.elements.each do |e| 
308:         n = e.name.downcase.gsub(/^dc:(\w)/,"\1").to_sym
309:         next if n == :item
310:         data[n] = e.text
311:       end
312:     end
313:     
314:     #data = {
315:     #  :title    => xml.root.elements['channel/title'].text,
316:     #  :link => xml.root.elements['channel/link'].text,
317:     #  :updated => xml.root.elements['channel/lastBuildDate'].text,
318:     #  :uri  => uri,
319:     #  :items    => []
320:     #}
321:     #data[:updated] &&= DateTime.parse(data[:updated])
322:     
323:     xml.elements.each '//item' do |item|
324:       new_items = {} and item.elements.each do |e| 
325:         n = e.name.downcase.gsub(/^dc:(\w)/,"\1").to_sym
326:         new_items[n] = e.text
327:       end
328:       data[:items] << new_items
329:     end
330:     data
331:   end