Skip to content

Instantly share code, notes, and snippets.

@bricker
Created November 16, 2012 08:25

Revisions

  1. bricker revised this gist Nov 16, 2012. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions rss_safe_parse.md
    Original file line number Diff line number Diff line change
    @@ -28,3 +28,5 @@ RSS::Parser.safe_parse("http://mysite.com/podcast.xml") do |feed|
    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!
  2. bricker revised this gist Nov 16, 2012. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions rss_safe_parse.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,6 @@
    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:
    The RSS Parser in Ruby's standard library is too strict, and is therefore, I believe, unfit for production. [Feedzirra](https://github.com/pauldix/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:

    ```ruby
    module RSS
    @@ -25,4 +27,4 @@ RSS::Parser.safe_parse("http://mysite.com/podcast.xml") do |feed|
    puts item.title
    end
    end
    ```
    ```
  3. bricker created this gist Nov 16, 2012.
    28 changes: 28 additions & 0 deletions rss_safe_parse.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    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:

    ```ruby
    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:

    ```ruby
    RSS::Parser.safe_parse("http://mysite.com/podcast.xml") do |feed|
    feed.items.each do |item|
    puts item.title
    end
    end
    ```