| 1 | package Catalyst::Plugin::UserAgent; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 2 | our $VERSION = '0.01'; | |||||||||
| 7 | |||||||||||
| 8 | 2 | ▶ | use base qw/ Class::Data::Inheritable /; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 2 | ▶ | use LWP::UserAgent; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | =head1 NAME | ||||||||||
| 12 | |||||||||||
| 13 | Catalyst::Plugin::UserAgent - Add a singleton LWP::UserAgent to the context | ||||||||||
| 14 | |||||||||||
| 15 | =head1 SYNOPSIS | ||||||||||
| 16 | |||||||||||
| 17 | use Catalyst qw/ UserAgent /; | ||||||||||
| 18 | |||||||||||
| 19 | __PACKAGE__->config( | ||||||||||
| 20 | name => 'MyApp', | ||||||||||
| 21 | lwp_user_agent => { | ||||||||||
| 22 | agent => 'MyApp/1.0', | ||||||||||
| 23 | cookie_jar => {}, | ||||||||||
| 24 | }, | ||||||||||
| 25 | ); | ||||||||||
| 26 | |||||||||||
| 27 | sub foo : Local { | ||||||||||
| 28 | my ($self, $c) = @_; | ||||||||||
| 29 | |||||||||||
| 30 | my $content = $c->user_agent->get('http://example.com/')->content; | ||||||||||
| 31 | |||||||||||
| 32 | $c->stash->{template} = 'show_example.com.tt2'; | ||||||||||
| 33 | $c->stash->{content} = $content; | ||||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | =head1 DESCRIPTION | ||||||||||
| 37 | |||||||||||
| 38 | Just creates a single L<LWP::UserAgent> available to the context. This object is created on-demand. | ||||||||||
| 39 | |||||||||||
| 40 | If you wish to pass any startup options to the constructor. Place those into the configuration under the key: "lwp_user_agent": | ||||||||||
| 41 | |||||||||||
| 42 | __PACKAGE__->config( | ||||||||||
| 43 | name => 'MyApp', | ||||||||||
| 44 | lwp_user_agent => { | ||||||||||
| 45 | agent => 'MyApp/1.0', | ||||||||||
| 46 | cookie_jar => {}, | ||||||||||
| 47 | }, | ||||||||||
| 48 | ); | ||||||||||
| 49 | |||||||||||
| 50 | To get the user agent instance, use the C<user_agent> method of the Catalyst context: | ||||||||||
| 51 | |||||||||||
| 52 | my $ua = $c->user_agent; | ||||||||||
| 53 | |||||||||||
| 54 | =cut | ||||||||||
| 55 | |||||||||||
| 56 | 2 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 57 | 2 | __PACKAGE__->mk_classdata(qw/ _user_agent /); | |||||||||
| 58 | } | ||||||||||
| 59 | |||||||||||
| 60 | 1 | ▶ | sub user_agent { | ||||||||
|
Subroutine
user_agent: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||
| 61 | 1 | my $c = shift; | |||||||||
| 62 | |||||||||||
| 63 | 1 | ▶ | if (!defined $c->_user_agent) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | 1 | my %options; | |||||||||
| 65 | 1 | ▶ | if (defined $c->config->{lwp_user_agent}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | 1 | %options = %{ $c->config->{lwp_user_agent} }; | |||||||||
| 67 | } | ||||||||||
| 68 | |||||||||||
| 69 | 1 | $c->_user_agent(LWP::UserAgent->new(%options)); | |||||||||
| 70 | } | ||||||||||
| 71 | |||||||||||
| 72 | 1 | return $c->_user_agent; | |||||||||
| 73 | } | ||||||||||
| 74 | |||||||||||
| 75 | =head1 SEE ALSO | ||||||||||
| 76 | |||||||||||
| 77 | L<LWP::UserAgent> | ||||||||||
| 78 | |||||||||||
| 79 | =head1 AUTHOR | ||||||||||
| 80 | |||||||||||
| 81 | Andrew Sterling Hanenkamp, E<lt>hanenkamp@cpan.orgE<gt> | ||||||||||
| 82 | |||||||||||
| 83 | =head1 LICENSE AND COPYRIGHT | ||||||||||
| 84 | |||||||||||
| 85 | Copyright 2006 Andrew Sterling Hanenkamp E<lt>hanenkamp@cpan.orgE<gt>. All | ||||||||||
| 86 | Rights Reserved. | ||||||||||
| 87 | |||||||||||
| 88 | This module is free software; you can redistribute it and/or modify it under | ||||||||||
| 89 | the same terms as Perl itself. See L<perlartistic>. | ||||||||||
| 90 | |||||||||||
| 91 | This program is distributed in the hope that it will be useful, but WITHOUT | ||||||||||
| 92 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||||||||||
| 93 | FOR A PARTICULAR PURPOSE. | ||||||||||
| 94 | |||||||||||
| 95 | =cut | ||||||||||
| 96 | |||||||||||
| 97 | 2 | 1 | |||||||||