Client/Response.pm

filter: Statementstmt 100% 52 / 52 Branchbran 91.6% 11 / 12 Conditioncond 71.4% 5 / 7 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 13 / 13 Podpod 100% 6 / 6 totaltotal 94.6% 89 / 94 CC 11 SCAR 11.0
CC
11
Cov
96%
set_result
6.0
new
4.0
result
2.0
CRAP 3.0
« Client/API.pm ↑ Summary Client/Base.pm »
1 package Net::OpenStack::Client::Response;
2 8 $Net::OpenStack::Client::Response::VERSION = '0.1.4';
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 8 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 8 ▶use Net::OpenStack::Client::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 8 our @EXPORT = qw(mkresponse);
11  
12 8 ▶use overload bool => '_boolean';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 8 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 8 Readonly my $RESULT_PATH => '/';
17  
18 =head1 NAME
19  
20 Net::OpenStack::Client::Response is an response class for Net::OpenStack.
21  
22 Boolean logic is overloaded using C<_boolean> method (as inverse of C<is_error>).
23  
24 =head2 Public methods
25  
26 =over
27  
28 =item mkresponse
29  
30 A C<Net::OpenStack::Client::Response> factory
31  
32 =cut
33  
34 sub mkresponse
35 58 ▶{
Subroutine mkresponse: called CC=1 SCAR=0.0
Pod documented
36 58 return Net::OpenStack::Client::Response->new(@_);
37 }
38  
39  
40 =item new
41  
42 Create new response instance.
43  
44 Options
45  
46 =over
47  
48 =item data: (first) response content, possibly decoded
49  
50 =item headers: hashref with reponse headers
51  
52 =item error: an error (passed to C<mkerror>).
53  
54 =item result_path: passed to C<set_result> to set the result attribute.
55  
56 =back
57  
58 =cut
59  
60 sub new
61 59 ▶{
Subroutine new: called CC=4 SCAR=14.0
Pod documented
62 59 my ($this, %opts) = @_;
63 59 ▶ my $class = ref($this) || $this;
Condition33%1/3
ref $this || $this
l!l&&r!l&&!r
0590
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table33%1/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
64 my $self = {
65 data => $opts{data} || {},
66 headers => $opts{headers} || {},
67 59 ▶ };
Condition100%4/4
$opts{'data'} || {}
l!l
563
$opts{'headers'} || {}
l!l
554
MC/DC100%1/1
$opts{'data'} || {}
$opts{'data'}
MC/DC100%1/1
$opts{'headers'} || {}
$opts{'headers'}
Truth table100%2/2
A
A= $opts{'data'}
Aresult
11
00
Truth table100%2/2
A
A= $opts{'headers'}
Aresult
11
00
68 59 bless $self, $class;
69  
70 # First error
71 59 $self->set_error($opts{error});
72  
73 # Then result
74 59 $self->set_result($opts{result_path});
75  
76 59 return $self;
77 };
78  
79 =item set_error
80  
81 Set and return the error attribute using C<mkerror>.
82  
83 =cut
84  
85 sub set_error
86 61 ▶{
Subroutine set_error: called CC=1 SCAR=0.0
Pod documented
87 61 my $self = shift;
88 61 $self->{error} = mkerror(@_);
89 61 return $self->{error};
90 }
91  
92 =item set_result
93  
94 Set and return the result attribute based on the C<result_path>.
95  
96 The C<result_path> is either
97  
98 =over
99  
100 =item (absolute, starting with C</>) path-like string, indicating which subtree of the answer
101 should be set as result attribute (default C</>).
102  
103 =item anything else is considered a header (from the response headers).
104  
105 =back
106  
107 =cut
108  
109 sub set_result
110 74 ▶{
Subroutine set_result: called CC=6 SCAR=17.9
Pod documented
111 74 my ($self, $result_path) = @_;
112  
113 74 my $res;
114  
115 74 ▶ if (! $self->is_error()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_error) 70 4 74
116 70 ▶ $result_path = $RESULT_PATH if ! defined($result_path);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $result_path 31 39 70
117  
118 70 $res = $self->{data};
119  
120 70 ▶ if ($result_path =~ m#^/#) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result_path =~ m[^/]) { } 65 5 70
121 # remove any "empty" paths
122 65 foreach my $subpath (grep {$_} split('/', $result_path)) {
123 38 ▶ $res = $res->{$subpath} if (defined($res));
Branch: 1 branch
BranchTrueFalseTotal
if defined $res 38 0 38
124 };
125 } else {
126 # a header
127 5 $res = $self->{headers}->{$result_path};
128 }
129 };
130  
131 74 $self->{result} = $res;
132  
133 74 return $self->{result};
134 };
135  
136 =item result
137  
138 Return the result attribute.
139  
140 If C<result_path> is passed (and defined),
141 (re)set the result attribute first.
142 (The default result path cannot be (re)set this way.
143 Use C<set_result> method for that).
144  
145 =cut
146  
147 sub result
148 37 ▶{
Subroutine result: called CC=2 SCAR=6.9
Pod documented
149 37 my ($self, $result_path) = @_;
150  
151 37 ▶ $self->set_result($result_path) if defined($result_path);
Branch: 1 branch
BranchTrueFalseTotal
if defined $result_path 10 27 37
152  
153 37 return $self->{result};
154 }
155  
156 =item is_error
157  
158 Test if this is an error or not (based on error attribute).
159  
160 =cut
161  
162 sub is_error
163 133 ▶{
Subroutine is_error: called CC=2 SCAR=6.9
Pod documented
164 133 my $self = shift;
165 133 ▶ return $self->{error} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'error'} ? : 10 123 133
166 }
167  
168 # Overloaded boolean, inverse of is_error
169 sub _boolean
170 54 ▶{
Subroutine _boolean: called CC=1 SCAR=0.0
171 54 my $self = shift;
172 54 return ! $self->is_error();
173 }
174  
175 =pod
176  
177 =back
178  
179 =cut
180  
181 8 1;
« Client/API.pm ↑ Summary Client/Base.pm »