| 1 | package HTTP::Response::Switch; | ||||||||||||||
| 2 | { | ||||||||||||||
| 3 | 2 | $HTTP::Response::Switch::VERSION = '1.1.1'; | |||||||||||||
| 4 | } | ||||||||||||||
| 5 | # ABSTRACT: handle many HTTP response possibilities | ||||||||||||||
| 6 | |||||||||||||||
| 7 | |||||||||||||||
| 8 | 2 | ▶ | use Moose::Role; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 2 | ▶ | use namespace::autoclean; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | |||||||||||||||
| 11 | 2 | ▶ | use HTTP::Response::Switch::HandlerDeclinedResponse (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | 2 | ▶ | use Module::Find (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | 2 | ▶ | use Module::Load (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | 2 | ▶ | use TryCatch 1.001000; # for bug fix; imports "try" and "catch" | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 15 | |||||||||||||||
| 16 | |||||||||||||||
| 17 | 2 | requires 'handler_namespace'; | |||||||||||||
| 18 | |||||||||||||||
| 19 | |||||||||||||||
| 20 | 5 | ▶ | sub default_handlers { () } | ||||||||||||
|
Subroutine
default_handlers: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 21 | |||||||||||||||
| 22 | |||||||||||||||
| 23 | 2 | ▶ | sub default_exception { die 'unexpected HTTP response' } | ||||||||||||
|
Subroutine
default_exception: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 24 | |||||||||||||||
| 25 | |||||||||||||||
| 26 | 2 | ▶ | sub load_classes { | ||||||||||||
|
Subroutine
load_classes: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 27 | 2 | my $class = shift; | |||||||||||||
| 28 | |||||||||||||||
| 29 | # Load all of the classes under the handler_namespace. | ||||||||||||||
| 30 | 2 | Module::Find::useall($class->handler_namespace); | |||||||||||||
| 31 | |||||||||||||||
| 32 | # If the "default_exception" method returns a string then treat it | ||||||||||||||
| 33 | # as an exception class. Ignore any failures, because any failures | ||||||||||||||
| 34 | # are probably by design (e.g. the default implementation). | ||||||||||||||
| 35 | 2 | my $exception_class; | |||||||||||||
| 36 | Module::Load::load($exception_class) | ||||||||||||||
| 37 | 2 | ▶ | if eval { $exception_class = $class->default_exception; 1 }; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 38 | } | ||||||||||||||
| 39 | |||||||||||||||
| 40 | |||||||||||||||
| 41 | 0 | ▶ | sub load_handlers { goto &load_classes; } | ||||||||||||
|
Subroutine
load_handlers: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 42 | |||||||||||||||
| 43 | |||||||||||||||
| 44 | 5 | ▶ | sub handle { | ||||||||||||
|
Subroutine
handle: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||
| 45 | 5 | my ($class, $res, @handlers) = @_; | |||||||||||||
| 46 | |||||||||||||||
| 47 | 5 | for my $handler (@handlers, $class->default_handlers) { | |||||||||||||
| 48 | 2 | ▶ | try { | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 49 | 7 | my $handler_class | |||||||||||||
| 50 | = $class->handler_namespace . '::' . $handler; | ||||||||||||||
| 51 | 7 | return $handler_class->new({ response => $res })->handle; | |||||||||||||
| 52 | } | ||||||||||||||
| 53 | 2 | ▶ | catch (HTTP::Response::Switch::HandlerDeclinedResponse $e) { | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Branch: 2 branches
| |||||||||||||||
| 54 | # This handler declined to handle this response. | ||||||||||||||
| 55 | # Move on to the next handler. | ||||||||||||||
| 56 | } | ||||||||||||||
| 57 | 2 | } | |||||||||||||
| 58 | |||||||||||||||
| 59 | # All of the specified handlers declined to handle the response. | ||||||||||||||
| 60 | 2 | die $class->default_exception->new({ response => $res }); | |||||||||||||
| 61 | } | ||||||||||||||
| 62 | |||||||||||||||
| 63 | |||||||||||||||
| 64 | 2 | 1; | |||||||||||||
| 65 | |||||||||||||||
| 66 | __END__ | ||||||||||||||