<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;"># NAME

Mojolicious::Plugin::FeedReader - Mojolicious plugin to find and parse RSS &amp; Atom feeds

# SYNOPSIS

        # Mojolicious
         $self-&gt;plugin('FeedReader');

         # Mojolicious::Lite
         plugin 'FeedReader';

        # Blocking:
        get '/b' =&gt; sub {
          my $self = shift;
          my ($feed) = $self-&gt;find_feeds(q{search.cpan.org});
          my $out = $self-&gt;parse_feed($feed);
          $self-&gt;render(template =&gt; 'uploads', items =&gt; $out-&gt;{items});
        };

        # Non-blocking:
        get '/nb' =&gt; sub {
          my $self = shift;
          $self-&gt;render_later;
          my $delay = Mojo::IOLoop-&gt;delay(
            sub {
              $self-&gt;find_feeds("search.cpan.org", shift-&gt;begin(0));
            },
            sub {
              my $feed = pop;
              $self-&gt;parse_feed($feed, shift-&gt;begin);
            },
            sub {
                my $data = pop;
                $self-&gt;render(template =&gt; 'uploads', items =&gt; $data-&gt;{items});
            });
          $delay-&gt;wait unless Mojo::IOLoop-&gt;is_running;
        };

        app-&gt;start;

        __DATA__

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

# 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](https://metacpan.org/pod/Mojolicious::Plugin::FeedReader) inherits all methods from
[Mojolicious::Plugin](https://metacpan.org/pod/Mojolicious::Plugin) and implements the following new ones.

## register

    $plugin-&gt;register(Mojolicious-&gt;new);

Register plugin in [Mojolicious](https://metacpan.org/pod/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 (@feeds) = app-&gt;find_feeds('search.cpan.org');
    # @feeds is a list of Mojo::URL objects

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

A Mojolicious port of [Feed::Find](https://metacpan.org/pod/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\_feed

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

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

    # parse a file
    $feed2 = $self-&gt;parse_feed('/downloads/foo.rss');

    # parse response
    $self-&gt;ua-&gt;get($feed_url, sub {
      my ($ua, $tx) = @_;
      my $feed = $self-&gt;parse_feed($tx-&gt;res);
    });

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

Dates are parsed using [HTTP::Date](https://metacpan.org/pod/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:

- title
- description (may be filled from subtitle or tagline if absent)
- htmlUrl - web page URL associated with the feed
- items - array ref of feed news items
- subtitle (optional)
- tagline (optional)
- author (name of author field, or dc:creator or webMaster)
- published - time in epoch seconds (may be filled with pubDate, dc:date, created, issued, updated or modified)

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

- title
- link
- content (may be filled with content:encoded, xhtml:body or description fields)
- id (will be equal to link or guid if it is undefined and either of those fields exists)
- description (optional) - usually a shorter form of the content (may be filled with summary if description is missing)
- guid (optional)
- published - time in epoch seconds (may be filled with pubDate, dc:date, created, issued, updated or modified)
- author (may be filled from author or dc:creator)
- tags (optional) - array ref of tags, categories or dc:subjects.
- \_raw - XML serialized text of the item's Mojo::DOM node. Note that this can be different from the original XML text in the feed.

# CREDITS

Some tests adapted from [Feed::Find](https://metacpan.org/pod/Feed::Find) and [XML::Feed](https://metacpan.org/pod/XML::Feed). Feed autodiscovery adapted from l&lt;Feed::Find&gt;.

Test data (web pages, feeds and excerpts) included in this package is intended for testing purposes only, and is not meant in anyway
to infringe on the rights of the respective authors.

# COPYRIGHT AND LICENSE

Copyright (C) 2014, Dotan Dimet.

This program is free software, you can redistribute it and/or modify it
under the terms of the Artistic License version 2.0.

# SEE ALSO

[Mojolicious](https://metacpan.org/pod/Mojolicious), [Mojolicious::Guides](https://metacpan.org/pod/Mojolicious::Guides), [http://mojolicio.us](http://mojolicio.us)

[XML::Feed](https://metacpan.org/pod/XML::Feed), [Feed::Find](https://metacpan.org/pod/Feed::Find), [HTTP::Date](https://metacpan.org/pod/HTTP::Date)
</pre></body></html>