| 1 | package WWW::Chain; | ||||||||||||||||||||||||||||
| 2 | 5 | our $VERSION = '0.101'; | |||||||||||||||||||||||||||
| 3 | 5 | our $AUTHORITY = 'cpan:GETTY'; | |||||||||||||||||||||||||||
| 4 | # ABSTRACT: A web request chain | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use MooX::Types::MooseLike::Base qw(:all); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use Safe::Isa; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 5 | ▶ | use WWW::Chain::UA::LWP; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 5 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 5 | our @EXPORT = qw( www_chain ); | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 5 | has stash => ( | |||||||||||||||||||||||||||
| 15 | isa => HashRef, | ||||||||||||||||||||||||||||
| 16 | is => 'lazy', | ||||||||||||||||||||||||||||
| 17 | ); | ||||||||||||||||||||||||||||
| 18 | 4 | ▶ | sub _build_stash {{}} | ||||||||||||||||||||||||||
|
Subroutine
_build_stash: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 5 | has next_requests => ( | |||||||||||||||||||||||||||
| 21 | isa => ArrayRef, | ||||||||||||||||||||||||||||
| 22 | is => 'rwp', | ||||||||||||||||||||||||||||
| 23 | clearer => 1, | ||||||||||||||||||||||||||||
| 24 | ); | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 5 | has next_step => ( | |||||||||||||||||||||||||||
| 27 | isa => AnyOf[Str, CodeRef], | ||||||||||||||||||||||||||||
| 28 | is => 'rwp', | ||||||||||||||||||||||||||||
| 29 | clearer => 1, | ||||||||||||||||||||||||||||
| 30 | ); | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | has done => ( | ||||||||||||||||||||||||||||
| 33 | isa => Bool, | ||||||||||||||||||||||||||||
| 34 | is => 'rwp', | ||||||||||||||||||||||||||||
| 35 | lazy => 1, | ||||||||||||||||||||||||||||
| 36 | 4 | ▶ | default => sub { 0 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | 5 | ); | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | has request_count => ( | ||||||||||||||||||||||||||||
| 40 | isa => Num, | ||||||||||||||||||||||||||||
| 41 | is => 'rwp', | ||||||||||||||||||||||||||||
| 42 | lazy => 1, | ||||||||||||||||||||||||||||
| 43 | 1 | ▶ | default => sub { 0 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | 5 | ); | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | has result_count => ( | ||||||||||||||||||||||||||||
| 47 | isa => Num, | ||||||||||||||||||||||||||||
| 48 | is => 'rwp', | ||||||||||||||||||||||||||||
| 49 | lazy => 1, | ||||||||||||||||||||||||||||
| 50 | 4 | ▶ | default => sub { 0 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 51 | 5 | ); | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 3 | ▶ | sub www_chain { | ||||||||||||||||||||||||||
|
Subroutine
www_chain: called CC=3 SCAR=11.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 54 | 3 | my ( @args ) = @_; | |||||||||||||||||||||||||||
| 55 | 3 | my ( $next_requests, $next_step, @others ) = __PACKAGE__->parse_chain(@args); | |||||||||||||||||||||||||||
| 56 | 3 | ▶ | die __PACKAGE__." can only use coderef as next step" unless !$next_step or ref $next_step eq 'CODE'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $next_step or ref $next_step eq "CODE"
MC/DC0%0/2
not $next_step or ref $next_step eq "CODE"
not $next_step
ref $next_step eq "CODE"
Truth table33%1/3
not(A) or B
A= not $next_stepB= ref $next_step eq "CODE"
| |||||||||||||||||||||||||||||
| 57 | return WWW::Chain->new( | ||||||||||||||||||||||||||||
| 58 | next_requests => $next_requests, | ||||||||||||||||||||||||||||
| 59 | next_step => $next_step, | ||||||||||||||||||||||||||||
| 60 | 3 | request_count => scalar @{$next_requests}, | |||||||||||||||||||||||||||
| 61 | @others, | ||||||||||||||||||||||||||||
| 62 | ); | ||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 1 | ▶ | sub request_with_lwp { | ||||||||||||||||||||||||||
|
Subroutine
request_with_lwp: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 66 | 1 | my ( $self ) = @_; | |||||||||||||||||||||||||||
| 67 | 1 | return WWW::Chain::UA::LWP->new->request_chain($self); | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 8 | ▶ | sub is_response { $_[1]->$_isa('HTTP::Response') } | ||||||||||||||||||||||||||
|
Subroutine
is_response: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 71 | 24 | ▶ | sub is_request { $_[1]->$_isa('HTTP::Request') } | ||||||||||||||||||||||||||
|
Subroutine
is_request: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 8 | ▶ | sub parse_chain { | ||||||||||||||||||||||||||
|
Subroutine
parse_chain: called CC=8 SCAR=22.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 74 | 8 | my ( $self, @args ) = @_; | |||||||||||||||||||||||||||
| 75 | 8 | my $step; | |||||||||||||||||||||||||||
| 76 | my @requests; | ||||||||||||||||||||||||||||
| 77 | 8 | ▶ | while (@args) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 16 | my $arg = shift @args; | |||||||||||||||||||||||||||
| 79 | 16 | ▶ | if ( $self->is_request($arg) ) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 80 | 8 | push @requests, $arg; | |||||||||||||||||||||||||||
| 81 | } elsif (ref $arg eq '') { | ||||||||||||||||||||||||||||
| 82 | 2 | ▶ | die "".(ref $self)."->parse_chain '".$arg."' is not a known function" unless $self->can($arg); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 2 | $step = $arg; | |||||||||||||||||||||||||||
| 84 | 2 | last; | |||||||||||||||||||||||||||
| 85 | } elsif (ref $arg eq 'CODE') { | ||||||||||||||||||||||||||||
| 86 | 6 | $step = $arg; | |||||||||||||||||||||||||||
| 87 | 6 | last; | |||||||||||||||||||||||||||
| 88 | } else { | ||||||||||||||||||||||||||||
| 89 | 0 | ▶ | die __PACKAGE__."->parse_chain got unparseable element".(defined $arg ? " ".$arg : "" ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | 8 | ▶ | die __PACKAGE__."->parse_chain found no HTTP::Request objects" unless @requests; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 8 | return \@requests, $step, @args; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 8 | ▶ | sub next_responses { | ||||||||||||||||||||||||||
|
Subroutine
next_responses: called CC=6 SCAR=18.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 97 | 8 | my ( $self, @responses ) = @_; | |||||||||||||||||||||||||||
| 98 | 8 | ▶ | die "".(ref $self)."->next_responses can't be called on chain which is done." if $self->done; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 8 | my $amount = scalar @{$self->next_requests}; | |||||||||||||||||||||||||||
| 100 | 8 | ▶ | die "".(ref $self)."->next_responses would need ".$amount." HTTP::Response objects to proceed" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | unless scalar @responses == $amount; | ||||||||||||||||||||||||||||
| 102 | die "".(ref $self)."->next_responses only takes HTTP::Response objects" | ||||||||||||||||||||||||||||
| 103 | 8 | ▶ | if grep { !$self->is_response($_) } @responses; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 8 | $self->clear_next_requests; | |||||||||||||||||||||||||||
| 105 | 8 | my @result = $self->${\$self->next_step}(@responses); | |||||||||||||||||||||||||||
| 106 | 8 | $self->clear_next_step; | |||||||||||||||||||||||||||
| 107 | 8 | $self->_set_result_count($self->result_count + 1); | |||||||||||||||||||||||||||
| 108 | # If the first result is a request again, then we need to parse_chain again. | ||||||||||||||||||||||||||||
| 109 | 8 | ▶ | if ( $self->is_request($result[0]) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 4 | my ( $next_requests, $next_step, @others ) = $self->parse_chain(@result); | |||||||||||||||||||||||||||
| 111 | 4 | ▶ | die "".(ref $self)."->next_responses can't parse the result, more arguments after next step" if @others; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 4 | $self->_set_next_requests($next_requests); | |||||||||||||||||||||||||||
| 113 | 4 | $self->_set_next_step($next_step); | |||||||||||||||||||||||||||
| 114 | 4 | $self->_set_request_count($self->request_count + scalar @{$next_requests}); | |||||||||||||||||||||||||||
| 115 | 4 | return 0; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | 4 | $self->_set_done(1); | |||||||||||||||||||||||||||
| 118 | 4 | return $self->stash; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 4 | ▶ | sub BUILD { | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=6 SCAR=18.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 122 | 4 | my ( $self ) = @_; | |||||||||||||||||||||||||||
| 123 | 4 | ▶ | unless ($self->next_requests) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 1 | ▶ | die "".(ref $self)." has no start_chain function and no requests supplied on build" unless $self->can('start_chain'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 1 | my ( $next_requests, $next_step, @others ) = $self->parse_chain($self->start_chain); | |||||||||||||||||||||||||||
| 126 | 1 | ▶ | die "".(ref $self)." parse_chain can't parse the start_chain return, more arguments after next step" if scalar @others > 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 1 | ▶ | die "".(ref $self)." has no requests from start_chain" unless scalar @{$next_requests} > 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | 1 | ▶ | $self->_set_next_step($next_step) if $next_step; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 1 | $self->_set_next_requests($next_requests); | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 5 | 1; | |||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | __END__ | ||||||||||||||||||||||||||||