File Coverage

blib/lib/WWW/Chain.pm
Criterion Covered Total %
statement 50 51 98.0
branch 14 24 58.3
condition 1 3 33.3
subroutine 7 7 100.0
pod 0 3 0.0
total 72 88 81.8


line stmt bran cond sub pod time code
1             package WWW::Chain;
2             our $AUTHORITY = 'cpan:GETTY';
3             # ABSTRACT: A web request chain
4              
5             our $VERSION ||= '0.000';
6              
7              
8 4     4   268117 use Moo;
  4         34106  
  4         16  
9 4     4   6216 use MooX::Types::MooseLike::Base qw(:all);
  4         21561  
  4         1079  
10 4     4   1496 use Safe::Isa;
  4         1475  
  4         2514  
11              
12             has stash => (
13             isa => HashRef,
14             is => 'lazy',
15             );
16              
17 3     3   1181 sub _build_stash {{}}
18              
19             has next_requests => (
20             isa => ArrayRef,
21             is => 'rwp',
22             clearer => 1,
23             required => 1,
24             );
25              
26             has next_coderef => (
27             #isa => CodeRef,
28             is => 'rwp',
29             clearer => 1,
30             );
31              
32             has done => (
33             isa => Bool,
34             is => 'rwp',
35             default => sub { 0 },
36             );
37              
38             has request_count => (
39             isa => Num,
40             is => 'rwp',
41             default => sub { 0 },
42             );
43              
44             has result_count => (
45             isa => Num,
46             is => 'rwp',
47             default => sub { 0 },
48             );
49              
50             sub BUILDARGS {
51 3     3 0 38511 my $self = shift;
52 3 50 33     22 return $_[0] if (scalar @_ == 1 && ref $_[0] eq 'HASH');
53 3         23 my ( $next_requests, $next_coderef, @args ) = $self->parse_chain(@_);
54             return {
55             next_requests => $next_requests,
56             next_coderef => $next_coderef,
57 3         7 request_count => scalar @{$next_requests},
  3         64  
58             @args,
59             };
60             }
61              
62             sub parse_chain {
63 6     6 0 22 my ( $self, @args ) = @_;
64 6         11 my $coderef;
65             my @requests;
66 6         25 while (@args) {
67 12         19 my $arg = shift @args;
68 12 100       38 if ( $arg->$_isa('HTTP::Request') ) {
    100          
    50          
69 6         124 push @requests, $arg;
70             } elsif (ref $arg eq 'CODE') {
71 4         34 $coderef = $arg;
72 4         7 last;
73             } elsif (ref $arg eq '') {
74 2 50       22 die "".(ref $self)."->parse_chain '".$arg."' is not a known function" unless $self->can($arg);
75 2         3 $coderef = $arg;
76 2         3 last;
77             } else {
78 0 0       0 die "".(ref $self)."->parse_chain got unparseable element".(defined $arg ? " ".$arg : "" );
79             }
80             }
81 6 50       15 die "".(ref $self)."->parse_chain found no HTTP::Request objects" unless @requests;
82 6         19 return [@requests], $coderef, @args;
83             }
84              
85             sub next_responses {
86 6     6 0 1769 my ( $self, @responses ) = @_;
87 6 50       46 die "".(ref $self)."->next_responses can't be called on chain which is done." if $self->done;
88 6         22 my $amount = scalar @{$self->next_requests};
  6         16  
89 6 50       21 die "".(ref $self)."->next_responses would need ".$amount." HTTP::Response objects to proceed"
90             unless scalar @responses == $amount;
91             die "".(ref $self)."->next_responses only takes HTTP::Response objects"
92 6 50       13 if grep { !$_->isa('HTTP::Response') } @responses;
  6         32  
93 6         138 $self->clear_next_requests;
94 6         57 my @result = $self->${\$self->next_coderef}(@responses);
  6         31  
95 6         7458 $self->clear_next_coderef;
96 6         137 $self->_set_result_count($self->result_count + 1);
97 6 100       256 if ( $result[0]->$_isa('HTTP::Request') ) {
98 3         52 my ( $next_requests, $next_coderef, @args ) = $self->parse_chain(@result);
99 3 50       21 die "".(ref $self)."->next_responses can't parse the result, more arguments after CodeRef" if @args;
100 3         60 $self->_set_next_requests($next_requests);
101 3         136 $self->_set_next_coderef($next_coderef);
102 3         9 $self->_set_request_count($self->request_count + scalar @{$next_requests});
  3         56  
103 3         120 return 0;
104             }
105 3         85 $self->_set_done(1);
106 3         158 return $self->stash;
107             }
108              
109             1;
110              
111             __END__