File Coverage

blib/lib/Connector/Proxy/JSON.pm
Criterion Covered Total %
statement 34 40 85.0
branch 2 4 50.0
condition 3 9 33.3
subroutine 8 9 88.8
pod 1 1 100.0
total 48 63 76.1


line stmt bran cond sub pod time code
1             # Connector::Proxy::JSON
2             #
3             # Proxy class for reading a JSON file
4              
5             package Connector::Proxy::JSON;
6              
7 1     1   128905 use strict;
  1         9  
  1         25  
8 1     1   4 use warnings;
  1         1  
  1         21  
9 1     1   6 use English;
  1         1  
  1         7  
10 1     1   2118 use JSON;
  1         13085  
  1         5  
11 1     1   658 use Data::Dumper;
  1         5702  
  1         60  
12              
13 1     1   448 use Moose;
  1         396875  
  1         7  
14              
15             extends 'Connector::Builtin::Memory';
16              
17             # set Location required (unset by parent class)
18             has '+LOCATION' => ( required => 1 );
19              
20             sub _build_config {
21 1     1   2 my $self = shift;
22              
23             # File not exist or not readable
24 1         1 my $config;
25 1         24 my $file = $self->LOCATION();
26 1 50 33     57 if ( ( -e $file ) && ( -r $file ) ) {
27              
28 1         3 my $content = do {
29 1         5 local $INPUT_RECORD_SEPARATOR;
30 1         33 open my $fh, '<', $file;
31 1         711 <$fh>;
32             };
33 1         5 eval {
34 1         32 $config = JSON->new()->decode($content);
35             };
36 1 50 33     16 if ($@ || !$config || !ref $config) {
      33        
37 0         0 $self->log()->error('Proxy::JSON error parsing content from file '.$file);
38 0         0 $self->log()->debug( Dumper( $@ ) );
39 0         0 return $self->_node_not_exists( $file );
40             }
41 1         31 $self->log()->debug('Proxy::JSON loading configuration from file '.$file);
42             } else {
43 0         0 $self->log()->warn('Proxy::JSON configuration file '.$file.' not found ');
44             }
45 1         32 $self->_config($config);
46             }
47              
48 0     0 1   sub set { shift; die "No set() method defined"; };
  0            
49              
50 1     1   6904 no Moose;
  1         2  
  1         5  
51             __PACKAGE__->meta->make_immutable;
52              
53             1;
54              
55             __END__
56              
57             =head1 Name
58              
59             Connector::Proxy::JSON
60              
61             =head1 Description
62              
63             Reads a json file from the path given as LOCATION and makes its structure
64             available via the accessor methods.
65              
66             If the file could not be loaded this connector returns undef for all requests.
67              
68             B<Note>: Changes made to the YAML file after the connector was first
69             initialized will not be visible as the file is read once on startup and
70             persited into memory.
71              
72             =head1 Parameters
73              
74             =over
75              
76             =item LOCATION
77              
78             The location of the json file.
79              
80             =back