File Coverage

blib/lib/WWW/Chain.pm
Criterion Covered Total %
statement 51 52 98.0
branch 14 24 58.3
condition 1 3 33.3
subroutine 8 8 100.0
pod 0 3 0.0
total 74 90 82.2


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