Catalyst::Plugin::UserAgent - Add a singleton LWP::UserAgent to the context


Catalyst-Plugin-UserAgent documentation Contained in the Catalyst-Plugin-UserAgent distribution.

Index


Code Index:

NAME

Top

Catalyst::Plugin::UserAgent - Add a singleton LWP::UserAgent to the context

SYNOPSIS

Top

  use Catalyst qw/ UserAgent /;

  __PACKAGE__->config(
      name => 'MyApp',
      lwp_user_agent => {
          agent      => 'MyApp/1.0',
          cookie_jar => {},
      },
  );

  sub foo : Local {
      my ($self, $c) = @_;

      my $content = $c->user_agent->get('http://example.com/')->content;

      $c->stash->{template} = 'show_example.com.tt2';
      $c->stash->{content} = $content;
  }

DESCRIPTION

Top

Just creates a single LWP::UserAgent available to the context. This object is created on-demand.

If you wish to pass any startup options to the constructor. Place those into the configuration under the key: "lwp_user_agent":

  __PACKAGE__->config(
      name => 'MyApp',
      lwp_user_agent => {
          agent      => 'MyApp/1.0',
          cookie_jar => {},
      },
  );

To get the user agent instance, use the user_agent method of the Catalyst context:

  my $ua = $c->user_agent;

SEE ALSO

Top

LWP::UserAgent

AUTHOR

Top

Andrew Sterling Hanenkamp, <hanenkamp@cpan.org>

LICENSE AND COPYRIGHT

Top


Catalyst-Plugin-UserAgent documentation Contained in the Catalyst-Plugin-UserAgent distribution.
package Catalyst::Plugin::UserAgent;

use strict;
use warnings;

our $VERSION = '0.01';

use base qw/ Class::Data::Inheritable /;
use LWP::UserAgent;

BEGIN {
    __PACKAGE__->mk_classdata(qw/ _user_agent /);
}

sub user_agent {
    my $c = shift;

    if (!defined $c->_user_agent) {
        my %options;
        if (defined $c->config->{lwp_user_agent}) {
            %options = %{ $c->config->{lwp_user_agent} };
        }

        $c->_user_agent(LWP::UserAgent->new(%options));
    }

    return $c->_user_agent;
}

1