File Coverage

blib/lib/WWW/Crawl4AI/StrategyChain.pm
Criterion Covered Total %
statement 29 41 70.7
branch 0 2 0.0
condition n/a
subroutine 10 13 76.9
pod 1 5 20.0
total 40 61 65.5


line stmt bran cond sub pod time code
1             package WWW::Crawl4AI::StrategyChain;
2             # ABSTRACT: ordered list of strategy objects, pluggable at construction time
3 2     2   10 use Moo;
  2         3  
  2         9  
4 2     2   1288 use WWW::Crawl4AI::Strategy::Plain ();
  2         5  
  2         49  
5 2     2   741 use WWW::Crawl4AI::Strategy::Browser ();
  2         5  
  2         54  
6 2     2   750 use WWW::Crawl4AI::Strategy::Stealth ();
  2         5  
  2         60  
7 2     2   812 use WWW::Crawl4AI::Strategy::CloakBrowser ();
  2         6  
  2         47  
8 2     2   727 use WWW::Crawl4AI::Strategy::Proxy ();
  2         5  
  2         47  
9 2     2   782 use WWW::Crawl4AI::Strategy::Callback ();
  2         5  
  2         685  
10              
11             our $VERSION = '0.001';
12              
13              
14             # Default order, cheapest first. Override in subclass or at construction.
15             my @CHAIN_CLASSES = qw(
16             WWW::Crawl4AI::Strategy::Plain
17             WWW::Crawl4AI::Strategy::Browser
18             WWW::Crawl4AI::Strategy::Stealth
19             WWW::Crawl4AI::Strategy::CloakBrowser
20             WWW::Crawl4AI::Strategy::Proxy
21             WWW::Crawl4AI::Strategy::Callback
22             );
23              
24              
25 16     16 1 40 sub chain_classes { @CHAIN_CLASSES }
26              
27             has strategies => (
28             is => 'ro',
29             builder => 1,
30             );
31              
32              
33             sub _build_strategies {
34 16     16   965 my ( $self ) = @_;
35 16         29 return [ map { $_->new } $self->chain_classes ];
  96         771  
36             }
37              
38             sub add_strategy {
39 0     0 0 0 my ( $self, $strategy ) = @_;
40 0         0 push @{ $self->{strategies} }, $strategy;
  0         0  
41             }
42              
43             sub remove_strategy {
44 0     0 0 0 my ( $self, $name ) = @_;
45 0         0 @{ $self->{strategies} } = grep { $_->name ne $name } @{ $self->{strategies} };
  0         0  
  0         0  
  0         0  
46             }
47              
48             sub replace_strategy {
49 0     0 0 0 my ( $self, $name, $strategy ) = @_;
50 0         0 my $s = $self->{strategies};
51 0         0 for my $i ( 0 .. $#$s ) {
52 0 0       0 $s->[$i] = $strategy if $s->[$i]->name eq $name;
53             }
54             }
55              
56             # Filter strategies by applicable($crawler) for this crawler instance.
57             sub applicable {
58 6     6 0 36 my ( $self, $crawler ) = @_;
59 6         7 return [ grep { $_->applicable($crawler) } @{ $self->strategies } ];
  36         79  
  6         45  
60             }
61              
62             1;
63              
64             __END__