File Coverage

blib/lib/Egg/Plugin/LWP.pm
Criterion Covered Total %
statement 21 42 50.0
branch 0 8 0.0
condition 0 15 0.0
subroutine 7 13 53.8
pod 1 1 100.0
total 29 79 36.7


line stmt bran cond sub pod time code
1             package Egg::Plugin::LWP;
2             #
3             # Masatoshi Mizuno E<lt>lusheE<64>cpan.orgE<gt>
4             #
5             # $Id: LWP.pm 319 2008-04-17 12:20:20Z lushe $
6             #
7 2     2   597 use strict;
  2         4  
  2         79  
8 2     2   10 use warnings;
  2         3  
  2         378  
9              
10             our $VERSION = '3.01';
11              
12             sub _setup {
13 0     0     my($e)= @_;
14 0   0       my $conf= $e->config->{plugin_lwp} ||= {};
15 0   0       $conf->{timeout} ||= 10;
16 0   0       $conf->{agent} ||= __PACKAGE__. " v$VERSION";
17 0           $e->next::method;
18             }
19 0     0 1   sub ua { Egg::Plugin::LWP::handler->new(@_) }
20              
21             package Egg::Plugin::LWP::handler;
22 2     2   19 use strict;
  2         4  
  2         61  
23 2     2   10 use warnings;
  2         3  
  2         62  
24 2     2   3459 use LWP::UserAgent;
  2         148068  
  2         80  
25 2     2   3625 use HTTP::Request::Common qw/ GET POST /;
  2         5496  
  2         188  
26              
27             {
28 2     2   15 no strict 'refs'; ## no critic
  2         5  
  2         787  
29             sub request {
30 0     0     my($self, $method, $url, $args)= _get_args(@_);
31 0           $self->{ua}->request
32 0 0         ( &{$method}($url, ($method=~m{POST} ? [%$args]: %$args) ) );
33             }
34             sub simple_request {
35 0     0     my($self, $method, $url, $args)= _get_args(@_);
36 0           $self->{ua}->simple_request
37 0 0         ( &{$method}($url, ($method=~m{POST} ? [%$args]: %$args) ) );
38             }
39             };
40              
41             sub new {
42 0     0     my($class, $e)= splice @_, 0, 2;
43 0           my $ua= LWP::UserAgent->new(
44 0 0 0       %{$e->config->{plugin_lwp}},
45 0           %{$_[1] ? {@_}: ($_[0] || {})},
46             );
47 0           bless { e=> $e, ua=> $ua }, $class;
48             }
49             sub _get_args {
50 0     0     my $self= shift;
51 0   0       my $meth= uc(shift) || 'GET';
52 0   0       my $url = shift || die qq{ I want 'url' };
53 0 0 0       ($self, $meth, $url, ($_[1] ? {@_}: ($_[0] || {})));
54             }
55              
56             1;
57              
58             __END__
59              
60             =head1 NAME
61              
62             Egg::Plugin::LWP - LWP for Egg Plugin.
63              
64             =head1 SYNOPSIS
65              
66             use Egg qw/ LWP /;
67            
68             __PACKAGE__->egg_startup(
69             ...
70             .....
71            
72             plugin_lwp => {
73             timeout => 10,
74             agent => 'MyApp Agent.',
75             },
76            
77             );
78              
79             # The GET request is sent.
80             my $res= $e->ua->request( GET => 'http://domain.name/hoge/' );
81            
82             # The POST request is sent.
83             my $res= $e->ua->request( POST => 'http://domain.name/hoge/form', {
84             param1 => 'hooo',
85             param2 => 'booo',
86             } );
87            
88             # It requests it GET doing to pass ua the option.
89             my $res= $e->ua( agent => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)' )
90             ->request( GET => 'http://domain.name/hoge/' );
91            
92             # It turns by using ua made once.
93             my $ua= $e->ua( timeout => 5 );
94             for my $domain (qw/ domain1 domain2 domain3 /) {
95             my $res= $ua->request( GET => "http://$domain/" ) || next;
96             $res->is_success || next;
97             $res->...
98             }
99              
100             =head1 DESCRIPTION
101              
102             It is a plugin to use L<LWP::UserAgent>.
103              
104             Please define HASH in 'plugin_lwp' about the setting.
105             All these set values are passed to L<LWP::UserAgent>.
106              
107             * Please refer to the document of L<LWP::UserAgent> for the option.
108              
109             =head1 METHODS
110              
111             =head2 ua ( [UA_OPTION_HASH] )
112              
113             The handler object of Egg::Plugin::LWP is returned.
114              
115             When UA_OPTION_HASH is given, everything is passed to L<LWP::UserAgent> as an
116             option.
117              
118             UA_OPTION_HASH overwrites a set value of default.
119              
120             =head1 HANDLER METHODS
121              
122             =head2 new
123              
124             It is a constructor who is called by $e-E<gt>ua.
125              
126             L<LWP::UserAgent> object is generated here.
127              
128             =head2 request ( [REQUEST_METHOD], [URL], [ARGS_HASH] )
129              
130             The request is sent based on generated ua.
131              
132             When an invalid value to REQUEST_METHOD is passed, it treats as GET request.
133              
134             URL is not omissible. The exception is generated when omitting it.
135              
136             ARGS_HASH is treated as an argument passed to L<HTTP::Request::Common>.
137              
138             L<HTTP::Response> object that ua returns after completing the request is returned.
139              
140             my $res= $e->ua->request(0, 'http://domain.name/');
141              
142             =head2 simple_request
143              
144             Simple_request of L<LWP::UserAgent> is done.
145              
146             The argument and others is similar to 'request' method.
147              
148             my $res= $e->ua->simple_request(0, 'http://domain.name/');
149              
150             =head1 SEE ALSO
151              
152             L<Egg::Release>,
153             L<LWP::UserAgent>,
154             L<HTTP::Request::Common>,
155              
156             =head1 AUTHOR
157              
158             Masatoshi Mizuno E<lt>lusheE<64>cpan.orgE<gt>
159              
160             =head1 COPYRIGHT AND LICENSE
161              
162             Copyright (C) 2008 Bee Flag, Corp. E<lt>L<http://egg.bomcity.com/>E<gt>, All Rights Reserved.
163              
164             This library is free software; you can redistribute it and/or modify
165             it under the same terms as Perl itself, either Perl version 5.8.6 or,
166             at your option, any later version of Perl 5 you may have available.
167              
168             =cut
169