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:
Fetch and parse the RSS feed for the Honeybadger blog
Select the appropriate articles by category
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.
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:
Make the template pretty & test it with our email provider
Add Google Analytics tracking parameters to the links
Add post descriptions to the template