File Coverage

blib/lib/WWW/Mechanize/Sleepy.pm
Criterion Covered Total %
statement 25 49 51.0
branch 6 12 50.0
condition 2 3 66.6
subroutine 6 11 54.5
pod 2 5 40.0
total 41 80 51.2


line stmt bran cond sub pod time code
1             package WWW::Mechanize::Sleepy;
2              
3             our $VERSION = 0.7;
4              
5 2     2   2759 use strict;
  2         4  
  2         88  
6 2     2   11 use warnings;
  2         3  
  2         80  
7 2     2   23 use Carp qw( croak );
  2         3  
  2         142  
8 2     2   12 use base qw( WWW::Mechanize );
  2         3  
  2         2612  
9              
10             =head1 NAME
11              
12             WWW::Mechanize::Sleepy - A Sleepy Mechanize Agent
13              
14             =head1 SYNOPSIS
15              
16             use WWW::Mechanize::Sleepy;
17            
18             # sleep 5 seconds between requests
19             my $a = WWW::Mechanize::Sleepy->new( sleep => 5 );
20             $a->get( 'http://www.cpan.org' );
21              
22             # sleep between 5 and 20 seconds between requests
23             my $a = WWW::Mechanize::Sleepy->new( sleep => '5..20' );
24             $a->get( 'http://www.cpan.org' );
25              
26             # don't sleep at all
27             my $a = WWW::Mechanize::Sleepy->new();
28             $a->get( 'http://www.cpan.org' );
29              
30             =head1 DESCRIPTION
31              
32             Sometimes when testing the behavior of a webserver it is important to be able
33             to space out your requests in order to simulate a person reading, thinking (or
34             sleeping) at the keyboard.
35              
36             WWW::Mechanize::Sleepy subclasses WWW::Mechanize to provide pauses between your server requests. Use it just like you would use WWW::Mechanize.
37              
38             =head1 METHODS
39              
40             All the methods are the same as WWW::Mechanize, except for the constructor
41             which accepts an additional parameter.
42              
43             =head2 new()
44              
45             The constructor which acts just like the WWW::Mechanize constructor except
46             you can pass it an extra parameter.
47              
48             =over 4
49              
50             =item * sleep
51              
52             An amount of time in seconds to sleep.
53              
54             my $a = WWW::Mechanize::Sleepy->new( sleep => 5 );
55              
56             Or a range of time to sleep within. Your robot will sleep a random
57             amount of time within that range.
58              
59             my $a = WWW::Mechanize::Sleepy->new( sleep => '5..20' );
60              
61             If you would like to have a non sleeping WWW::Mechanize object, you can
62             simply not pass in the sleep paramter.
63              
64             my $a = WWW::Mechanize::Sleepy->new();
65              
66             =back
67              
68             Note: since WWW::Mechanize::Sleepy subclasses WWW::Mechanize, which subclasses
69             LWP::UserAgent, you can pass in LWP::UserAgent::new() options to
70             WWW::Mechanize::Sleepy::new().
71              
72             my $a = WWW::Mechanize::Sleepy->new(
73             agent => 'foobar agent',
74             timeout => 100
75             );
76              
77             =cut
78              
79             sub new {
80 5     5 1 3507 my $class = shift;
81 5         15 my %parms = @_;
82 5         8 my $sleep = 0;
83 5 100       19 if ( exists( $parms{ sleep } ) ) {
84 3         7 $sleep = $parms{ sleep };
85 3         9 _sleepCheck( $sleep );
86 1         2 delete( $parms{ sleep } );
87             }
88 3         160 my $self = $class->SUPER::new( %parms );
89 0         0 $self->{ Sleepy_Time } = $sleep;
90 0         0 return( $self );
91             }
92              
93             =head2 sleep()
94              
95             If you want to get or set your object's sleep value on the fly use sleep().
96              
97             my $a = WWW::Mechanize::Sleepy->new( sleep => '1..3' );
98             ...
99             print "currently sleeping ", $a->sleep(), " seconds\n";
100             $a->sleep( '4..6' );
101              
102             If you want to make your WWW::Mechanize::Sleepy object no longer sleepy just
103             set to 0.
104              
105             $a->sleep( 0 );
106              
107             =cut
108              
109             sub sleep {
110 0     0 1 0 my ( $self, $arg ) = @_;
111 0 0       0 if ( defined( $arg ) ) {
112 0         0 _sleepCheck( $arg );
113 0         0 $self->{ Sleepy_Time } = $arg;
114             }
115 0         0 return( $self->{ Sleepy_Time } );
116             }
117              
118             sub back {
119 0     0 0 0 my $self = shift;
120 0         0 $self->_sleep();
121 0         0 $self->SUPER::back( @_ );
122             }
123              
124             sub request {
125 0     0 0 0 my $self = shift;
126 0         0 $self->_sleep();
127 0         0 $self->SUPER::request( @_ );
128             }
129              
130             sub reload {
131 0     0 0 0 my $self = shift;
132 0         0 $self->_sleep();
133 0         0 $self->SUPER::reload( @_ );
134             }
135              
136             sub _sleep {
137 0     0   0 my $self = shift;
138 0 0       0 return( 1 ) if $self->{ Sleepy_Time } eq '0';
139 0         0 my $sleep;
140 0 0       0 if ( $self->{ Sleepy_Time } =~ /^(\d+)\.\.(\d+)$/ ) {
141 0         0 $sleep = int( rand( $2 - $1 ) ) + $1;
142             } else {
143 0         0 $sleep = $self->{ Sleepy_Time };
144             }
145 0         0 CORE::sleep( $sleep );
146 0         0 return( 1 );
147             }
148              
149             sub _sleepCheck {
150 3     3   6 my $sleep = shift;
151 3 100       484 croak( "sleep parameter must be an integer or a range i1..i2" )
152             if ( $sleep !~ /^(\d+)|(\d+\.\.\d+)$/ );
153 2 100 66     19 if ( $sleep =~ /(\d+)\.\.(\d+)/ and $1 >= $2 ) {
154 1         149 croak( "sleep range (i1..i2) must have i1 < i2" );
155             }
156 1         2 return( 1 );
157             }
158              
159              
160             =head1 AUTHOR/MAINTAINER
161              
162             WWW::Mechanize::Sleepy was originally written in 2003 by Ed Summers (ehs@pobox.com).
163             Since version 0.7 (September 2010) it has been maintained by Kostas Ntonas (kntonas@gmail.com).
164              
165             =head1 SEE ALSO
166              
167             =over 4
168              
169             =item * L
170              
171             =back
172              
173             =head1 LICENSE
174              
175             This library is free software; you can redistribute it and/or modify it under
176             the same terms as Perl itself.
177              
178             =cut
179              
180             1;