Feeds Plugin
Author: Marc Palmer (marc at anyware.co.uk)
A plugin that renders RSS/Atom feeds, or any other formats supported by the ROME API.
It works like this - you call render() method from your controller action as normal, but instead
of using a view you specify a feedType parameter and a feedVersion parameter, as well as a closure
that uses a custom feed builder to define the feed. Currently feed types "rss" and "atom" are supported.
|
If you are using Safari (certainly version 3) you may find that links to your feeds are rejected with a message about it not being able to get feeds from localhost. The solution is to change your link in your address bar to your local host name i.e. "http://yourmacbook.local:8080/yourfeedtest/feed" and from then on the links will work. |
Using the dynamic render method and the Feed Builder DSL
class YourController {
def feed = {
render(feedType:"rss", feedVersion:"2.0") {
title = "My test feed"
link = "http://your.test.server/yourController/feed"
description = "The funky Grails news feed"
Article.list().each() { article ->
entry(article.title) {
link = "http://your.test.server/article/${article.id}"
article.content // return the content
}
}
}
}
}
The feedType parameter is required. The feedVersion is optional and will default to 2.0 for RSS and 1.0 for Atom if not supplied. Current tested feedType values are "rss" and "atom", but any ROME supported feed type should work.
|
The different feed types have different requirements. For example RSS 2.0 requires a "description" property in the channel (root node of the builder) whereas older versions may not. So watch out for errors relating to properties being required in certain nodes. |
Builder semantics
Some of the feed formats have different required properties that you must set on the feed (AKA Channel)
and the child nodes (entry and content). Exceptions will occur if you don't meet these constraints.
The feed builder is very forgiving. The general pattern is:
- set feed top-level properties i.e. link and title
- define entry nodes and properties of them
- define content for entry nodes, and properties of content
There are some smarts, namely:
- entry nodes can take a title parameter as a shortcut.
- content nodes can take a string parameter which is used as the text/plain content body
- entry node bodies can just return an object, the string value of which will be used as text/plain content for the entry
- entry and content nodes can take a map as parameter, to set any properties of the node
Common properties of entry nodes you may want to set:
| publishedDate | the date the entry was published |
| categories | the list of categories |
| author | author name |
| link | link to the online entry |
Common properties of content nodes you may want to set:
| type | the mime type of the content |
Enclosures and descriptions are currently not directly supported by the builder but can be constructed using the ROME API directly.
Examples
Setting root node (feed) properties directly using FeedBuilder.
|
This section does not apply if using the render(feedType:"xxxx") mechanism, only if using the FeedBuilder directly, in the render(feedType:"xxx") case you can only set feed properties in the top level of the closure |
def builder = new FeedBuilder() builder.feed { title = "My title" link = "http://www.myblogsite.com" } def feedA = builder.makeFeed('rss_2.0') def builder2 = new FeedBuilder() builder2.feed(title: "My title", link: "http://www.myblogsite.com") { // nodes here } def feedB = builder2.makeFeed('rss_2.0') def builder3 = new FeedBuilder() builder3.feed("My title") { link = "http://www.myblogsite.com" } def feedC = builder2.makeFeed('rss_2.0')
Entry nodes
entry {
title = "Article 1"
link = "http://somedomain.com/feed/1"
publishDate = new Date()
//content here
}
entry("Article 2") {
link = "http://somedomain.com/feed/2"
publishDate = new Date()
//content here
}
entry(title:"Title here", link:"http://somedomain.com/feed") {
publishDate = new Date()
//content here
}
Using the taglib
There is a "<feed:meta>" tag provided to generate the <link> tag correctly for use within the <head> section of your content. It sets the mime type and title of the link correctly.
<feed:meta kind="rss" version="2.0" controller="feed" action="index"/> <feed:meta kind="atom" version="1.0" controller="feed" action="index"/>

