File Coverage

blib/lib/WWW/Yahoo/Groups/Mechanize.pm
Criterion Covered Total %
statement 42 51 82.3
branch 12 26 46.1
condition 1 3 33.3
subroutine 10 10 100.0
pod 4 4 100.0
total 69 94 73.4


line stmt bran cond sub pod time code
1             package WWW::Yahoo::Groups::Mechanize;
2             our $VERSION = '1.91';
3              
4             =head1 NAME
5              
6             WWW::Yahoo::Groups::Mechanize - Control WWW::Mechanize for WYG.
7              
8             =head1 DESCRIPTION
9              
10             This module is a subclass of L that permits us a bit
11             more control over some aspects of the fetching behaviour.
12              
13             =head1 INHERITANCE
14              
15             This module inherits from L, which inherits from
16             L. As such, any method available to either of them is
17             available here. Any overridden methods will be explained below.
18              
19             =cut
20              
21             our @ISA = qw( WWW::Mechanize );
22 12     12   35034 use WWW::Mechanize;
  12         2919031  
  12         549  
23 12     12   13004 use Net::SSL;
  12         452039  
  12         244  
24 12     12   12612 use Params::Validate qw( validate_pos SCALAR );
  12         28  
  12         1280  
25 12     12   70 use strict;
  12         21  
  12         473  
26 12     12   69 use warnings FATAL => 'all';
  12         24  
  12         16967  
27              
28             require WWW::Yahoo::Groups::Errors;
29             Params::Validate::validation_options(
30             WWW::Yahoo::Groups::Errors->import()
31             );
32              
33             =head1 CONSTRUCTOR
34              
35             =head2 new
36              
37             As for L but sets the agent string
38             to our custom agent.
39              
40             =cut
41              
42             sub new
43             {
44 12     12 1 37 my $class = shift;
45 12         201 my $self = $class->SUPER::new(@_);
46 12         132366 $self->agent("Mozilla/5.0 (LWP; $class)");
47 12         1406 return $self;
48             }
49              
50             =head1 METHODS
51              
52             =head2 debug
53              
54             Sets or gets whether we are in debugging mode. Returns true
55             if set, else false.
56              
57             warn "Awooga!" if $self->debug;
58             $self->debug( 1 );
59              
60             =cut
61              
62             sub debug
63             {
64 22     22 1 43 my $self = shift;
65 22 50       117 $self->{__PACKAGE__.'-debug'} = ($_[0] ? 1 : 0) if @_;
    100          
66 22         78 $self->{__PACKAGE__.'-debug'};
67             }
68              
69             =head2 get
70              
71             We override L<< get|WWW::Mechanize/"$a->get()" >> in order to
72             provide some behind the scenes actions.
73              
74             =over 4
75              
76             =item * Sleeping
77              
78             We allow you to rate limit your downloading. See L.
79              
80             =item * Automatic adult confirmation
81              
82             We automatically click Accept on adult confirmation. So I hope you agree
83             to all that.
84              
85             =item * Debugging
86              
87             If L is enabled, then it will display a warning showing the
88             URL.
89              
90             =back
91              
92             I should probably shift the advertisement interruption skipping
93             into this method at some point, along with the redirect handling.
94              
95             It will throw a C if
96             it is unable to retrieve the specified page.
97              
98             Returns 0 if success, else an exception object.
99              
100             my $rv = $y->get( 'http://groups.yahoo.com' );
101             $rv->rethrow if $rv;
102              
103             # or, more idiomatically
104             $rv = $y->get( 'http://groups.yahoo.com' ) and $rv->rethrow;
105              
106              
107             =cut
108              
109             sub get
110             {
111 10     10 1 21 my $self = shift;
112 10         273 my $url = $_[0];
113 10 50       45 warn "Fetching $url\n" if $self->debug;
114 10         23 my $rv;
115 10         25 $rv = eval {
116             # Fetch page
117 10         100 my $rv = $self->SUPER::get(@_);
118             # Throw if problem
119 10 50       10700172 X::WWW::Yahoo::Groups::BadFetch->throw(error =>
120             "Unable to fetch $url: ".
121             $self->res->code.' - '.$self->res->message)
122             if ($self->res->is_error);
123             # Sleep for a bit
124 0 0       0 if (my $s = $self->autosleep() )
125             {
126 0         0 sleep( $s );
127             }
128             # Return something
129 0         0 0;
130             };
131 10 50 33     9597 if ( $self->uri and $self->uri =~ m,/adultconf\?, )
132             {
133 0         0 my $form = $self->form_number( 0 );
134 0 0       0 if ($self->debug)
135             {
136 0         0 for my $form ( $self->forms )
137             {
138 0         0 warn $form->dump;
139             }
140             }
141 0 0       0 warn "Clicking accept for adultconf\n" if $self->debug;
142 0         0 $self->click( 'accept' );
143             }
144 10 50       787 if ($@) {
145 10 50       185 die $@ unless ref $@;
146 10 50       317 $@->rethrow if $@->fatal;
147 10         84 $rv = $@;
148             }
149 10         44 return $rv;
150             }
151              
152             =head2 autosleep
153              
154             Allows one to configure the sleep period between fetches
155             The default is 1 (as of 1.86).
156              
157             my $period = $ua->autosleep;
158             $ua->autosleep( 10 ); # for a 10 second delay
159              
160             =cut
161              
162             sub autosleep
163             {
164 19     19 1 29 my $w = shift;
165 19         37 my $key = __PACKAGE__.'-sleep';
166 19 100       97 if (@_) {
167             my ($sleep) = validate_pos( @_,
168             { type => SCALAR, callbacks => {
169 17     17   4451 'is integer' => sub { shift() =~ /^ \d+ $/x },
170             } }, # number
171 18         1081 );
172 13         102 $w->{$key} = $sleep;
173             }
174 14 50       90 return ( exists $w->{$key} ? $w->{$key} : 1 );
175             }
176              
177             1;
178              
179             __DATA__