RESTfully Forward Users to FeedBurner in Rails

After launching my new site, I realized I had forgotten to use FeedBurner to track my RSS feed subscribers. Because FeedBurner requires that you forward users to your feed on their servers, I needed a way to forward users along, but I also wanted to make sure that if FeedBurner’s spider came to the same feed URL, they would be served the source RSS instead of being forwarded. My solution ended up like this:

class NotebooksController < ApplicationController
  def show
    respond_to do |format|
      format.html
      format.rss do
        unless request.env['HTTP_USER_AGENT'] =~ /feedburner/i
          redirect_to 'http://feeds.feedburner.com/KyleSlattery'
        end
      end
    end
  end
end

If you take a close look at the, all I’m doing is checking the user agent of the request, and unless it’s FeedBurner, I redirect the user to the “burned” feed. If it is FeedBurner, things go ahead normally, and the RSS feed gets rendered.

This means that instead of creating separate actions, one that forwards to FeedBurner, and one that renders the RSS, I just have one action/URL that does both: http://kyleslattery.com/notebook.rss. So far, it’s worked great, and doing it this way really helped to keep my code clean and RESTful.

Posted on August 26, 2008
Tagged with: , , , ,

3 Comments

Avatar

Colin Devroe 26 Aug 2008 at 8:36AM

My favorite part is the end, end, end, end, end. :)

Avatar

redconfetti 16 Mar 2009 at 9:22PM

Awesome. Thanks for the tip. Saved me some time :)

Avatar

Kyle 16 Mar 2009 at 11:35PM

Glad it helped, redconfetti!

Leave A Comment

Ajax-loader