Skip to content

Instantly share code, notes, and snippets.

@bricker
Created November 16, 2012 08:25
Show Gist options
  • Save bricker/4085442 to your computer and use it in GitHub Desktop.
Save bricker/4085442 to your computer and use it in GitHub Desktop.

The RSS Parser in Ruby's standard library is too strict, and too slow, and is therefore unfit for production, in my opinion. 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment