Gnats/Response.pm

filter: Statementstmt 86.9% 93 / 107 Branchbran 79.6% 43 / 54 Conditioncond 85.7% 18 / 21 MC/DCmcdc 75.0% 9 / 12 Subroutinesub 94.7% 18 / 19 Podpod 80.0% 8 / 10 totaltotal 84.7% 189 / 223 CC 39 SCAR 19.8
CC
39
Cov
85%
status
30.0
_process_line
9.0
new
7.1
CRAP 7.3
« Gnats/PR.pm ↑ Summary Gnats/Session.pm »
1 package Net::Gnats::Response;
2 40 ▶use v5.10.00;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 40 ▶use strictures;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 40 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
5 40 $Net::Gnats::Response::VERSION = '0.22';
6 }
7 40 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 40 ▶use Net::Gnats qw(verbose_level);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 40 ▶use Net::Gnats::Constants qw(LF CODE_TEXT_READY CODE_PR_READY);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # {
13 # my ($code, $raw, $type);
14  
15 # # internally manage type
16 # my $set_type = sub { $type = shift };
17 # my $set_code = sub {
18 # my $value = shift;
19 # $code = -1 if $value !~ /\d\d\d/;
20 # $code = $value;
21 # };
22 # my $set_raw = sub {
23 # $raw = shift;
24 # };
25 # }
26  
27 =head1 NAME
28  
29 Net::Gnats::Response - A Gnats payload class.
30  
31 =head1 DESCRIPTION
32  
33 For dealing with raw responses and error codes returned by
34 Gnatsd. Enables an easier payload method.
35  
36 =head1 VERSION
37  
38 0.15
39  
40 =head1 SYNOPSIS
41  
42 use Net::Gnats::Reponse;
43  
44 # Compose payload via constructor
45 my $response = Net::Gnats::Response->new({ raw => $data,
46 code => $code});
47  
48 # Compose disparately
49 my $response = Net::Gnats::Response->new;
50 $response->raw($data);
51 $response->code($code);
52  
53  
54 =head1 CONSTRUCTORS
55  
56 There are two types of constructors available.
57  
58 The first enables a 'shell' response which carries children responses.
59 The shell response does not require initialization data.
60  
61 The second enables the capturing a full response. When initializing
62 the response, the code and raw data must be passed to initialization.
63  
64 =head2 new
65  
66 Constructor for the shell Response object.
67  
68 my $r = new(code => $code, raw => $raw)
69  
70 =cut
71  
72 571 ▶sub new {
Subroutine new: called CC=7 SCAR=19.6
Pod documented
73 571 my ($class, %opt) = @_;
74 571 my $c = { trace => 0,
75 delim => ' ',
76 is_finished => 0,
77 content => [],
78 has_more => 0,
79 type => 0,
80 };
81 571 my $self = bless $c, $class;
82  
83 571 ▶ if (%opt) {
Branch: 1 branch
BranchTrueFalseTotal
if (%opt) 571 0 571
84 571 ▶ $c->{type} = $opt{type} if defined $opt{type};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"type"} 569 2 571
85 571 ▶ $c->{code} = $opt{code} if defined $opt{code};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"code"} 0 571 571
86 571 ▶ if (defined $opt{raw} and ref $opt{raw} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt{"raw"} and ref $opt{"raw"} eq "ARRAY") 2 569 571
Condition66%2/3
defined $opt{"raw"} and ref $opt{"raw"} eq "ARRAY"
!ll&&!rl&&r
56902
MC/DC50%1/2
defined $opt{"raw"} and ref $opt{"raw"} eq "ARRAY"
defined $opt{"raw"} ref $opt{"raw"} eq "ARRAY"
Truth table66%2/3
A and B
A= defined $opt{"raw"}B= ref $opt{"raw"} eq "ARRAY"
ABresult
0X0
100
111
87 2 foreach my $r (@{$opt{raw}}) {
88 6 $c->raw($r);
89 }
90 }
91 }
92  
93 571 return $self;
94 }
95  
96 =head1 ACCESSORS
97  
98 The following public accessors are available. All accessors are
99 readonly because the response class expects the raw data to be
100 submitted during instantiation.
101  
102 =head2 raw
103  
104 The readonly raw accessor retrieves raw result data for this particular
105 response. If this is a parent response for a single payload, then it
106 will return an empty anonymous array.
107  
108 my $r = Net::Gnats::Response( code => $code, raw => $raw );
109  
110 =cut
111  
112 6026 ▶sub raw {
Subroutine raw: called CC=5 SCAR=16.1
Pod documented
113 6026 my ( $self, $value ) = @_;
114 6026 _trace('start raw');
115 6026 ▶ $self->{raw} = [] if not defined $self->{raw};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"raw"} 571 5455 6026
116 6026 ▶ push @{ $self->{raw} }, $value if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 6021 5 6026
117 6026 ▶ $self->_process_line($value) if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 6021 5 6026
118 6026 ▶ $self->_check_finish($value) if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 6021 5 6026
119 6026 _trace('end raw');
120 6026 return $self->{raw};
121 }
122  
123 =head2 code
124  
125 The readonly code accessor for the result code.
126  
127 my $r = Net::Gnats::Response( code => $code, raw => $raw );
128 return 1 if $r->code == Net::Gnats::CODE_OK;
129  
130 =cut
131  
132 28526 ▶sub code {
Subroutine code: called CC=2 SCAR=6.9
Pod documented
133 28526 my ( $self ) = @_;
134 28526 ▶ if ( $self->{type} == 1 ) { return 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"type"} == 1) 3 28523 28526
135 28523 return $self->{code};
136 }
137  
138 =head2 inner_responses
139  
140 The readonly accessor for fetching child responses.
141  
142 =cut
143  
144 6 ▶sub inner_responses {
Subroutine inner_responses: called CC=2 SCAR=6.9
Pod documented
145 6 my ( $self ) = @_;
146  
147 6 ▶ $self->{inner_responses} = [] if not defined $self->{inner_responses};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"inner_responses"} 1 5 6
148 6 return $self->{inner_responses};
149 }
150  
151 =head2 is_finished
152  
153 The response has completed processing. Returns 1 if processing has
154 completed, returns 0 otherwise.
155  
156 =cut
157  
158 6583 ▶sub is_finished {
Subroutine is_finished: called CC=1 SCAR=0.0
Pod documented
159 6583 return shift->{is_finished};
160 }
161  
162 5857 ▶sub has_more { return shift->{has_more}; }
Subroutine has_more: called CC=1 SCAR=0.0
Pod undocumented
163  
164 =head2 status
165  
166 Retrieve the overall status of the response. If this response, or all child responses,
167 resulted positively then returns 1. Otherwise, it returns 0.
168  
169 =cut
170  
171 0 ▶sub status {
Subroutine status: not called CC=5 SCAR=34.0
Pod documented
172 0 my ( $self ) = @_;
173 0 ▶ if ( $self->type == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->type == 1) 0 0 0
174 0 foreach ( @{ $self->inner_responses } ) {
175 0 ▶ return 0 if $_->status == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $_->status == 0 0 0 0
176 }
177 0 return 1;
178 }
179 0 ▶ return 0 if $self->code;
Branch: 1 branch
BranchTrueFalseTotal
if $self->code 0 0 0
180 }
181  
182 =head1 METHODS
183  
184 =begin
185  
186 =item as_list
187  
188 Assumes the Gnatsd payload response is a 'list' and parses it as so.
189  
190 Returns: Anonymous array of list items from this response and all
191 children.
192  
193 =cut
194  
195 3858 ▶sub as_list {
Subroutine as_list: called CC=3 SCAR=11.0
Pod documented
196 3858 my ($self) = @_;
197  
198 # get children lists
199 3858 ▶ if ( $self->{type} == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"type"} == 1) 3 3855 3858
200 3 my $result = [];
201 3 for ( @{ $self->inner_responses } ) {
202 3 push @$result, @{ $_->as_list };
203 }
204 3 return $result;
205 }
206  
207 3855 return $self->{content};
208 }
209  
210 =item as_string
211  
212 =back
213  
214 =cut
215  
216 100 ▶sub as_string {
Subroutine as_string: called CC=4 SCAR=13.9
Pod documented
217 100 my ( $self ) = @_;
218 100 ▶ if ( $self->{type} == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"type"} == 1) 3 97 100
219 3 my $result = '';
220 3 my @responses = @{ $self->inner_responses };
221 3 my $last_response = pop @responses;
222 3 for ( @responses ) {
223 1 $result .= $_->as_string . ', ';
224 }
225 3 ▶ $result .= defined $last_response ? $last_response->as_string : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $last_response ? : 2 1 3
226 3 return $result;
227 }
228 97 return join ( $self->{delim}, @{ $self->{content} } );
229 }
230  
231  
232 2 ▶sub add {
Subroutine add: called CC=3 SCAR=14.2
Pod undocumented
233 2 my ( $self, $response ) = @_;
234 2 ▶ if (ref $response eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $response eq 'ARRAY') { } 0 2 2
elsif (not $response->isa('Net::Gnats::Response')) { } 0 2 2
235 0 push @{$self->{inner_responses}}, @{$response};
236 }
237 elsif ( not $response->isa('Net::Gnats::Response') ) {
238 0 warn "you tried adding a response that's not a response! Discarded.";
239 0 return $self;
240 }
241 2 push @{$self->{inner_responses}}, $response;
242 2 return $self;
243 }
244  
245 6021 ▶sub _check_finish {
Subroutine _check_finish: called CC=7 SCAR=19.5
246 6021 my ( $self, $last ) = @_;
247 6021 ▶ if ( $last eq '.' and
Branch: 3 branches
BranchTrueFalseTotal
if ($last eq '.' and $self->code == 301 || $self->code == 300) { } 164 5857 6021
elsif ($self->has_more == 1) { } 3598 2259 5857
elsif ($self->code != 301 and $self->code != 300) { } 406 3540 3946
Condition66%4/6
$last eq "." and $self->code == 301 || $self->code == 300
!ll&&!rl&&r
58570164
$self->code == 301 or $self->code == 300
l!l&&r!l&&!r
150140
MC/DC33%1/3
$last eq "." and $self->code == 301 || $self->code == 300
$last eq "." $self->code == 301 $self->code == 300
Truth table75%3/4
A and B or C
A= $last eq "."B= $self->code == 301C= $self->code == 300
ABCresult
0XX0
1000
11X1
1011
248 ($self->code == CODE_TEXT_READY or
249 $self->code == CODE_PR_READY)) {
250 164 $self->{is_finished} = 1;
251 164 return;
252 }
253 elsif ($self->has_more == 1) {
254 3598 $self->{is_finished} = 0;
255 }
256 ▶ elsif ($self->code != CODE_TEXT_READY and
Condition100%3/3
$self->code != 301 and $self->code != 300
!ll&&!rl&&r
1687166406
MC/DC100%2/2
$self->code != 301 and $self->code != 300
$self->code != 301 $self->code != 300
Truth table100%3/3
A and B
A= $self->code != 301B= $self->code != 300
ABresult
0X0
100
111
257 $self->code != CODE_PR_READY) {
258 406 $self->{is_finished} = 1;
259 }
260 }
261  
262 6021 ▶sub _process_line {
Subroutine _process_line: called CC=9 SCAR=22.0
263 6021 my ( $self, $raw ) = @_;
264 6021 _trace('start _process_line');
265 #list
266 6021 ▶ if ( defined $self->code and
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->code and $self->code == 301 || $self->code == 300) 1853 4168 6021
Condition100%6/6
defined $self->code and $self->code == 301 || $self->code == 300
!ll&&!rl&&r
57035981853
$self->code == 301 or $self->code == 300
l!l&&r!l&&!r
16871663598
MC/DC100%3/3
defined $self->code and $self->code == 301 || $self->code == 300
defined $self->code $self->code == 301 $self->code == 300
Truth table100%4/4
A and B or C
A= defined $self->codeB= $self->code == 301C= $self->code == 300
ABCresult
0XX0
1000
11X1
1011
267 ($self->code == CODE_TEXT_READY or
268 $self->code == CODE_PR_READY)) {
269 1853 ▶ return if $raw eq '.';
Branch: 1 branch
BranchTrueFalseTotal
if $raw eq "." 164 1689 1853
270 1689 push @{ $self->{content} }, $raw;
271 1689 return;
272 }
273  
274 # this is a list and code has already been processed
275 #return if defined $self->code;
276 4168 my @result = $raw =~ /^(\d\d\d)([- ]?)(.*$)/sxm;
277 4168 $self->{code} = $result[0];
278 4168 ▶ $self->{has_more} = 1 if $result[1] eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $result[1] eq "-" 3598 570 4168
279 4168 ▶ $self->{has_more} = 0 if $result[1] eq ' ';
Branch: 1 branch
BranchTrueFalseTotal
if $result[1] eq " " 570 3598 4168
280 4168 ▶ push @{ $self->{content} }, $result[2]
Branch: 1 branch
BranchTrueFalseTotal
unless $self->code == 301 or $self->code == 300 4004 164 4168
Condition100%3/3
$self->code == 301 or $self->code == 300
l!l&&r!l&&!r
150144004
MC/DC100%2/2
$self->code == 301 or $self->code == 300
$self->code == 301 $self->code == 300
Truth table100%3/3
A or B
A= $self->code == 301B= $self->code == 300
ABresult
1X1
011
000
281 unless ( $self->code == CODE_TEXT_READY or
282 $self->code == CODE_PR_READY );
283 4168 return;
284 }
285  
286 18073 ▶sub _trace {
Subroutine _trace: called CC=2 SCAR=9.2
287 18073 my ( $message ) = @_;
288 18073 ▶ return if Net::Gnats->verbose_level() != 3;
Branch: 1 branch
BranchTrueFalseTotal
if "Net::Gnats"->verbose_level != 3 18073 0 18073
289 0 print 'TRACE(Response): [' . $message . ']' . LF;
290 0 return;
291 }
292  
293 40 1;
294  
295  
296 =head1 INCOMPATIBILITIES
297  
298 None.
299  
300 =head1 SUBROUTINES/METHODS
301  
302 =over
303  
304 =head1 BUGS AND LIMITATIONS
305  
306 None.
307  
308 =head1 CONFIGURATION AND ENVIRONMENT
309  
310 None.
311  
312 =head1 DEPENDENCIES
313  
314 None.
315  
316 =head1 DIAGNOSTICS
317  
318 None.
319  
320 =head1 AUTHOR
321  
322 Richard Elberger, riche@cpan.org
323  
324 =head1 LICENSE AND COPYRIGHT
325  
326 License: GPL V3
327  
328 (c) 2014 Richard Elberger
329  
330 =cut
« Gnats/PR.pm ↑ Summary Gnats/Session.pm »