RSS

The final topic that we'll examine is RSS feeds. An RSS feed is an XML format designed to let users subscribe to a website and receive notification whenever a new article (or other piece of content) is posted. It works best for news sites or blogs.

RSS is beginning to be phased out in favor of Atom that serves the same purpose but is an actual IETF standard. Users use RSS and Atom interchangeably and most RSS readers support Atom feeds. We'll be using the XML::Feed CPAN module to generate the RSS feed, which can generate Atom and legacy RSS from the same code.

To demonstrate the concept of RSS, we'll add an RSS feed to our mini-blog program from Chapter 6, Building Your Own Model. To start, let's create a Controller to generate RSS feeds. As the RSS feed is specific to one piece of data and no templating system is used to generate the feed, we're going to do everything inside a Controller for demonstration. However, when building real application, always use a View.

Here's the source of the Feeds Controller:

(in Blog/lib/Blog/Controller/Feeds.pm):
package Blog::Controller::Feeds;
use strict;
use warnings;
use base 'Catalyst::Controller';
use XML::Feed;
use DateTime;
sub atom : Local {
my ($self, $c) = @_;
$c->stash->{type} = 'Atom';
}
sub rss : Local {
my ($self, $c) = @_;
$c->stash->{type} = 'RSS';
}
sub end : Private {
my ($self, $c) = @_;
my @posts = $c->model('Filesystem')->get_recent_posts;
my $feed = XML::Feed->new($c->stash->{type});
$feed->title("Test Blog Feed");
$feed->link($c->uri_for('/'));
$feed->description("Test feed for my mini-blog");
$feed->author("Your Name");
$feed->language('en-US'),
foreach my $post (@posts) {
my $entry = XML::Feed::Entry->new($c->stash->{type});
$entry->title($post->title);
$entry->content($post->body);
$entry->issued(DateTime->from_epoch(epoch => $post->created));
$entry->modified(DateTime->from_epoch(epoch =>
$post->modified));
# entry unique ID
$entry->link($c->req->base.$post->created.$post->title);
$feed->add_entry($entry);
}
$c->res->content_type('application/xml'),
$c->res->body($feed->as_xml);
}
1;

Creating an RSS feed is very simple. The first two actions let the user select what type of feed he wants based on the URL. Regardless of choice, we forward to end action where we actually render the feed.

Here, we create an XML::Feed object and then set as many attributes as possible (the manual for XML::Feed shows the whole list of possible attributes). Then, for each blog post, we create an XML::Feed::Entry, populate it, and add it to the feed. At the very end, we send the generated XML to the user.

When a user subscribes to the feed, his client will request the feed every few hours. When a new article is added (as specified by "link", the article's unique ID), he'll be alerted that the site has changed. Most users prefer that the full text of the updated article be in the "content" field in the RSS entry (so that they can view the article right inside the RSS reader), but it's acceptable to only include the first paragraph so that the user will visit your site for the full story.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.129.194