Let's build an RSS to email digest script with Ruby

I needed a script that will fetch our most recent blog posts and output a "digest" HTML email that I can personalize. In this post we walk through the process of creating it. You'll learn about fetching and parsing RSS as well as templating with ERB. Yes! You can use ERB outside of Rails!

Wistia always has great content I want something like this

Setting up a newsletter has been on my todo list for way too long. Today is the day I'm going to make it happen. If you'd like to sign up you can do so here.

I don't really like long-copy newsletters. The ones I do like are curated digests of interesting content. RubyWeekly comes do mind. So does Wistia's blog digest.

Putting these digests together manually takes too much time. But going full-auto is too impersonal. So what I want is a semi-automated process. I need a script that will fetch our most recent blog posts and output an HTML email that I can personalize.

So let's build it! It'll be a fun little project with only one user to make happy - me!

The Game Plan

Our script needs to do a few things:

  1. Fetch and parse the RSS feed for the Honeybadger blog

  2. Select the appropriate articles by category

  3. Render the collection of articles via an ERB template

It'll be run from the command line and print its results to STDOUT.

Fetching and Parsing Feeds in Ruby

Did you know that Ruby's standard library ships with a module for producing and consuming RSS & ATOM feeds? For our use case it couldn't get much simpler. Here's how it works:

require 'rss'

feed = RSS::Parser.parse('https://www.honeybadger.io/blog/feed/')
feed.items.each do |item|
  puts item.title
end

The module even fetches the feed for us. Talk about Service!

Working with Categories

I don't want to send subscribers links they're not interested in, so I'm going to filter articles by category. While Ruby's RSS library has a categories method, it returns an array of XML node objects. I need the category names, so I wrap the RSS items in a decorator class called Article.

Now I can easily select only the articles in the category "How To".

require 'rss'
require 'delegate'

class Article < SimpleDelegator
  def category_names
    categories.map &:content
  end
end

feed = RSS::Parser.parse('https://www.honeybadger.io/blog/feed/')
articles = feed.items.map { |o| Article.new(o) }.select { |a| a.category_names.include?("How To") }

Rendering the Template

Since this is going to be an email without very much markup, I'm just going to use ERB for templating. As you can see below, I put the template and the rendering code together in a class called DigestView. For such a small, single-purpose template it seemed overkill to split it into a separate file.

The final output is printed to STDOUT. This will let me pipe the output into OSXs pbcopy command, copying the output to the clipboard so I can paste it into our mail system.

require 'rss'
require 'delegate'
require 'erb'

class Article < SimpleDelegator
  def category_names
    categories.map &:content
  end
end

class DigestView
  attr_accessor :articles

  def initialize(articles)
    @articles = articles
  end

  def render
    ERB.new(template, 0, '>').result(binding)
  end

  def template
%{<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head>
<body>
<h1>Headline: Replace me</h1>
<p>Intro paragraph: Replace me.</p>
<ul>
<% for article in @articles %>
  <li>
    <a href="<%= article.link %>">
  </li>
<% end %>
</ul>
</body>
</html>}
  end
end

feed = RSS::Parser.parse('https://www.honeybadger.io/blog/feed/')
articles = feed.items.map { |o| Article.new(o) }.select { |a| a.category_names.include?("How To") }
printf DigestView.new(articles).render

This is what the output looks like:

Output of our blog digest generator. Output of our blog digest generator.

Future Work

I'll need to do a bit more before this is ready for production. But these are mostly customizations specific to Honeybadger, and which wouldn't be very useful otherwise. Here's my strike list for the rest of the day:

  1. Make the template pretty & test it with our email provider

  2. Add Google Analytics tracking parameters to the links

  3. Add post descriptions to the template

What to do next:
  1. Try Honeybadger for FREE
    Honeybadger helps you find and fix errors before your users can even report them. Get set up in minutes and check monitoring off your to-do list.
    Start free trial
    Easy 5-minute setup — No credit card required
  2. Get the Honeybadger newsletter
    Each month we share news, best practices, and stories from the DevOps & monitoring community—exclusively for developers like you.
    author photo

    Starr Horne

    Starr Horne is a Rubyist and Chief JavaScripter at Honeybadger.io. When she's not neck-deep in other people's bugs, she enjoys making furniture with traditional hand-tools, reading history and brewing beer in her garage in Seattle.

    More articles by Starr Horne
    Stop wasting time manually checking logs for errors!

    Try the only application health monitoring tool that allows you to track application errors, uptime, and cron jobs in one simple platform.

    • Know when critical errors occur, and which customers are affected.
    • Respond instantly when your systems go down.
    • Improve the health of your systems over time.
    • Fix problems before your customers can report them!

    As developers ourselves, we hated wasting time tracking down errors—so we built the system we always wanted.

    Honeybadger tracks everything you need and nothing you don't, creating one simple solution to keep your application running and error free so you can do what you do best—release new code. Try it free and see for yourself.

    Start free trial
    Simple 5-minute setup — No credit card required

    Learn more

    "We've looked at a lot of error management systems. Honeybadger is head and shoulders above the rest and somehow gets better with every new release."
    — Michael Smith, Cofounder & CTO of YvesBlue

    Honeybadger is trusted by top companies like:

    “Everyone is in love with Honeybadger ... the UI is spot on.”
    Molly Struve, Sr. Site Reliability Engineer, Netflix
    Start free trial