Err the Blog Atom Feed Icon
Err the Blog
Rubyisms and Railities
  • “Feeds for Free”
    – Chris on January 23, 2008

    And money for nothing. Or something like that? Sorry, Mark Knopfler. I’ll pay more attention next time.

    Anyways, let us be painfully aware that we can get Atom feeds for free. Not as in beer or speech, but as in ‘zero lines of code.’ How? Microformats.

    You and meFormats

    Almost a year has past since we last spoke of microformats, and way more than a year since our first encounter. Seems like only yesterday.

    Remember hAtom? It’s like Atom, only embedded into your existing content’s HTML pages. The mofo site references the following example:

    A normal, typical blog post:

    <div class="post">
      <h3>Megadeth Show Last Night</h3>
      <span class="subtitle">Posted by Chris on June 4th</span>
      <div class="content">
        Went to a show last night. Megadeth.
        It was alright.
      </div>
    </div>
    

    The same post with hAtom superpowers:

    <div class="post hentry">
      <h3 class="entry-title">Megadeth Show Last Night</h3>
      <span class="subtitle">Posted by
        <span class="author vcard fn">Chris</span>
        on
        <abbr class="updated" title="2006-06-04T10:32:10Z">June 4th</abbr>
      </span>
      <div class="content entry-content">
        Went to a show last night. Megadeth. It was alright.
      </div>
    </div>
    

    To you and I, eagerly searching for a review of last summer’s Megadeth show, there is no difference between the two. Our browsers render them the same. To a machine, however, the second post is chock full of semantic goodness.

    This semantic goodness represents, in our HTML, the same information an Atom feed would provide. This leaves us with two paths of action for gettin’ our feed on: we can wait for feed readers to start speaking hAtom fluently, or we can have someone translate hAtom to Atom for us.

    Subtlely Free Feeds

    One year ago today Subtlety was released. Today it is re-released with a new feature: it can convert a page containing hAtom entries into an Atom feed. This means your feeds are now officially free.

    We’ve actually been doing this for a while right here on Err. Our Feedburner feed points to this url: http://subtlety.errtheblog.com/O_o/29f.xml. It’s an Atom feed generated by Subtlety after parsing the hAtom elements on this site. On Err the Blog.

    My ozmm blog is a static blog with no special RSS code. Instead, I point the Feedburner URL at a Subtlety Atom feed which is generated from the hAtom in the posts. Our Dynamite blog uses the same trick. See the pattern?

    There’s no reason to ever write your own Atom feeds anymore. Sorry.

    But what if I don’t want you hosting my feeds?

    That’s fine, and acceptable. How about I just hand you the technology to do this on your own?

    It goes like this:

    $ gem install mofo
    $ cd rails_app/vendor/plugins
    $ gem unpack mofo
    

    Then, here’s your controller:

    class PostsController < ApplicationController
      def index
        @posts = Post.find(:all)
      end
    
      def atom
        target = url_for(:action => :index)
        render :xml => hEntry.find(target).to_atom(:title => 'whatever')
      end
    end
    

    You can use this trick for dynamically generated feeds (changelogs or activity feeds, perhaps) or whatever else. Thanks, mofo.

    Last Step: Cut the Code

    Now go through your app and remove all the Atom code. Drop those extra plugins, remove those xml templates, cut out all the special logic, and enjoy simple Subtlety or profound mofo.

    Have fun.

  • Peter Dierx, about 5 hours later:

    Wicked !! Thx

  • Rob Sanheim, about 6 hours later:

    You guys rock so hard, you should start a megadeth blog.

  • Eivind Uggedal, about 6 hours later:

    I’ve been doing this for a while with my journal using “hAtom2atom:”http://lukearno.com/projects/hatom2atom/. This tools creates valid atom while you’re implementation currently don’t. Seems you forgot that one can use a global author and not need to have an author for each hentry.

  • Martin Probst, about 7 hours later:

    Isn’t there a race condition in the atom call? As in: Rails is single threaded. If the request that Mofo (presumably) sends to this host for the index page is dispatched to the same mongrel instance (or whatever webserver you use), it will be queued to wait for the original request to ‘atom’ to finish, which is forever and will give a timeout. No?

  • Anil Wadghule, about 7 hours later:

    You rock man! It means that you never have to write your own RSS feeds. It is just awesome!

  • Jesse Newland, about 8 hours later:

    Microformats FTW. An hCard → LDAP proxy would be a snap, based mostly on this:

    http://thoughtbot.com/projects/ldap-ar-gateway

  • Matthijs Langenberg, about 11 hours later:

    Really cool, didn’t know it was possible. Webservices that extract microformats from HTML really rock!

  • Chris, about 18 hours later:

    Eivind: We used to use that service but found it to be unreliable. Thus, we wrote our own. And oh yeah, the Err feed validates. Patches welcome!

  • Geoffrey Grosenbach, about 18 hours later:

    @RobSanheim: You may be interested in an audio recording currently on my hard drive where a certain blog celebrity plays Megadeth tunes on his guitar.

  • Nilesh, 2 days later:

    hi my name is nilesh

  • Parker, 4 days later:

    You might also consider Dapper. No need to use a specific microformat.

  • Alastair Brunton, 5 days later:

    @martin is right I think. There is a race condition in the atom call and you get a timeout error in dev mode.

    Maybe change the implementation to use a ActionController:TestRequest to achieve this internally?

  • Eric Wahlforss, 8 days later:

    Supercool stuff.

  • Justin Blake, 14 days later:

    It’s getting harder and harder for me to keep ignoring microformats…

  • Stephen Touset, 19 days later:

    This won’t work on development mode, since it requires doing another GET in parallell. Perhaps some render_to_string magic?

  • Nathan Rambarran, about 1 month later:

    To get around the race condition in development just start up another server on saw port 3001. Then just set the host like so

    url_for(:action => :index, :host => ‘localhost:3001’).

  • Evan, about 1 month later:

    What about RDFa?

  • Stephen Touset, 2 months later:

    Getting around the race condition in development does not get around the race in production.

    Rails is non-threaded, so if your mofo request happens to hit the same server as the initial atom request, the request will hang. Best I can tell, there is no way to work around this, save not performing an additional request.

  • Scott Ballantyne, 3 months later:

    Wow, the relevance of your blog is staggering, and I’m here to praise and to you to boost my own pagerank! Back to keeping my comment relevant. So you used to work for CHOW… Do you have any advise about microformats for recipes? I’m working on a site that is like a delicious for recipes… Reci-P… go figure… but we are trying to hook it up so the recipes are semantically organized… We’ve got tags and I’ve hooked them with the rel-tag semantic markup.. got any pointers for recipe ingredients, units of measure, styles of cooking? Anything would be a big help. Another question. Why is this blog so inactive? You have a good thing going. Why stop?

  • Dimitri, 4 months later:

    Hey there – running your suggestion on my site: VibeReview (NSFW, by the way) and I get “execution expired” errors from time to time. I am unable to replicate it reliably and I’m sorry to say that I don’t have much more info, but has anyone here had the same problem? The top line in the backtrace is this: timeout.rb:54:in `open’ and the message is simply “execution expired”

    Thanks!

  • Twenty people have commented.
    Chime in.
    Sorry, no more comments :(
This is Err, the weblog of PJ Hyett and Chris Wanstrath.
All original content copyright ©2006-2008 the aforementioned.