Commit Redux

A software development blog in commit-sized retrospectives

Building an Atom (RSS) feed

Tuesday May 13 2025 • 06:39 AM

Like the BashBlog announcement post I shared a couple of days ago says, “a blog’s magic is in the RSS feed”, so this commit was dedicated to building it!

First I’m installing the builder gem that ships with Rails since it provides all the necessary builder objects for generating XML markup and data structures.

Next, I’m creating a route at /feed.xml (the way Jekyll does it) which will return the generated Atom/XML file. Within this block I define an instance variable for the posts I want to include in the feed, set the content_type header to application/atom+xml, and call the rendering method with the :atom template as its only argument.


get '/feed.xml' do
  @posts = Post.order(created_at: :desc).limit(20)
  content_type 'application/atom+xml'
  builder :atom
end

Atom Feed Template


xml.instruct! :xml, version: "1.0", encoding: "UTF-8"

xml.feed xmlns: "http://www.w3.org/2005/Atom" do
  xml.id         "https://commit-redux.enocc.com"
  xml.title      "Commit Redux"
  xml.updated    (@posts.first&.updated_at || Time.now).iso8601
  xml.link       href: "https://commit-redux.enocc.com/feed.xml", rel: "self"
  xml.link       href: "https://commit-redux.enocc.com"

  @posts.each do |post|
    xml.entry do
      xml.title     post.title
      xml.id        "https://commit-redux.enocc.com/posts/#{post.id}"
      xml.link      href: "https://commit-redux.enocc.com/posts/#{post.id}"
      xml.updated   post.updated_at.iso8601
      xml.published post.created_at.iso8601
      xml.summary   summarize_post_content(post.content, 200)
      xml.content   post.content, type: "html"
    end
  end

end

For the xml.summary object I had issues with the built-in truncate method (something about being unable to fetch an integer?!) so I opted for defining my own little helper called summarize_post_content which grabs the first 200 characters of a post’s content and appends an ellipsis.

To make sure the feed is detected automatically by RSS clients, I’ll add a link tag to the layout template’s head element.

I use Vivaldi as my main web browser and it comes with an RSS previewer, so that’s what I used to check the Atom/RSS feed had been constructed successfully. A couple of days ago I wrote about migrating PowRSS from Heroku to Mojave, so I also checked this site’s RSS feed over on the app and everything seems to be working 🥳

The last step will be adding an RSS icon somewhere on the site’s header or footer, but that’s beyond the scope of this commit :-)