| 1 | package WWW::JSON::Response; | ||||||||||||||||||||||||||||
| 2 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 8 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 8 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 8 | ▶ | use JSON::XS; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 8 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 8 | has http_response => ( | |||||||||||||||||||||||||||
| 9 | is => 'ro', | ||||||||||||||||||||||||||||
| 10 | required => 1, | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | handles => { | ||||||||||||||||||||||||||||
| 13 | status_line => 'status_line', | ||||||||||||||||||||||||||||
| 14 | code => 'code', | ||||||||||||||||||||||||||||
| 15 | url => 'base', | ||||||||||||||||||||||||||||
| 16 | content => 'decoded_content', | ||||||||||||||||||||||||||||
| 17 | }, | ||||||||||||||||||||||||||||
| 18 | ); | ||||||||||||||||||||||||||||
| 19 | 0 | ▶ | has json => ( is => 'lazy', default => sub { JSON::XS->new } ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 20 | 8 | has response => ( is => 'lazy', builder => '_build_response' ); | |||||||||||||||||||||||||||
| 21 | 8 | has error => ( is => 'lazy', writer => '_set_error' ); | |||||||||||||||||||||||||||
| 22 | 8 | has request_object => ( is => 'ro' ); | |||||||||||||||||||||||||||
| 23 | 8 | has _response_transform => ( is => 'ro' ); | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 13 | ▶ | sub success { !shift->error } | ||||||||||||||||||||||||||
|
Subroutine
success: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | sub _build_error { | ||||||||||||||||||||||||||||
| 28 | 18 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_build_error: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 18 | $self->_set_error(''); | |||||||||||||||||||||||||||
| 30 | 18 | $self->response; | |||||||||||||||||||||||||||
| 31 | 18 | return $self->error; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | sub _build_response { | ||||||||||||||||||||||||||||
| 35 | 23 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_build_response: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 23 | ▶ | $self->_set_error( $self->status_line ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | unless ( $self->http_response->is_success ); | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 23 | ▶ | return unless ($self->http_response->decoded_content); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | my $decoded = try { | ||||||||||||||||||||||||||||
| 43 | 21 | ▶ | $self->json->decode( $self->http_response->decoded_content ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | catch { | ||||||||||||||||||||||||||||
| 46 | 1 | ▶ | $self->_set_error("$_") unless ( $self->error ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 1 | return; | |||||||||||||||||||||||||||
| 48 | 21 | }; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 21 | ▶ | if ( !( $self->error ) && $self->_response_transform ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $self->error and $self->_response_transform
MC/DC100%2/2
not $self->error and $self->_response_transform
not $self->error
$self->_response_transform
Truth table100%3/3
not(A) and B
A= not $self->errorB= $self->_response_transform
| |||||||||||||||||||||||||||||
| 51 | 2 | $decoded = $self->_response_transform->($decoded); | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | 21 | return $decoded; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 14 | ▶ | sub res { shift->response } | ||||||||||||||||||||||||||
|
Subroutine
res: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 8 | 1; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | __END__ | ||||||||||||||||||||||||||||