NAME

Mojolicious::Plugin::FeedReader - Mojolicious Plugin to fetch and parse RSS & Atom feeds

SYNOPSIS

    # Mojolicious
     $self->plugin('FeedReader');

     # Mojolicious::Lite
     plugin 'FeedReader';

    my ($info, $feed) = app->find_feeds(q{search.cpan.org});
    my $out = app->parse_rss($feed);
    say $_->{title} for (@{$out->{items}});

    # In a route handler:
    get '/' => sub {
      my $self = shift;
      $self->render_later;
      my $delay = Mojo::IOLoop->delay(
        sub {
          $self->find_feeds("search.cpan.org", shift->begin(0));
        },
        sub {
          my $feed = pop;
          $self->parse_rss($feed, shift->begin);
        },
        sub {
            my $data = pop;
            $self->render(items => $data->{items});
        });
      $delay->wait unless Mojo::IOLoop->is_running;
    } => 'uploads';



    app->start;

    __DATA__

    @@ uploads.html.ep
    <ul>
    % for my $item (@$items) {
      <li><%= link_to $item->{title} => $item->{link} %> - <%= $item->{description} %></li>
    % }
    </ul>

DESCRIPTION

Mojolicious::Plugin::FeedReader implements minimalistic helpers for identifying, fetching and parsing RSS and Atom Feeds. It has minimal dependencies, relying as much as possible on Mojolicious components - Mojo::UserAgent for fetching feeds and checking URLs, Mojo::DOM for XML/HTML parsing. It is therefore rather fragile and naive, and should be considered Experimental/Toy code - use at your own risk.

METHODS

Mojolicious::Plugin::FeedReader inherits all methods from Mojolicious::Plugin and implements the following new ones.

register

$plugin->register(Mojolicious->new);

Register plugin in Mojolicious application. This method will install the helpers listed below in your Mojolicious application.

HELPERS

Mojolicious::Plugin::FeedReader implements the following helpers.

find_feeds

# Call blocking
my ($info, @feeds) = app->find_feeds('search.cpan.org');
# $info is a hash ref
# @feeds is a list of Mojo::URL objects

# Call non-blocking
$self->find_feeds('http://example.com', sub {
  my ($info, @feeds) = @_;
  unless (@feeds) {
    $self->render_exception("no feeds found, " . $info->{error});
  }
  else {
    ....
  }
});

A Mojolicious port of Feed::Find by Benjamin Trott. This helper implements feed auto-discovery for finding syndication feeds, given a URI. If given a callback function as an additional argument, execution will be non-blocking.

parse_rss

# parse an RSS feed
# blocking
my $url = Mojo::URL->new('http://rss.slashdot.org/Slashdot/slashdot');
my $feed = $self->parse_rss($url);
for my $item (@{$feed->{items}}) {
  say $_ for ($item->{title}, $item->{description}, 'Tags: ' . join q{,}, @{$item->{tags}});
}

# non-blocking
$self->parse_rss($url, sub {
  my ($c, $feed) = @_;
  $c->render(text => "Feed tagline: " . $feed->{tagline});
});

# parse a file
$feed2 = $self->parse_rss('/downloads/foo.rss');

# parse response DOM
$self->ua->get($feed_url, sub {
  my ($ua, $tx) = @_;
  my $feed = $self->parse_rss($tx->res->dom);
});

A minimalist liberal RSS/Atom parser, using Mojo::DOM queries.

Dates are parsed using HTTP::Date.

If parsing fails (for example, the parser was given an HTML page), the helper will return undef.

On success, the result returned is a hashref with the following keys:

Each item in the items array is a hashref with the following keys:

SEE ALSO

Mojolicious, Mojolicious::Guides, http://mojolicio.us.