Catalyst::View::XML::Feed - Catalyst view for RSS, Atom, or other XML feeds
Create your view, e.g. lib/MyApp/View/Feed.pm
package MyApp::View::Feed; use base qw( Catalyst::View::XML::Feed ); 1;
In a controller, set the feed stash variable and forward to your view:
feed
sub rss : Local { my ($self, $c) = @_: $c->stash->{feed} = $feed_obj_or_data; $c->forward('View::Feed'); }
Catalyst::View::XML::Feed is a hassle-free way to serve an RSS, Atom, or other XML feed from your Catalyst application.
Your controller should put feed data into $c->stash->{feed}.
$c->stash->{feed}
The value in $c->stash->{feed} can be an object from any of the popular RSS or Atom classes, a plain Perl data structure, arbitrary custom objects, or an xml string.
$c->stash->{feed} = { format => 'RSS 1.0', id => $c->req->base, title => 'My Great Site', description => 'Kitten pictures for the masses', link => $c->req->base, modified => DateTime->now, entries => [ { id => $c->uri_for('rss', 'kitten_post')->as_string, link => $c->uri_for('rss', 'kitten_post')->as_string, title => 'First post!', modified => DateTime->now, content => 'This is my first post!', }, # ... more entries. ], };
The feed hash can take any of the following keys. They are identical to those supported by XML::Feed. See XML::Feed for more details.
Note: Depending on the feed format you choose, different subsets of attributes might be required. As such, it is recommended that you run the generated XML through a validator such as http://validator.w3.org/feed/ to ensure you included all necessary information.
Can be any of: "Atom", "RSS 0.91", "RSS 1.0", "RSS 2.0"
This should be a DateTime object.
An array ref of entries.
The entries array contains any number of hashrefs, each representing an entry in the feed. Each can contain any of the following keys. They are identical to those of XML::Feed::Entry. See XML::Feed::Entry for details.
entries
If you have custom objects that you would like to turn into feed entries, this can be done similar to plain Perl data structures.
For example, if we have a DB::BlogPost DBIx::Class model, we can do the following:
DB::BlogPost
$c->stash->{feed} = { format => 'Atom', id => $c->req->base, title => 'My Great Site', description => 'Kitten pictures for the masses', link => $c->req->base, modified => DateTime->now, entries => [ $c->model('DB::BlogPost')->all() ], };
The view will go through the keys for entries fields and, if possible, call a method of the same name on your entry object (e.g. $your_entry->title(); $your_entry->modified();) to get that value for the XML.
$your_entry->title(); $your_entry->modified();
Any missing fields are simply skipped.
If your class's method names do not match up to the entries keys, you can simply alias them by wrapping with another method. For example, if your DB::BlogPost has a post_title field which should be the title for the feed entry, you can add this to BlogPost.pm:
post_title
sub title { $_[0]->post_title }
An XML::Feed object.
$c->stash->{feed} = $xml_feed_obj;
An XML::RSS object.
$c->stash->{feed} = $xml_rss_obj;
An XML::Atom::SimpleFeed object.
$c->stash->{feed} = $xml_atom_simplefeed_obj;
An XML::Atom::Feed object.
$c->stash->{feed} = $xml_atom_feed_obj;
An XML::Atom::Syndication::Feed object.
$c->stash->{feed} = $xml_atom_syndication_feed_obj;
If none of the formats mentioned above are suitable, you may also provide a string containing the XML data.
$c->stash->{feed} = $xml_string;
http://github.com/mstratman/Catalyst-View-XML-Feed
Mark A. Stratman <stratman@gmail.com>
Thomas Doran (t0m)
Copyright 2011 the above author(s).
This sofware is free software, and is licensed under the same terms as perl itself.
To install Catalyst::View::XML::Feed, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Catalyst::View::XML::Feed
CPAN shell
perl -MCPAN -e shell install Catalyst::View::XML::Feed
For more information on module installation, please visit the detailed CPAN module installation guide.