| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Net::HTTP::Knork::Role::Middleware; | 
| 2 |  |  |  |  |  |  | # ABSTRACT: Role implementing middleware capabilities for Knork main class | 
| 3 | 6 |  |  | 6 |  | 41364 | use Moo::Role; | 
|  | 6 |  |  |  |  | 10 |  | 
|  | 6 |  |  |  |  | 32 |  | 
| 4 | 6 |  |  | 6 |  | 1578 | use Carp; | 
|  | 6 |  |  |  |  | 20 |  | 
|  | 6 |  |  |  |  | 412 |  | 
| 5 | 6 |  |  | 6 |  | 2781 | use Class::Method::Modifiers qw/install_modifier/; | 
|  | 6 |  |  |  |  | 6862 |  | 
|  | 6 |  |  |  |  | 1294 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | requires qw/perform_request generate_response/; | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | sub add_middleware { | 
| 11 | 2 |  |  | 2 | 0 | 152 | my $self    = shift; | 
| 12 | 2 |  |  |  |  | 4 | my $mw_spec = shift; | 
| 13 | 2 | 50 | 33 |  |  | 15 | croak | 
| 14 |  |  |  |  |  |  | "A middleware should specify at least a action 'on_requests' or 'on_response'" | 
| 15 |  |  |  |  |  |  | unless ( defined( $mw_spec->{on_request} ) | 
| 16 |  |  |  |  |  |  | || defined( $mw_spec->{on_response} ) ); | 
| 17 | 2 |  |  |  |  | 5 | my $class = ref($self); | 
| 18 | 2 | 50 |  |  |  | 7 | if ( $mw_spec->{on_request} ) { | 
| 19 |  |  |  |  |  |  | install_modifier $class, 'around', 'perform_request', sub { | 
| 20 | 2 |  |  | 2 |  | 48 | my $orig = shift; | 
| 21 | 2 |  |  |  |  | 3 | my ( $self, $request ) = @_; | 
| 22 | 2 |  |  |  |  | 5 | my $meth        = $mw_spec->{on_request}; | 
| 23 | 2 |  |  |  |  | 8 | my $new_request = $meth->($self,$request); | 
| 24 | 2 |  |  |  |  | 184 | $orig->( $self, $new_request ); | 
| 25 |  |  |  |  |  |  | } | 
| 26 | 2 |  |  |  |  | 16 | } | 
| 27 |  |  |  |  |  |  |  | 
| 28 | 2 | 50 |  |  |  | 503 | if ( $mw_spec->{on_response} ) { | 
| 29 |  |  |  |  |  |  | install_modifier $class, 'around', 'generate_response', sub { | 
| 30 | 2 |  |  | 2 |  | 49 | my $orig = shift; | 
| 31 | 2 |  |  |  |  | 3 | my ( $self, $resp ) = @_; | 
| 32 | 2 |  |  |  |  | 5 | my $meth        = $mw_spec->{on_response}; | 
| 33 | 2 |  |  |  |  | 28 | my $old_resp = $resp->clone; | 
| 34 | 2 |  |  |  |  | 500 | my $new_resp = $meth->($self,$old_resp); | 
| 35 | 2 |  |  |  |  | 63 | $orig->( $self, $new_resp, $resp ); | 
| 36 |  |  |  |  |  |  | } | 
| 37 | 2 |  |  |  |  | 14 | } | 
| 38 |  |  |  |  |  |  | } | 
| 39 |  |  |  |  |  |  |  | 
| 40 |  |  |  |  |  |  | 1; | 
| 41 |  |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  | __END__ |