Perlanet::Trait::OPML - generate an OPML file


Perlanet documentation Contained in the Perlanet distribution.

Index


Code Index:

NAME

Top

Perlanet::Trait::OPML - generate an OPML file

SYNOPSIS

Top

  my $perlanet = Perlanet->new_with_traits(
    traits => [ 'Perlanet::Trait::OPML' ]
  );

  $perlanet->run;

DESCRIPTION

Top

Generates an OPML file of all feeds that are being aggregated by the planet.

ATTRIBUTES

Top

opml_generator

An XML::OPML::SimpleGen object to generate the XML for the OPML file

opml_file

Where to save the OPML feed when it has been created

METHODS

Top

update_opml

Updates the OPML file of all contributers to this planet. If the opml attribute does not have a value, this method does nothing, otherwise it inserts each author into the OPML file and saves it to disk.

save_opml

Save the OPML file, by default to disk.

AUTHOR

Top

Dave Cross, <dave@mag-sol.com>

COPYRIGHT AND LICENSE

Top


Perlanet documentation Contained in the Perlanet distribution.
package Perlanet::Trait::OPML;
use Moose::Role;
use namespace::autoclean;

use Carp qw( carp );
use POSIX qw(setlocale LC_ALL);

has 'opml_generator' => (
  is         => 'rw',
  isa        => 'Maybe[XML::OPML::SimpleGen]',
  lazy_build => 1,
  predicate => 'has_opml'
);

sub _build_opml_generator {
  my $self = shift;

  eval { require XML::OPML::SimpleGen; };

  if ($@) {
    carp 'You need to install XML::OPML::SimpleGen to enable OPML ' .
          'support';
    $self->opml(undef);
    return;
  }

  my $loc = setlocale(LC_ALL, 'C');
  my $opml = XML::OPML::SimpleGen->new;
  setlocale(LC_ALL, $loc);
  $opml->head(
    title => $self->title,
  );

  return $opml;
}


has 'opml' => (
  isa       => 'Maybe[Str]',
  is        => 'rw',
);

sub update_opml {
  my ($self, @feeds) = @_;

  return unless $self->has_opml;

  foreach my $f (@feeds) {
    $self->opml_generator->insert_outline(
      title   => $f->title,
      text    => $f->title,
      xmlUrl  => $f->url,
      htmlUrl => $f->url,
    );
  }

  $self->save_opml;
}

sub save_opml {
  my $self = shift;
  $self->opml_generator->save($self->opml);
}

around 'fetch_feeds' => sub {
  my $orig = shift;
  my ($self, @feeds) = @_;
  @feeds = $self->$orig(@feeds);
  $self->update_opml(@feeds) if $self->has_opml;
  return @feeds;
};

1;