File Coverage

blib/lib/HealthCheck/WebRequests.pm
Criterion Covered Total %
statement 40 41 97.5
branch 7 12 58.3
condition 5 13 38.4
subroutine 9 9 100.0
pod 2 2 100.0
total 63 77 81.8


line stmt bran cond sub pod time code
1             package HealthCheck::WebRequests;
2 2     2   142035 use parent 'HealthCheck';
  2         3  
  2         18  
3              
4             # ABSTRACT: Make HTTP/HTTPS requests to web servers to check connectivity
5 2     2   11727 use version;
  2         4  
  2         11  
6             our $VERSION = 'v1.4.4'; # VERSION
7              
8 2     2   157 use strict;
  2         2  
  2         28  
9 2     2   6 use warnings;
  2         5  
  2         88  
10              
11 2     2   8 use Carp;
  2         3  
  2         88  
12 2     2   491 use HealthCheck::Diagnostic::WebRequest;
  2         8  
  2         53  
13 2     2   18 use Scalar::Util 'blessed';
  2         2  
  2         827  
14              
15             sub new {
16 1     1 1 25 my ($class, @params) = @_;
17              
18             my %params = @params == 1 && ( ref $params[0] || '' ) eq 'HASH'
19 1 50 33     6 ? %{ $params[0] } : @params;
  0         0  
20              
21             my @bad_params = grep {
22 1         3 !/^( checks
  2         10  
23             | content_regex
24             | id
25             | label
26             | no_follow_redirects
27             | options
28             | response_time_threshold
29             | status_code
30             | status_code_eval
31             | runbook
32             | tags
33             | timeout
34             | ua
35             )$/x
36             } keys %params;
37              
38 1 50       3 carp("Invalid parameter: " . join(", ", @bad_params)) if @bad_params;
39              
40 1 50       3 croak "No checks specified!" unless $params{checks};
41              
42 1         4 my %default_params = %params;
43 1         12 delete $default_params{checks};
44 1         3 $params{default_params} = \%default_params;
45              
46 1         8 return $class->SUPER::new(
47             label => 'web_requests',
48             %params,
49             );
50             }
51              
52             sub register {
53 1     1 1 13 my ( $self, @checks ) = @_;
54 1 50 50     9 @checks = @{ $checks[0] } if @checks == 1 && ( ref $checks[0] || '' ) eq 'ARRAY';
  1   33     2  
55              
56 1         2 for my $check (@checks) {
57 3 100 50     9 if ( ( ref $check || '' ) eq 'HASH' ) {
58 2         29 $check = HealthCheck::Diagnostic::WebRequest->new( %{ $self->{default_params} }, %$check );
  2         12  
59             }
60              
61 3 50 33     40 croak "Invalid check. Checks must either be a hashref or HealthCheck::Diagnostic::WebRequest"
62             unless blessed $check
63             and $check->isa("HealthCheck::Diagnostic::WebRequest");
64             }
65              
66 1         18 return $self->SUPER::register(@checks);
67             }
68              
69             1;
70              
71             __END__