blib/lib/HealthCheck/Diagnostic/RemoteHealth.pm

filter: Statementstmt 97.1% 34 / 35 Branchbran 83.3% 5 / 6 Conditioncond 41.6% 5 / 12 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 100% 8 / 8 Podpod 100% 3 / 3 totaltotal 80.0% 56 / 70 CC 9 SCAR 13.8
CC
9
Cov
83%
new
8.1
run
3.0
check_content
3.0
CRAP 4.0
↑ Summary
1 package HealthCheck::Diagnostic::RemoteHealth;
2 2 ▶use parent 'HealthCheck::Diagnostic::WebRequest';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # ABSTRACT: Get results from an HTTP HealthCheck
9 2 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 our $VERSION = 'v0.1.2'; # VERSION
11  
12 1 ▶sub new {
Subroutine new: called CC=5 SCAR=20.9
Pod documented
13 1 my ($class, @params) = @_;
14  
15 my %params = @params == 1 && ( ref $params[0] || '' ) eq 'HASH'
16 0 ▶ ? %{ $params[0] } : @params;
Branch: 1 branch
BranchTrueFalseTotal
@params == 1 && (ref $params[0] || '') eq 'HASH' ? : 0 1 1
Condition20%1/5
@params == 1 and (ref $params[0] || "") eq "HASH"
!ll&&!rl&&r
100
ref $params[0] || ''
l!l
00
MC/DC0%0/2
@params == 1 and (ref $params[0] || "") eq "HASH"
@params == 1 ref $params[0]
Truth table33%1/3
A and B
A= @params == 1B= ref $params[0]
ABresult
0X0
100
111
17  
18 # Allows 200 and 503 codes by default.
19 1 ▶ $params{status_code} //= '200, 503';
Condition50%1/2
$params{"status_code"} //= "200, 503"
l!l
01
MC/DC0%0/1
$params{"status_code"} //= "200, 503"
$params{"status_code"}
Truth table50%1/2
A
A= $params{"status_code"}
Aresult
11
00
20  
21 1 return $class->SUPER::new(
22 id => 'remotehealth',
23 label => 'RemoteHealth',
24 %params,
25 );
26 }
27  
28 6 ▶sub run {
Subroutine run: called CC=3 SCAR=11.0
Pod documented
29 6 my ($self, %params) = @_;
30 6 my $result = $self->next::method(%params);
31  
32 # Throws away the HTTP status check if OK,
33 # since it's implied to be successful
34 # if it retrieves the encoded JSON object.
35 6 ▶ if (($result->{results}->[0]->{status} || '') eq 'OK' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($result->{"results"}[0]{"status"} || "") eq "OK") 4 2 6
Condition50%1/2
$result->{'results'}[0]{'status'} || ''
l!l
60
MC/DC0%0/1
$result->{'results'}[0]{'status'} || ''
$result->{'results'}[0]{'status'}
Truth table50%1/2
A
A= $result->{'results'}[0]{'status'}
Aresult
11
00
36 4 shift @{ $result->{results} };
37 # info key is removed since it is redundant with the result-level info keys
38 4 return { results => $result->{results} };
39 }
40 2 return $result;
41 }
42  
43 # Checking for content regex from JSON seems unnecessary, so this has been
44 # repurposed to return the decoded JSON object.
45 4 ▶sub check_content {
Subroutine check_content: called CC=3 SCAR=11.0
Pod documented
46 4 my ($self, $response) = @_;
47  
48 4 local $@;
49 4 my $remote_result = eval { decode_json($response->content) };
50 return {
51 4 ▶ status => 'CRITICAL',
Branch: 1 branch
BranchTrueFalseTotal
if $@ or ref $remote_result ne "HASH" 1 3 4
Condition66%2/3
$@ or ref $remote_result ne "HASH"
l!l&&r!l&&!r
103
MC/DC50%1/2
$@ or ref $remote_result ne "HASH"
$@ ref $remote_result ne "HASH"
Truth table66%2/3
A or B
A= $@B= ref $remote_result ne "HASH"
ABresult
1X1
011
000
52 info => 'Could not decode JSON.',
53 data => $response->content,
54 } if $@ or ref($remote_result) ne 'HASH';
55  
56 3 return $remote_result;
57 }
58  
59 2 1;
60  
61 __END__
↑ Summary