The RSS Parser in Ruby's standard library is too strict, and is therefore, I believe, unfit for production. Feedzirra is tolerant of invalid XML, and I recommend it over the RSS module in Ruby.
However, if you know the RSS you're dealing with valid XML 100% of the time, and that you won't get 404 errors or anything else unexpected, then you might like to use it. Here's a little monkey patch to make it slightly safer:
module RSS
class Parser
def self.safe_parse(url, &block)
begin
open(url) do |xml|
feed = RSS::Parser.parse(xml)
yield feed
end
rescue Exception => e
$stdout.puts "Error: #{e}"
end
end
end
end
Use it like so:
RSS::Parser.safe_parse("http://mysite.com/podcast.xml") do |feed|
feed.items.each do |item|
puts item.title
end
end
The only problem with Feedzirra is that it doesn't use Ruby's standard Net::HTTP
. It instead uses a Ruby wrapper for the curl
unix utility, which faster but also means I can't use FakeWeb, my favorite (also, the only) HTTP stubbing library for it. No problem, just a minor inconvenience!