| 1 | package Net::HTTP::Knork::Role::Middleware; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Role implementing middleware capabilities for Knork main class | ||||||||||||||||||||||||||||
| 3 | 6 | ▶ | use Moo::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 6 | ▶ | use Class::Method::Modifiers qw/install_modifier/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 6 | requires qw/perform_request generate_response/; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | sub add_middleware { | ||||||||||||||||||||||||||||
| 11 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
add_middleware: called CC=5 SCAR=16.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 12 | 2 | my $mw_spec = shift; | |||||||||||||||||||||||||||
| 13 | croak | ||||||||||||||||||||||||||||
| 14 | "A middleware should specify at least a action 'on_requests' or 'on_response'" | ||||||||||||||||||||||||||||
| 15 | unless ( defined( $mw_spec->{on_request} ) | ||||||||||||||||||||||||||||
| 16 | 2 | ▶ | || defined( $mw_spec->{on_response} ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $mw_spec->{"on_request"} or defined $mw_spec->{"on_response"}
MC/DC0%0/2
defined $mw_spec->{"on_request"} or defined $mw_spec->{"on_response"}
defined $mw_spec->{"on_request"}
defined $mw_spec->{"on_response"}
Truth table33%1/3
A or B
A= defined $mw_spec->{"on_request"}B= defined $mw_spec->{"on_response"}
| |||||||||||||||||||||||||||||
| 17 | 2 | my $class = ref($self); | |||||||||||||||||||||||||||
| 18 | 2 | ▶ | if ( $mw_spec->{on_request} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 19 | install_modifier $class, 'around', 'perform_request', sub { | ||||||||||||||||||||||||||||
| 20 | 2 | ▶ | my $orig = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 2 | my ( $self, $request ) = @_; | |||||||||||||||||||||||||||
| 22 | 2 | my $meth = $mw_spec->{on_request}; | |||||||||||||||||||||||||||
| 23 | 2 | my $new_request = $meth->($self,$request); | |||||||||||||||||||||||||||
| 24 | 2 | $orig->( $self, $new_request ); | |||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | 2 | } | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 2 | ▶ | if ( $mw_spec->{on_response} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | install_modifier $class, 'around', 'generate_response', sub { | ||||||||||||||||||||||||||||
| 30 | 2 | ▶ | my $orig = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | 2 | my ( $self, $resp ) = @_; | |||||||||||||||||||||||||||
| 32 | 2 | my $meth = $mw_spec->{on_response}; | |||||||||||||||||||||||||||
| 33 | 2 | my $old_resp = $resp->clone; | |||||||||||||||||||||||||||
| 34 | 2 | my $new_resp = $meth->($self,$old_resp); | |||||||||||||||||||||||||||
| 35 | 2 | $orig->( $self, $new_resp, $resp ); | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | 2 | } | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 6 | 1; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | __END__ | ||||||||||||||||||||||||||||