SoggyOnion::Resource - determines which SoggyOnion::Plugin to use


SoggyOnion documentation Contained in the SoggyOnion distribution.

Index


Code Index:

NAME

Top

SoggyOnion::Resource - determines which SoggyOnion::Plugin to use

SYNOPSIS

Top

    my $item = {
        rss          => 'http://use.perl.org/useperl.rss',
        id           => 'use_perl',
        limit        => 10,
        descriptions => 'no',
        };
    my $resource = SoggyOnion::Resource->new($item);
    print $resource->content;

DESCRIPTION

Top

To override how SoggyOnion determines what plugin to use based on what keys are in the item in the configuration, copy this module and add a resourceclass option in your config.yaml, such as:

    ...
    templatedir:   '/path/to/templates'
    cachedir:      '/tmp/soggycache'
    resourceclass: 'My::Custom::ResourceHandler'
    ...

SEE ALSO

Top

SoggyOnion::Plugin, SoggyOnion

AUTHOR

Top

Ian Langworth, <ian@cpan.org>

COPYRIGHT AND LICENSE

Top


SoggyOnion documentation Contained in the SoggyOnion distribution.

package SoggyOnion::Resource;
use warnings;
use strict;

our $VERSION = '0.04';

# make sure that plugins i expect to use are loaded
require SoggyOnion::Plugin::RSS;
require SoggyOnion::Plugin::ImageScraper;

sub new {
    my ( $self, $item ) = @_;

    # do some error checking
    unless ( ref $item eq 'HASH' ) {
        warn "\t\titems must be a hash (got '$item')\n";
        return;
    }
    if ( not( defined $item->{id} ) || $item->{id} =~ /\W/ ) {
        warn "\t\titems must have an alphanumeric-only 'id' key\n";
        return;
    }

    # if we have an 'rss' key, assume it's an rss feed
    if ( exists $item->{rss} ) {
        return SoggyOnion::Plugin::RSS->new($item);
    }

    # if we have an 'images' key, assume it's an image scraper
    if ( exists $item->{images} ) {
        return SoggyOnion::Plugin::ImageScraper->new($item);
    }

    # if we have a 'plugin' key, it's a module. try using it.
    elsif ( exists $item->{plugin} ) {

        # try requiring
        eval "require $item->{plugin}";
        if ($@) {
            warn "\t\t$@\n";
            return;
        }

        # try actually getting an object from it
        my $rval;
        eval { $rval = $item->{plugin}->new($item); };
        warn "\t\t$@\n" if $@;
        return $rval;
    }

    # augh, no handler at all!
    warn "\t\titem contained keys qw("
        . join( ' ', keys %$item )
        . ") but don't know how to handle it\n";
    return;
}

1;

__END__