DEV Community

Mitch Stanley
Mitch Stanley

Posted on • Updated on • Originally published at

Adding RSS Feeds to a Lucky app

Just a quick post for anyone looking to implement RSS Feeds in Crystal Lucky Framework. This post works with Lucky 0.14.1 but it should work with 0.15 as well.

Thanks to @paulcsmith and @jeremywoertink for helping me work this out in Gitter.

If you haven't heard of Lucky, check out the website here. It's a web framework written in Crystal

First, create an Action that inherits from Lucky::Action. We’ll add a method called xml which can be called in each of your actions, passing in the data for the feed. The xml method will then create the xml string with Crystal’s built in XML Builder and iterate over the data that you pass in.

# src/actions/
require "xml"
abstract class XMLAction < Lucky::Action
    def title
        "Website RSS Feed"

    def description
        "Updates for Website"

    def link

    private def xml(articles : ArticleQuery)
        string = "  ", encoding: "UTF-8") do |xml|
                version: "2.0", 
                "xmlns:dc": "",
                "xmlns:content": "",
                "xmlns:atom": "",
                "xmlns:media": ""
                ) do
                xml.element("channel") do
                    xml.element("title") { xml.cdata title }
                    xml.element("description") { xml.cdata description }
                    xml.element("link") { xml.text link }
                    xml.element("generator") { xml.text "Lucky Framework" }
                    xml.element("lastBuildDate") { xml.text Time.utc_now.to_s }
                    xml.element("atom:link") { 
                        xml.attribute "href", "#{link}#{request.path}"
                        xml.attribute "rel", "self"
                        xml.attribute "type", "application/rss+xml"
                    xml.element("ttl") { xml.text "60" }
                    articles.each do |article|
                        xml.element("item") do
                            # title, description, link, category, dc:creator, pubDate, content:encoded
                            xml.element("title") { xml.cdata article.title }
                            if article.meta_description
                                xml.element("description") { xml.cdata article.meta_description.not_nil! }
                            xml.element("link") { xml.text "#{link}articles/#{article.slug}" }             xml.element("dc:creator") { xml.cdata "Author Name" }
                            xml.element("pubDate") { xml.text article.created_at.to_s }
                            if article.og_image
                                xml.element("media:content") do
                                    xml.attribute "url", "article.og_image"
                                    xml.attribute "medium", "image"
                            if article.content
                                content = Markdown.to_html(article.content.not_nil!)
                                xml.element("content:encoded") { xml.cdata content }

        end, content_type: "text/xml; charset=utf-8", body: string status: 200)

Although this abstract class looks quite large it’s not really doing much except generating the XML to output to the browser. By doing this it will greatly simplify our action classes.

In the example we’re passing in an ArticleQuery which you will need to implement in your own app. For reference, mine has a scope for published so I can keep unpublished articles from being viewed.

class ArticleQuery < Article::BaseQuery
  def published

The Article model has the following attributes: title, meta_description (optional), slug, content (optional), og_image (optional). You will need to update this for your own purposes as well.

At the end of the XMLAction#xml method we’re using Lucky::TextResponse to send the XML string to the browser. Note that application/rss+xml is the proper content type to respond with, however, if you want it to be viewable in a web browser you need to use text/xml; charset=utf-8 (This is how Ghost does it at least).

Example Action

Since all the XML logic is in the XMLAction we can make our actions really clean. In the example below Rss::Index overrides the feed title and then passes in a list of published articles through the xml method.

# src/actions/rss/
class Rss::Index < ::XMLAction

    def title
        "Latest Dailies"

    get "/feeds/all.rss" do
        articles =
        xml articles


Top comments (2)

jwoertink profile image
Jeremy Woertink

This is great! You should tag this with lucky so it shows up in the list of all the other lucky posts :D

mitchartemis profile image
Mitch Stanley

Done! 😀