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