File Coverage

blib/lib/App/Presto/Config.pm
Criterion Covered Total %
statement 66 77 85.7
branch 16 24 66.6
condition 3 5 60.0
subroutine 13 15 86.6
pod 0 8 0.0
total 98 129 75.9


line stmt bran cond sub pod time code
1             package App::Presto::Config;
2             BEGIN {
3 1     1   31513 $App::Presto::Config::AUTHORITY = 'cpan:BPHILLIPS';
4             }
5             {
6             $App::Presto::Config::VERSION = '0.009';
7             }
8              
9             # ABSTRACT: Manage configuration for a given endpoint
10              
11 1     1   1112 use Moo;
  1         24871  
  1         7  
12 1     1   4020 use JSON qw(decode_json encode_json);
  1         23157  
  1         6  
13 1     1   2510 use File::HomeDir;
  1         7608  
  1         77  
14 1     1   8 use File::Path 2.08 qw(make_path);
  1         21  
  1         7618  
15              
16             has endpoint => (
17             is => 'rw',
18             required => 1,
19             trigger => sub { delete shift->{endpoint_dir} }
20             );
21             has config => (
22             is => 'lazy',
23             clearer => 'reload',
24             isa => sub { die "not a HashRef" if ref($_[0]) ne 'HASH'; },
25             );
26             sub _build_config {
27 2     2   9176 my $self = shift;
28 2         84 my $config_file = $self->file('config.json');
29 2 100       84 if (! -e $config_file ) {
30 1         11 warn "creating new config file: $config_file\n";
31 1 50       125 open( my $fh, '>', $config_file ) or die "Unable to open $config_file for writing: $!";
32 1         30 print $fh encode_json({});
33 1         76 close $fh;
34             }
35              
36 2         5 my $config;
37             eval {
38 2         9 local $/;
39 2 50       74 open( my $fh, '<', $config_file ) or die "Unable to open $config_file for reading: $!";
40 2         62 my $json_text = <$fh>;
41 2   50     19 $config = decode_json( $json_text || '{}' );
42 2         37 1;
43 2 50       4 } or do {
44 0         0 warn "Ignoring invalid config file $config_file: $@\n";
45 0         0 $config = {};
46             };
47 2         47 return $config;
48             }
49              
50             has endpoint_dir => (
51             is => 'lazy',
52             );
53              
54             sub _build_endpoint_dir {
55 6     6   7238 my $self = shift;
56 6   66     41 my $root = $ENV{APP_REST_CLI_DIR} || sprintf('%s/.app-presto', File::HomeDir->my_home);
57 6         153 (my $endpoint_dir = lc $self->endpoint) =~ s/\W+/-/g;
58 6         92 my $dir = sprintf( '%s/%s', $root, $endpoint_dir);
59 6 100       172 if(!-d $dir){
60 2         21 warn "creating directory $dir\n";
61 2         1468 make_path($dir);
62             }
63 6         67 return $dir;
64             }
65              
66             sub file {
67 6     6 0 1644 my $self = shift;
68 6         17 (my $file = shift) =~ s{^[\./]+}{}g; # remove leading dots or slashes
69 6         152 return sprintf('%s/%s', $self->endpoint_dir, $file);
70             }
71              
72             sub is_set {
73 0     0 0 0 my $self = shift;
74 0         0 my $key = shift;
75 0         0 return exists $self->config->{$key};
76             }
77             sub set {
78 4     4 0 3054 my $self = shift;
79 4         10 my $key = shift;
80 4         6 my $value = shift;
81 4 100       14 if($key eq 'endpoint'){
82 2         53 return $self->endpoint($value);
83             }
84 2 100       7 if(!defined $value){
85 1         100 delete $self->config->{$key};
86             } else {
87 1         5 $self->config->{$key} = $value;
88             }
89             eval {
90 2         8 $self->write_config;
91 2         7 1;
92 2 50       35 } or do {
93 0         0 warn "unable to persist config: $@\n";
94             };
95 2         7 return;
96             }
97             sub unset {
98 1     1 0 641 my $self = shift;
99 1         5 return $self->set($_[0],undef);
100             }
101              
102             sub get {
103 3     3 0 1665 my $self = shift;
104 3         7 my $key = shift;
105 3 100       16 return $self->endpoint if $key eq 'endpoint';
106 2 50       48 return exists $self->config->{$key} ? $self->config->{$key} : undef;
107             }
108              
109             sub keys {
110 2     2 0 553 my $self = shift;
111 2         4 return keys %{ $self->config };
  2         53  
112             }
113              
114             sub write_config {
115 2     2 0 4 my $self = shift;
116 2         5 my $config_file = $self->file('config.json');
117 2 50       204 open(my $fh, '>', $config_file) or die "Unable to open $config_file for writing: $!";
118 2         52 print $fh encode_json($self->config);
119 2         103 close $fh;
120 2         8 return;
121             }
122              
123             my %DEFAULTS = (
124             binmode => 'utf8',
125             pretty_printer => 'JSON',
126             deserialize_response => 1,
127             );
128             sub init_defaults {
129 0     0 0   my $self = shift;
130 0           foreach my $k(CORE::keys %DEFAULTS){
131 0 0         unless($self->is_set($k)){
132 0           $self->set($k, $DEFAULTS{$k});
133             }
134             }
135 0           return;
136             }
137              
138             1;
139              
140             __END__