App-ZofCMS

 view release on metacpan or  search on metacpan

lib/App/ZofCMS/Plugin/BasicLWP.pm  view on Meta::CPAN


use LWP::UserAgent;
use base 'App::ZofCMS::Plugin::Base';

sub _key { 'plug_basic_lwp' }

sub _defaults {
    return (
        t_name  => 'plug_basic_lwp',
        t_key   => 'd',
        decoded => 0,
        uri_fix => 0,
        ua_args => [
            agent   => 'Opera 9.2',
            timeout => 30,
        ],
        # uri     => 'http://google.com/',
    );
}

sub _do {

lib/App/ZofCMS/Plugin/BasicLWP.pm  view on Meta::CPAN


    my ( $t_name, $t_key ) = @$conf{ qw/t_name t_key/ };

    my $ua = LWP::UserAgent->new( @{ $conf->{ua_args} || [] } );
    my $response = $ua->get( $conf->{uri} );
    unless ( $response->is_success ) {
        $template->{ $t_key }{ $t_name . '_error' } = $response->status_line;
        return;
    }

    $template->{ $t_key }{ $t_name } = $conf->{decoded}
                                     ? $response->decoded_content
                                     : $response->content;

    return 1;
}

1;
__END__

=encoding utf8

lib/App/ZofCMS/Plugin/BasicLWP.pm  view on Meta::CPAN


You need to add the plugin to the list of plugins to execute. Since you are likely to work
on the fetched data, make sure to set correct priorities.

=head2 C<plug_basic_lwp>

    plug_basic_lwp => {
        uri     => 'http://zofdesign.com/', # everything but 'uri' is optional
        t_name  => 'plug_basic_lwp',
        t_key   => 'd',
        decoded => 0,
        fix_uri => 0,
        ua_args => [
            agent   => 'Opera 9.2',
            timeout => 30,
        ],
    }

The plugin won't run unless C<plug_basic_lwp> first-level key is present either in Main
Config File or ZofCMS Template. Takes a hashref or a subref as a value. If subref is
specified,

lib/App/ZofCMS/Plugin/BasicLWP.pm  view on Meta::CPAN

See SYNOPSIS for examples. B<Defaults to:> C<plug_basic_lwp> (and
the errors will be in C<plug_basic_lwp_error>

=head3 C<t_key>

    t_key => 'd',

B<Optional>. Takes a string as a value. Specifies the name of B<first-level> key in ZofCMS
Template hashref in which to create the C<t_name> key (see above). B<Defaults to:> C<d>

=head3 C<decoded>

    decoded => 0,

B<Optional>. Takes either true or false values as a value. When set to a I<true> value,
the content will be given us with C<decoded_content()>. When set to a I<false> value, the
content will be given us with C<content()> method. See L<HTTP::Response> for description
of those two methods. B<Defaults to:> C<0> (use C<content()>)

=head3 C<fix_uri>

    fix_uri => 0,

B<Optional>. Takes either true or false values as a value. When set to a true value, the
plugin will try to "fix" URIs that would cause LWP to crap out with "URI must be absolute"
errors. When set to a false value, will attempt to fetch the URI as it is. B<Defaults to:>



( run in 0.326 second using v1.01-cache-2.11-cpan-26ccb49234f )