File Coverage

blib/lib/WebService/BitbucketServer/Response.pm
Criterion Covered Total %
statement 40 53 75.4
branch 9 14 64.2
condition 5 11 45.4
subroutine 14 20 70.0
pod 10 10 100.0
total 78 108 72.2


line stmt bran cond sub pod time code
1             package WebService::BitbucketServer::Response;
2             # ABSTRACT: A response object for Bitbucket Server REST APIs
3              
4              
5 4     4   23 use warnings;
  4         8  
  4         119  
6 4     4   21 use strict;
  4         9  
  4         144  
7              
8             our $VERSION = '0.602'; # VERSION
9              
10 4     4   785 use Clone qw(clone);
  4         6691  
  4         217  
11 4     4   27 use Types::Standard qw(HashRef Object);
  4         7  
  4         19  
12              
13 4     4   3210 use Moo;
  4         30594  
  4         17  
14 4     4   5376 use namespace::clean;
  4         26685  
  4         24  
15              
16 0     0   0 sub _croak { require Carp; Carp::croak(@_) }
  0         0  
17 0     0   0 sub _usage { _croak("Usage: @_\n") }
18              
19              
20             has context => (
21             is => 'ro',
22             isa => Object,
23             required => 1,
24             );
25              
26              
27             has request_args => (
28             is => 'ro',
29             isa => HashRef,
30             required => 1,
31             );
32              
33              
34             has raw => (
35             is => 'ro',
36             isa => HashRef,
37             required => 1,
38             );
39              
40              
41             has decoded_content => (
42             is => 'lazy',
43             );
44              
45             sub _build_decoded_content {
46 2     2   17 my $self = shift;
47 2         9 my $body = $self->raw->{content};
48              
49 2 50 50     17 if (($self->raw->{headers}{'content-type'} || '') =~ /json/) {
50 2         30 $body = $self->json->decode($body);
51             }
52              
53 2         64 return $body;
54             }
55              
56              
57             has json => (
58             is => 'lazy',
59             isa => Object,
60             default => sub {
61             shift->context->json || do {
62             require JSON;
63             JSON->new->utf8(1);
64             };
65             },
66             );
67              
68              
69 4     4 1 6423 sub is_success { shift->raw->{success} }
70 2     2 1 342 sub status { shift->raw->{status} }
71              
72              
73             sub error {
74 2     2 1 5 my $self = shift;
75              
76 2 50       5 return if $self->is_success;
77              
78 0         0 return $self->decoded_content;
79             }
80              
81              
82             sub data {
83 2     2 1 1689 my $self = shift;
84              
85 2 100       6 return $self->decoded_content->{values} if $self->is_paged;
86 1         22 return $self->decoded_content;
87             }
88              
89 0     0 1 0 sub info { shift->data(@_) }
90 0     0 1 0 sub value { shift->data(@_) }
91 0     0 1 0 sub values { shift->data(@_) }
92              
93              
94 4     4 1 88 sub is_paged { !!shift->page_info }
95              
96              
97             has page_info => (
98             is => 'lazy',
99             );
100              
101             sub _build_page_info {
102 2     2   19 my $self = shift;
103              
104 2         31 my $content = $self->decoded_content;
105              
106 2         9 my @envelope_keys = qw(isLastPage limit size start values);
107             return if $self->request_args->{method} ne 'GET' ||
108 2 100 33     21 ref($content) ne 'HASH' || @envelope_keys != grep { exists $content->{$_} } @envelope_keys;
  10   66     28  
109              
110             return {
111             filter => $content->{filter},
112             is_last_page => $content->{isLastPage},
113             limit => $content->{limit},
114             next_page_start => $content->{nextPageStart},
115             size => $content->{size},
116             start => $content->{start},
117 1         10 };
118             }
119              
120              
121             sub next {
122 2     2 1 1984 my $self = shift;
123              
124 2 100       41 return if not my $page_info = $self->page_info;
125              
126 1 50 33     12 return if $page_info->{is_last_page} || !$page_info->{next_page_start};
127              
128 0           my $args = clone($self->request_args);
129 0           $args->{data}{start} = $page_info->{next_page_start};
130              
131 0           return $self->context->call($args);
132             }
133              
134              
135             sub wrap {
136 0     0 1   my $self = shift;
137 0 0         my $field = shift or _usage(qw{$response->wrap($field)});
138              
139             return __PACKAGE__->new(
140             context => $self->context,
141             request_args => $self->request_args,
142             raw => $self->raw,
143 0           decoded_content => $self->data->{$field},
144             json => $self->json,
145             );
146             }
147              
148             1;
149              
150             __END__