File Coverage

blib/lib/WWW/JSON/Response.pm
Criterion Covered Total %
statement 31 31 100.0
branch 7 8 87.5
condition 3 3 100.0
subroutine 11 11 100.0
pod 2 2 100.0
total 54 55 98.1


line stmt bran cond sub pod time code
1             package WWW::JSON::Response;
2 8     8   42 use strict;
  8         10  
  8         294  
3 8     8   91 use warnings;
  8         11  
  8         253  
4 8     8   41 use Moo;
  8         11  
  8         41  
5 8     8   8018 use JSON::XS;
  8         41447  
  8         569  
6 8     8   60 use Try::Tiny;
  8         12  
  8         2826  
7              
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             has json => ( is => 'lazy', default => sub { JSON::XS->new } );
20             has response => ( is => 'lazy', builder => '_build_response' );
21             has error => ( is => 'lazy', writer => '_set_error' );
22             has request_object => ( is => 'ro' );
23             has _response_transform => ( is => 'ro' );
24              
25 13     13 1 18550 sub success { !shift->error }
26              
27             sub _build_error {
28 18     18   3128 my $self = shift;
29 18         82 $self->_set_error('');
30 18         255 $self->response;
31 18         647 return $self->error;
32             }
33              
34             sub _build_response {
35 23     23   3192 my $self = shift;
36              
37 23 100       145 $self->_set_error( $self->status_line )
38             unless ( $self->http_response->is_success );
39              
40 23 100       1491 return unless ($self->http_response->decoded_content);
41              
42             my $decoded = try {
43 21     21   1110 $self->json->decode( $self->http_response->decoded_content );
44             }
45             catch {
46 1 50   1   492 $self->_set_error("$_") unless ( $self->error );
47 1         11 return;
48 21         2873 };
49              
50 21 100 100     5787 if ( !( $self->error ) && $self->_response_transform ) {
51 2         23 $decoded = $self->_response_transform->($decoded);
52             }
53 21         324 return $decoded;
54             }
55              
56 14     14 1 15083 sub res { shift->response }
57              
58             1;
59              
60             __END__