Saturday, December 26, 2009

GroovyMag Plugin Corner: Feeds Plugin

The following post is a reprint of the Plugin Corner article for the January 2009 issue of GroovyMag. You can find this and other past issues at http://groovymag.com.


RSS and Atom feeds are becoming more popular and consequently more important to include in our applications. The battle for mindshare among our users or potential users is constant, and having a feed available is one of our more effective weapons.

But feeds are complicated, right? There are so many different formats and versions and then there's all that XML- don't get me started on XML! Not to worry; some of the smart people at Sun came up with the ROME API that encapsulates and simplifies much of what goes into an RSS/Atom feed. So, all we have to do is learn a new Java API. NOT! This is Grails we're talking about. In steps Groovy Award winner Marc Palmer with the Feeds Plugin.

This plugin wraps the ROME API and gives us the very groovy FeedBuilder with its powerful yet simple DSL. Once installed, the Feeds Plugin gives us a new version of the familiar render() method. Now we can just “render" a feed instead of a view. Let's get started.

For our example, we are going to build a book of the month feed. We already have a Book domain class with corresponding controller and views.

First install the plugin:

grails install-plugin feeds

That was pretty painless. Now we will add a new action to our controller:

def monthly = {
render(feedType:"atom"){
title="Book of the Month"
link="http://localhost:8080/bom/book/monthly/feed"
description="A monthly book recommendation"
def book = bookOfTheMonth() //complex algorithm here :)
if(book){
entry{
title = book.title
link="${g.createLink(action:''show'', id:book.id)}"
}
}
}
}

The render() method now takes a feedType parameter. It is this parameter that tells the plugin that this is indeed a feed. If that parameter is there, we can also include a version parameter to specify the version of the feed format we are using. The defaults are 2.0 for RSS and 1.0 for Atom. Next the render() method takes a closure where we can set the various properties of our feed and entries. In our example we are setting the title, link, and description properties on our feed and then including one entry for the book of the month. Since the FeedBuilder DSL is still Groovy code, we can handle any logic that is needed. Here, for example, we are only creating an entry if our book is not null.

Next we create our entry. The entry node also takes a closure where we will set the various properties of the entry. All we are doing with our entry is giving it a title (the book's title) and a link to the show page for that book. Now if we run our application and subscribe to the feed at http://localhost:8080/bom/book/monthly we will get
something like this:



I know- kind of boring. Let's add another feed to our application that will give us a bit more to look at. Some of us are more voracious readers than others so we might want to have a list of books all at once rather than one each month. Here's a new controller action that will give us just that:

def booklist={
render(feedType:"atom"){
title="Groovy Books"
link="http://localhost:8080/bom/book/booklist/feed"
description="A listing of Groovy and Grails books"
def books = Book.list()
books.each{book ->
entry(book.title){
link="${g.createLink(action:''show'', id:book.id)}"
}
}
}
}

Now if we subscribe to http://localhost:8080/bom/book/monthly we will get a bit more content from our feed:



There is a great deal more that can be done with the Feeds Plugin than what we demonstrated here, so when you're ready to dig deeper, the official documentation is at http://grails.org/plugin/feeds. You can also find lots of information on the ROME API at the java.net site: http://rome.dev.java.net. As always, another excellent source of information is the plugin source itself. The Feeds Plugin source can be found in plugins/feedsx.x/ Much of the meat of the plugin is in src/groovy/feedplugin/FeedBuilder.groovy.

Even without digging in to the docs or source, you can see how easy it is to add basic RSS/Atom feeds to your application with the Feeds plugin. Now you can set your mind to work on finding new and innovative ways to use them. Once again Grails and Grails plugins take away the drudgery and leave us with the fun!

No comments: