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   154846 use strict;
  1         11  
  1         29  
8 1     1   5 use warnings;
  1         1  
  1         25  
9 1     1   5 use English;
  1         2  
  1         9  
10 1     1   1114 use JSON;
  1         12960  
  1         6  
11 1     1   758 use Data::Dumper;
  1         6705  
  1         62  
12              
13 1     1   535 use Moose;
  1         472199  
  1         8  
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         2 my $config;
25 1         46 my $file = $self->LOCATION();
26 1 50 33     72 if ( ( -e $file ) && ( -r $file ) ) {
27              
28 1         3 my $content = do {
29 1         6 local $INPUT_RECORD_SEPARATOR;
30 1         51 open my $fh, '<', $file;
31 1         78 <$fh>;
32             };
33 1         4 eval {
34 1         17 $config = decode_json($content);
35             };
36 1 50 33     12 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         32 $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         34 $self->_config($config);
46             }
47              
48 0     0 1   sub set { shift; die "No set() method defined"; };
  0            
49              
50 1     1   7696 no Moose;
  1         3  
  1         4  
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 JSON 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