Created
June 14, 2019 10:26
-
-
Save acoustep/8eeea97e5a91ad3cb27bf5129f5f593e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# src/actions/rss/show.cr | |
#... | |
xml ArticlesXmlSerializer.new(articles, {title: "Feed Name", description: "Here's the descr", path: request.path }).render | |
#... | |
# src/actions/xml_action.cr | |
abstract class XMLAction < Lucky::Action | |
private def xml(body : String) | |
Lucky::TextResponse.new(context, content_type: "text/xml; charset=utf-8", body: body, status: 200) | |
end | |
end | |
# src/serializers/article_xml_serializer.cr | |
require "xml" | |
class ArticlesXmlSerializer < Lucky::Serializer | |
property title : String = "" | |
property description : String = "" | |
property base_url : String = "https://example.dev" | |
property path : String = "" | |
def initialize(@articles : ArticleQuery, options = {} of String => String) | |
@title = options.fetch("title", @title) | |
@description = options.fetch("description", @description) | |
@base_url = options.fetch("base_url", @base_url) | |
@path = options.fetch("path", @path) | |
end | |
def render | |
XML.build(indent: " ", encoding: "UTF-8") do |xml| | |
xml.element( | |
"rss", | |
version: "2.0", | |
"xmlns:dc": "http://purl.org/dc/elements/1.1/", | |
"xmlns:content": "http://purl.org/rss/1.0/modules/content/", | |
"xmlns:atom": "http://www.w3.org/2005/Atom", | |
"xmlns:media": "http://search.yahoo.com/mrss/" | |
) do | |
xml.element("channel") do | |
xml.element("title") { xml.cdata title } | |
xml.element("description") { xml.cdata description } | |
xml.element("link") { xml.text base_url } | |
xml.element("generator") { xml.text "Solo" } | |
xml.element("lastBuildDate") { xml.text Time.utc_now.to_s } | |
xml.element("atom:link") { | |
xml.attribute "href", "#{base_url}#{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 | |
xml.element("title") { xml.cdata article.title } | |
if article.meta_description | |
xml.element("description") { xml.cdata article.meta_description.not_nil! } | |
end | |
xml.element("link") { xml.text "#{base_url}/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" | |
end | |
end | |
if article.content | |
content = Markdown.to_html(article.content.not_nil!) | |
xml.element("content:encoded") { xml.cdata content } | |
end | |
end | |
end | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment