blib/lib/IO/Iron/Common.pm

filter: Statementstmt 97.8% 90 / 92 Branchbran 85.7% 12 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 21 / 21 Podpod 100% 4 / 4 totaltotal 96.9% 127 / 131 CC 10 SCAR 7.6
CC
10
Cov
96%
_read_iron_config_file
5.0
get_config
3.0
_read_iron_config_env_vars
3.0
CRAP 2.1
« blib/lib/IO/Iron/IronCache/Policy.pm ↑ Summary blib/lib/IO/Iron/IronCache/Client.pm »
1 package IO::Iron::Common;
2  
3 ## no critic (Documentation::RequirePodAtEnd)
4 ## no critic (Documentation::RequirePodSections)
5 ## no critic (Subroutines::RequireArgUnpacking)
6  
7 8 ▶use 5.010_000;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # Global creator
12 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
13 # No exports
14 }
15  
16 # Global destructor
17 ▶END {
Subroutine END: called CC=1 SCAR=0.0
18 }
19  
20  
21 # ABSTRACT: Common routines for Client Libraries to Iron services IronCache, IronMQ and IronWorker.
22  
23 8 our $VERSION = '0.12_01'; # TRIAL VERSION: generated by DZP::OurPkgVersion
24  
25  
26  
27 8 ▶use Path::Tiny qw{path};
Subroutine BEGIN: called CC=1 SCAR=0.0
28 8 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 8 ▶use Log::Any qw{$log};
Subroutine BEGIN: called CC=1 SCAR=0.0
30 8 require JSON::MaybeXS;
31 8 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
32 8 ▶use File::HomeDir ();
Subroutine BEGIN: called CC=1 SCAR=0.0
33 8 ▶use Hash::Util 0.06 qw{lock_keys unlock_keys};
Subroutine BEGIN: called CC=1 SCAR=0.0
34 8 ▶use Carp::Assert::More;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 8 ▶use English '-no_match_vars';
Subroutine BEGIN: called CC=1 SCAR=0.0
36 8 ▶use Params::Validate qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38  
39  
40  
41 sub IRON_CONFIG_KEYS {
42 return (
43 # Iron.io standard:
44 12 ▶ 'project_id', # The ID of the project to use for requests.
Subroutine IRON_CONFIG_KEYS: called CC=1 SCAR=0.0
Pod documented
45 'token', # The OAuth token that should be used to authenticate requests. Can be found in the HUD.
46 'host', # The domain name the API can be located at. Defaults to a product-specific value, but always using Amazon's cloud.
47 'protocol', # The protocol that will be used to communicate with the API. Defaults to "https", which should be sufficient for 99% of users.
48 'port', # The port to connect to the API through. Defaults to 443, which should be sufficient for 99% of users.
49 'api_version', # The version of the API to connect through. Defaults to the version supported by the client. End-users should probably never change this. Except: IronMQ service upgraded from v2 to v3 in 2015!
50 # IO::Iron additions:
51 'timeout', # REST client timeout (for REST calls accessing IronMQ). N.B. This is not a IronMQ config option! It only configures client this client.
52 'policies', # Filename of JSON file containing policies.
53 );
54 }
55  
56  
57 sub IRON_CLIENT_PARAMETERS {
58 return (
59 3 ▶ IRON_CONFIG_KEYS(),
Subroutine IRON_CLIENT_PARAMETERS: called CC=1 SCAR=0.0
Pod documented
60 'config', # The config file name.
61 'connector', # Reference to a preinitiated connector object.
62 # 'policy', # Reference to a preinitiated policy hash.
63 );
64 }
65  
66  
67 sub get_config { ## no critic (Subroutines::RequireArgUnpacking)
68 my %params = validate(
69 @_, {
70 3 ▶ map { $_ => { type => SCALAR, optional => 1 }, } IRON_CONFIG_KEYS(), ## no critic (ValuesAndExpressions::ProhibitCommaSeparatedStatements)
Subroutine get_config: called CC=3 SCAR=11.0
Pod documented
71 'config' => { type => SCALAR, optional => 1, },
72 }
73 );
74 3 $log->tracef('Entering get_config(%s)', \%params);
75 3 my %config = ( map { $_ => undef } IRON_CONFIG_KEYS() ); ## preset config keys.
76 3 lock_keys(%config, IRON_CONFIG_KEYS());
77 3 _read_iron_config_file(\%config, File::Spec->catfile(File::HomeDir->my_home, '.iron.json')); # Homedir
78 3 _read_iron_config_env_vars(\%config); # Global envs
79 3 _read_iron_config_file(\%config, File::Spec->catfile(File::Spec->curdir(), 'iron.json')); # current dir
80 3 ▶ if(defined $params{'config'}) { # config file specified when creating the class, if given.
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"config"}) 1 2 3
81 _read_iron_config_file(\%config,
82 File::Spec->file_name_is_absolute($params{'config'})
83 1 ▶ ? $params{'config'} : File::Spec->catfile(File::Spec->curdir(), $params{'config'})
Branch: 1 branch
BranchTrueFalseTotal
'File::Spec'->file_name_is_absolute($params{'config'}) ? : 0 1 1
84 );
85 }
86 # The parameters given when the object was created, except 'config'
87 3 my @copy_param_keys = grep { !/^config$/msx} keys %params;
88 3 @config{@copy_param_keys} = @params{@copy_param_keys};
89  
90 3 $log->tracef('Exiting get_config: %s', \%config);
91 3 return \%config;
92 }
93  
94 # Replace the existing values in $config if new environment variables found.
95 # Vars:
96 # $config->{'project_id'} = $ENV{'IRON_PROJECT_ID'}
97 # $config->{'token'} = $ENV{'IRON_TOKEN'}
98 # $config->{'host'} = $ENV{'IRON_HOST'}
99 # $config->{'protocol'} = $ENV{'IRON_PROTOCOL'}
100 # $config->{'port'} = $ENV{'IRON_PORT'}
101 # $config->{'api_version'} = $ENV{'IRON_API_VERSION'}
102 # $config->{'timeout'} = $ENV{'IRON_TIMEOUT'}
103 sub _read_iron_config_env_vars {
104 3 ▶ my ($config) = @_;
Subroutine _read_iron_config_env_vars: called CC=3 SCAR=11.0
105 3 $log->tracef('Entering _read_iron_config_env_vars(%s)', $config);
106 3 foreach my $config_key (keys %{$config}) {
107 24 ▶ if (defined $ENV{'IRON_' . uc $config_key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ENV{'IRON_' . uc($config_key)}) 6 18 24
108 6 $config->{$config_key} = $ENV{'IRON_' . uc $config_key};
109 }
110 }
111 3 $log->tracef('Exiting _read_iron_config_env_vars: %s', $config);
112 3 return $config;
113 }
114  
115  
116 # Try to read the file given as second parameter. (if undef, fail).
117 # If fails, gracefully return 0; if succeed, change configuration (first parameter) and return 1.
118 sub _read_iron_config_file {
119 7 ▶ my ($config, $full_path_name) = @_;
Subroutine _read_iron_config_file: called CC=5 SCAR=16.1
120 7 $log->tracef('Entering _read_iron_config_file(%s, %s)', $full_path_name, $config);
121  
122 7 assert_nonblank( $full_path_name, 'full_path_name is not defined or is blank.' );
123  
124 7 my $read_config;
125 my $rval;
126 7 my $file = path($full_path_name);
127 7 ▶ if ($file->is_file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file->is_file) { } 2 5 7
128 2 $log->tracef('File %s exists', $full_path_name);
129 2 my $file_contents;
130 2 ▶ try { $file_contents = $file->slurp_utf8 };
Subroutine __ANON__: called CC=1 SCAR=0.0
131 2 ▶ if($file_contents) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file_contents) { } 2 0 2
132 2 $log->tracef('Slurped file %s', $full_path_name);
133 2 my $json = JSON::MaybeXS->new(utf8 => 1, pretty => 1);
134 2 $read_config = $json->decode($file_contents);
135 2 foreach my $config_key (keys %{$config}) {
136 16 ▶ if (defined $read_config->{$config_key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $read_config->{$config_key}) 8 8 16
137 8 $config->{$config_key} = $read_config->{$config_key};
138 }
139 }
140 2 $rval = 1;
141 }
142 else {
143 0 $log->debugf('Could not read file %s', $full_path_name);
144 0 $rval = 0;
145 }
146 }
147 else {
148 5 $log->tracef('File %s does not exist', $full_path_name);
149 5 $rval = 0;
150 }
151 7 $log->tracef('Exiting _read_iron_config_file: %s', $config);
152 7 return $rval;
153 }
154  
155  
156 #my $GEN_DELIMS = q{!} . q{$} . q{&} . q{'} . q{(} . q{)}
157 # . q{*} . q{+} . q{,} . q{;} . q{=};
158 #my $SUB_DELIMS = q{:} . q{/} . q{?} . q{#} . q{[} . q{]} . q{@};
159 #my $RESERVED_CHARACTERS = $GEN_DELIMS . $SUB_DELIMS;
160 #my $RFC_3986_RESERVED_CHARACTERS =~ s/(.{1})/\\$1/sg; # Escape every character.
161 sub contains_rfc_3986_res_chars {
162 11 ▶ my @params = validate_pos( @_, { type => SCALAR } );
Subroutine contains_rfc_3986_res_chars: called CC=2 SCAR=6.9
Pod documented
163 ## no critic (ValuesAndExpressions::RequireInterpolationOfMetachars)
164 11 my $rfc_3986_reserved_characters = q{\!\$\&\'\(\)\*\+\,\;\=\:\/\?\#\[\]\@};
165 ## critic (ValuesAndExpressions::RequireInterpolationOfMetachars)
166 11 ▶ return ($params[0] =~ m/[$rfc_3986_reserved_characters]{1,}/msx) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$params[0] =~ /[$rfc_3986_reserved_characters]{1,}/msx ? : 5 6 11
167 }
168  
169 8 1;
170  
171 __END__
« blib/lib/IO/Iron/IronCache/Policy.pm ↑ Summary blib/lib/IO/Iron/IronCache/Client.pm »