Catalyst-Model-XML-Feed

 view release on metacpan or  search on metacpan

lib/Catalyst/Model/XML/Feed.pm  view on Meta::CPAN

=item feeds

An arrayref of hashes containing feeds to preload.  The hash is
required to contain a key called "uri" or "location", specifing the
URL of the feed to load.  It may optinally contain "name" or "title",
if you wish to override the feed's own title.

=back

Example config in MyApp.yml (assuming you call your feed model
C<Feeds>):

     Model::Feeds:
       feeds:
         - uri: http://blog.jrock.us/
         - title: delicious
           location: http://del.icio.us/rss/
       ttl: 1337

=head1 METHODS

=head2 new

Creates a new instance.  Called for you by Catalyst.  If your config
file contains invalid feeds the feed will be refetched when the feed
content is accessed. This allows your Catalyst app to start even in
the case of an external outage of an RSS feed.

=cut

sub new {
    my $self = shift;
    $self = $self->next::method(@_);
    my @in_feeds = eval { @{$self->feeds} };
    $self->feeds({});

    $self->ttl($self->ttl || 3600);
    foreach my $feed (@in_feeds) {
        my $name = $feed->{name} || $feed->{title};
        my $uri  = $feed->{uri}  || $feed->{location};
        #my $c = $_[0];
        if($name){
            #$c->log->debug("registering XML feed $uri as $name") if $c;
            $self->register($name, $uri);
        }
        else {
            #$c->log->debug("registering XML feed $uri") if $c;
            my @names = $self->register($uri);
            my $name = join q{,},@names;
            #$c->log->debug("feed(s) at $uri created as $name") if $c;
        }
    }

    return $self;
}

=head2 register($uri_of_feed)

Registers a feed with the Model.  If C<$uri_of_feed> points to a feed,
the feed is added under its own name.  If $C<$uri_of_feed> points to
an HTML or XHTML document containing C<< <link> >> tags pointing to
feeds, all feeds are added by using their URIs as their names.

Returns a list of the names of the feeds that were added.

Warns if the C<$uri_of_feeds> doesn't contain a feed
or links to feeds, or it cannot be fetched.

=head2 register($name, $uri_of_feed)

Registers a feed with the Model.  If C<$name> is already registered,
the old feed at C<$name> is forgotten and replaced with the new feed
at C<$uri_of_feed>.  The C<title> of the feed is replaced with
C<$name>.

Warns if C<$uri_of_feed> isn't an XML feed (or doesn't
contain a C<link> to one).  

Throws an exception if the C<$uri_of_feed> links to multiple feeds.

=cut

sub register {
    my $self = shift;
    my ($arg1, $arg2) = @_;

    my $name;
    my $uri;

    if($arg2){
        # get only one feed
        $name = $arg1;
        $uri  = URI->new($arg2);
        my $feed;
        eval {
            $feed = XML::Feed->parse($uri)
                or die XML::Feed->errstr;
        };
        if($@){
            my @feeds = XML::Feed->find_feeds($arg2);
            if(@feeds > 1){
                croak "$arg2 points to too many feeds";
            }
            if(!@feeds){
                carp "$arg2 does not reference any feeds";
		# register $uri as it is, but without the feed, in hope that it comes online later.
            } else {
		$uri = shift @feeds;
	    }
        }

        return $self->_add_uri($uri, $name);
    }
    else {
        $uri = URI->new($arg1);
        my @feed_uris = XML::Feed->find_feeds($uri);
        croak "$arg1 does not reference any feeds" if !@feed_uris;

        my @added;
        foreach my $uri (@feed_uris){
            $uri = URI->new($uri);



( run in 1.896 second using v1.01-cache-2.11-cpan-39bf76dae61 )