File Coverage

blib/lib/Net/Respite/Common.pm
Criterion Covered Total %
statement 20 21 95.2
branch 6 6 100.0
condition 15 24 62.5
subroutine 5 6 83.3
pod 0 3 0.0
total 46 60 76.6


line stmt bran cond sub pod time code
1             package Net::Respite::Common;
2              
3             =pod
4              
5             =head1 NAME
6              
7             Net::Respite::Common - Common methods used internally
8              
9             =head1 SYNOPSIS
10              
11             package CustomModule;
12             use base 'Net::Respite::Common';
13              
14             sub xkey1 { return shift->_configs->{xxx_key} || "unknown" }
15              
16             sub parse1 { return shift->json->decode(shift) }
17              
18             =cut
19              
20 11     11   715682 use strict;
  11         27  
  11         494  
21 11     11   91 use warnings;
  11         24  
  11         5530  
22              
23             sub new {
24 27     27 0 990512 my ($class, $args) = @_;
25 27 100       60 return bless {%{$args || {}}}, $class;
  27         343  
26             }
27              
28             our $js;
29 27   50 27 0 233278 sub json { $js ||= eval { require JSON; JSON->new->utf8->allow_unknown->allow_nonref->convert_blessed->canonical } || die "Could not load JSON: $@" }
      66        
30             our $jp;
31 0   0 0 0 0 sub jsop { $jp ||= eval { require JSON; JSON->new->utf8->allow_unknown->allow_nonref->convert_blessed->canonical->pretty } || die "Could not load JSON: $@" }
      0        
32              
33             our $config;
34             sub _configs {
35 182   66 182   9297 return $config ||= do {
36 8         23 my $c = undef;
37 8         17 eval {
38 8         80 require config;
39 7         17 eval { $c = config->load };
  7         108  
40 7   66     69 $c ||= defined($config::config) && "HASH" eq ref $config::config && $config::config;
      100        
41 7 100 100     26 $c ||= %config::config ? \%config::config : undef;
42             };
43 8 100 100     44 "HASH" eq ref $c or $c = { failed_load => ($@ || "missing config::config hash") };
44 8         40 $c;
45             };
46             }
47              
48             1;