blib/lib/Healthchecks.pm

filter: Statementstmt 37.5% 24 / 64 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 35.2% 6 / 17 Podpod 100% 10 / 10 totaltotal 30.0% 40 / 133 SCAR 50.8
CC
18
Cov
24%
_execute
342.0
get_all_checks
2.0
get_check
2.0
CRAP 160.2
↑ Summary Build »
1 # vim:set sw=4 ts=4 sts=4 ft=perl expandtab:
2 package Healthchecks;
3 # ABSTRACT: interact with Healthchecks API
4 1 ▶use Mojo::Base -base, -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Mojo::JSON qw(decode_json encode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Carp qw(carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 our $VERSION = "0.01";
12  
13 1 has 'url';
14 1 has 'apikey';
15 1 has 'user';
16 1 has 'password';
17 1 has 'proxy';
18 1 ▶has 'ua' => sub { Mojo::UserAgent->new; };
Subroutine __ANON__: called CC=1 SCAR=0.0
19  
20 =encoding utf-8
21  
22 =head1 SYNOPSIS
23  
24 use Healthchecks;
25 my $hc = Healthchecks->new(
26 url => 'http://hc.example.org',
27 apikey => 'secret_healthchecks_API_key',
28 user => 'http_user',
29 password => 'http_password',
30 proxy => {
31 http => 'http://proxy.example.org',
32 https => 'http://proxy.example.org'
33 }
34 );
35  
36 $hc->get_check('uuid_or_unique_key');
37  
38 =head1 DESCRIPTION
39  
40 Client module for L<Healthchecks|https://healthchecks.io/> L<HTTP API|https://healthchecks.io/docs/api/>.
41  
42 =head1 ATTRIBUTES
43  
44 L<Healthchecks> implements the following attributes.
45  
46 =head2 url
47  
48 my $url = $hc->url;
49 $hc = $hc->url('http://hc.example.org');
50  
51 MANDATORY. The Healthchecks URL, no default.
52  
53 =head2 apikey
54  
55 my $apikey = $hc->apikey;
56 $hc = $hc->apikey('secret_etherpad_API_key');
57  
58 MANDATORY. Secret API key, no default
59  
60 =head2 ua
61  
62 my $ua = $hc->ua;
63 $hc = $hc->ua(Mojo::UserAgent->new);
64  
65 OPTIONAL. User agent, default to a Mojo::UserAgent. Please, don't use anything other than a Mojo::Useragent.
66  
67 =head2 user
68  
69 my $user = $hc->user;
70 $hc = $hc->user('bender');
71  
72 OPTIONAL. HTTP user, use it if your Healthchecks is protected by a HTTP authentication, no default.
73  
74 =head2 password
75  
76 my $password = $hc->password;
77 $hc = $hc->password('beer');
78  
79 OPTIONAL. HTTP password, use it if your Healthchecks is protected by a HTTP authentication, no default.
80  
81 =head2 proxy
82  
83 my $proxy = $hc->proxy;
84 $hc = $hc->proxy({
85 http => 'http://proxy.example.org',
86 https => 'http://proxy.example.org'
87 });
88  
89 OPTIONAL. Proxy settings. If set to { detect => 1 }, Healthchecks will check environment variables HTTP_PROXY, http_proxy, HTTPS_PROXY, https_proxy, NO_PROXY and no_proxy for proxy information. No default.
90  
91 =cut
92  
93 sub _execute {
94 0 ▶ my $c = shift;
Subroutine _execute: not called CC=18 SCAR=58.3
95 0 my $args = shift;
96  
97 0 ▶ if (defined $c->proxy) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $c->proxy) 0 0 0
98 0 ▶ if ($c->proxy->{detect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->proxy->{'detect'}) { } 0 0 0
99 0 $c->ua->proxy->detect;
100 } else {
101 0 ▶ $c->ua->proxy->http($c->proxy->{http}) if defined $c->proxy->{http};
Branch: 1 branch
BranchTrueFalseTotal
if defined $c->proxy->{"http"} 0 0 0
102 0 ▶ $c->ua->proxy->http($c->proxy->{https}) if defined $c->proxy->{https};
Branch: 1 branch
BranchTrueFalseTotal
if defined $c->proxy->{"https"} 0 0 0
103 }
104 }
105  
106 0 ▶ my $url = Mojo::URL->new($args->{url} // $c->url);
Condition0%0/3
$args->{"url"} // $c->url
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{"url"} // $c->url
$args->{"url"} $c->url
Truth table0%0/3
A // B
A= $args->{"url"}B= $c->url
ABresult
1X1
011
000
107 0 ▶ $url->userinfo($c->user.':'.$c->password) if defined $c->user && defined $c->password;
Branch: 1 branch
BranchTrueFalseTotal
if defined $c->user and defined $c->password 0 0 0
Condition0%0/3
defined $c->user and defined $c->password
!ll&&!rl&&r
000
MC/DC0%0/2
defined $c->user and defined $c->password
defined $c->user defined $c->password
Truth table0%0/3
A and B
A= defined $c->userB= defined $c->password
ABresult
0X0
100
111
108  
109 0 my $path = $url->path;
110 0 $path =~ s#/$##;
111 0 ▶ $url->path($path.'/api/'.$args->{api}) unless $args->{url};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"url"} 0 0 0
112  
113 0 ▶ $url->query($args->{query} // {});
Condition0%0/2
$args->{"query"} // {}
l!l
00
MC/DC0%0/1
$args->{"query"} // {}
$args->{"query"}
Truth table0%0/2
A
A= $args->{"query"}
Aresult
11
00
114  
115 0 ▶ my $method = $args->{method} // 'get';
Condition0%0/2
$args->{"method"} // "get"
l!l
00
MC/DC0%0/1
$args->{"method"} // "get"
$args->{"method"}
Truth table0%0/2
A
A= $args->{"method"}
Aresult
11
00
116  
117 0 my $res;
118 0 ▶ if (defined $args->{data}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{'data'}) { } 0 0 0
119 0 $res = $c->ua->$method($url => { 'X-Api-Key' => $c->apikey } => json => $args->{data})->result;
120 } else {
121 0 $res = $c->ua->$method($url => { 'X-Api-Key' => $c->apikey })->result;
122 }
123  
124 0 ▶ return $res->is_success if $args->{success};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"success"} 0 0 0
125  
126 0 ▶ if ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
127 # Can’t use $res->json when json is too large
128 0 my $json = decode_json($res->body);
129 0 my $data;
130 0 ▶ if (defined $args->{key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{'key'}) { } 0 0 0
131 0 ▶ $data = (ref($json) eq 'HASH') ? $json->{$args->{key}} : $json;
Branch: 1 branch
BranchTrueFalseTotal
ref $json eq 'HASH' ? : 0 0 0
132 } else {
133 0 $data = $json;
134 }
135  
136 0 ▶ return (wantarray) ? @{$data}: $data if ref($data) eq 'ARRAY';
Branch: 2 branches
BranchTrueFalseTotal
if ref $data eq "ARRAY" 0 0 0
wantarray ? : 0 0 0
137 0 return $data;
138 } else {
139 0 carp Dumper $res->message;
140 0 return undef;
141 }
142 }
143  
144 =head1 METHODS
145  
146 Healthchecks inherits all methods from Mojo::Base and implements the following new ones.
147  
148 =cut
149  
150 #################### subroutine header begin ####################
151  
152 =head3 get_all_checks
153  
154 Usage : $hc->get_all_checks();
155 Purpose : Get all checks
156 Returns : An array of checks belonging to the user, optionally filtered by one or more tags.
157 Argument : None
158 See : https://healthchecks.io/docs/api/#list-checks
159  
160 =cut
161  
162 #################### subroutine header end ####################
163  
164 ▶sub get_all_checks($c) {
Subroutine get_all_checks: not called CC=1 SCAR=6.9
Pod documented
165 0 return $c->_execute({
166 api => 'v1/checks/',
167 key => 'checks'
168 });
169 }
170  
171 #################### subroutine header begin ####################
172  
173 =head3 get_check
174  
175 Usage : $hc->get_check('uuid_or_unique_key');
176 Purpose : Get details of a check
177 Returns : A hash, representation of a single check.
178 Argument : Accepts either check's UUID or the unique_key (a field derived from UUID and returned by API responses when using the read-only API key) as argument.
179 MANDATORY
180 See : https://healthchecks.io/docs/api/#get-check
181  
182 =cut
183  
184 #################### subroutine header end ####################
185  
186 ▶sub get_check($c, $uuid) {
Subroutine get_check: not called CC=1 SCAR=6.9
Pod documented
187 0 return $c->_execute({
188 api => 'v1/checks/'.$uuid
189 });
190 }
191  
192 #################### subroutine header begin ####################
193  
194 =head3 create_check
195  
196 Usage : $hc->({name => 'foobarbaz'});
197 Purpose : Create a check
198 Returns : A hash, representation of a single check.
199 Argument : A hash of the check’s options (see API documentation)
200 OPTIONAL
201 See : https://healthchecks.io/docs/api/#create-check
202  
203 =cut
204  
205 #################### subroutine header end ####################
206  
207 ▶sub create_check($c, $data){
Subroutine create_check: not called CC=1 SCAR=6.9
Pod documented
208 0 return $c->_execute({
209 api => 'v1/checks/',
210 method => 'post',
211 data => $data
212 });
213 }
214  
215 #################### subroutine header begin ####################
216  
217 =head3 update_check
218  
219 Usage : $hc->update_check('uuid', { name => 'quux' });
220 Purpose : Update the configuration of a check
221 Returns : A hash, representation of a single check.
222 Argument : The check's UUID (MANDATORY) and a hash of the check’s options (see API documentation)
223 OPTIONAL
224 See : https://healthchecks.io/docs/api/#update-check
225  
226 =cut
227  
228 #################### subroutine header end ####################
229  
230 ▶sub update_check($c, $uuid, $data){
Subroutine update_check: not called CC=1 SCAR=6.9
Pod documented
231 0 return $c->_execute({
232 api => 'v1/checks/'.$uuid,
233 method => 'post',
234 data => $data
235 });
236 }
237  
238 #################### subroutine header begin ####################
239  
240 =head3 pause_check
241  
242 Usage : $hc->pause_check('uuid');
243 Purpose : Disables monitoring for a check without removing it. The check goes into a "paused" state. You can resume monitoring of the check by pinging it.
244 Returns : A boolean : true if the check is paused, false otherwise.
245 Argument : The check's UUID
246 MANDATORY
247 See : https://healthchecks.io/docs/api/#pause-check
248  
249 =cut
250  
251 #################### subroutine header end ####################
252  
253 ▶sub pause_check($c, $uuid){
Subroutine pause_check: not called CC=1 SCAR=6.9
Pod documented
254 0 return $c->_execute({
255 api => 'v1/checks/'.$uuid.'/pause',
256 method => 'post',
257 key => 'status'
258 }) eq 'paused';
259 }
260  
261 #################### subroutine header begin ####################
262  
263 =head3 delete_check
264  
265 Usage : $hc->delete_check('uuid');
266 Purpose : Permanently deletes the check from the user's account.
267 Returns : A boolean : true if the check has been successfully deleted, false otherwise.
268 Argument : The check's UUID
269 MANDATORY
270 See : https://healthchecks.io/docs/api/#delete-check
271  
272 =cut
273  
274 #################### subroutine header end ####################
275  
276 ▶sub delete_check($c, $uuid){
Subroutine delete_check: not called CC=1 SCAR=6.9
Pod documented
277 0 return $c->_execute({
278 api => 'v1/checks/'.$uuid,
279 method => 'delete',
280 success => 1
281 });
282 }
283  
284 #################### subroutine header begin ####################
285  
286 =head3 get_check_pings
287  
288 Usage : $hc->get_check_pings('uuid');
289 Purpose : Get the pings of a check.
290 Returns : An array of pings this check has received.
291 Argument : The check's UUID
292 MANDATORY
293 See : https://healthchecks.io/docs/api/#list-pings
294  
295 =cut
296  
297 #################### subroutine header end ####################
298  
299 ▶sub get_check_pings($c, $uuid){
Subroutine get_check_pings: not called CC=1 SCAR=6.9
Pod documented
300 0 return $c->_execute({
301 api => 'v1/checks/'.$uuid.'/pings/',
302 key => 'pings'
303 });
304 }
305  
306 #################### subroutine header begin ####################
307  
308 =head3 get_check_flips
309  
310 Usage : $hc->get_check_flips('uuid_or_unique_key', { seconds => 3, start => 1592214380, end => 1592217980});
311 Purpose : Get the "flips" of a check has experienced.
312 Returns : An array of the "flips" the check has experienced. A flip is a change of status (from "down" to "up," or from "up" to "down").
313 Argument : Accepts either check's UUID or the unique_key (a field derived from UUID and returned by API responses when using the read-only API key) as argument.
314 MANDATORY
315 You can specify an optional hash table to add parameters as query string (see API documentation)
316 See : https://healthchecks.io/docs/api/#list-flips
317  
318 =cut
319  
320 #################### subroutine header end ####################
321  
322 ▶sub get_check_flips($c, $uuid, $query){
Subroutine get_check_flips: not called CC=1 SCAR=6.9
Pod documented
323 0 return $c->_execute({
324 api => 'v1/checks/'.$uuid.'/flips/',
325 key => 'flips',
326 query => $query
327 });
328 }
329  
330 #################### subroutine header begin ####################
331  
332 =head3 get_integrations
333  
334 Usage : $hc->get_all_checks();
335 Purpose : Get a list of existing integrations
336 Returns : An array of integrations belonging to the project.
337 Argument : None
338 See : https://healthchecks.io/docs/api/#list-channels
339  
340 =cut
341  
342 #################### subroutine header end ####################
343  
344 ▶sub get_integrations($c){
Subroutine get_integrations: not called CC=1 SCAR=6.9
Pod documented
345 0 return $c->_execute({
346 api => 'api/v1/channels/',
347 key => 'channels'
348 });
349 }
350  
351 #################### subroutine header begin ####################
352  
353 =head3 ping_check
354  
355 Usage : $hc->ping_check('uuid');
356 Purpose : Ping a check
357 Returns : A boolean : true if the check has been successfully pinged, false otherwise.
358 Argument : The check's UUID
359 MANDATORY
360 See : This is not part of the Healthchecks API but a facility offered by this module
361  
362 =cut
363  
364 #################### subroutine header end ####################
365  
366 ▶sub ping_check($c, $uuid){
Subroutine ping_check: not called CC=1 SCAR=6.9
Pod documented
367 0 return $c->_execute({
368 url => $c->_execute({
369 api => 'v1/checks/'.$uuid,
370 key => 'ping_url'
371 }),
372 success => 1
373 });
374 }
375  
376 1 1;
377  
378 __END__
↑ Summary Build »