RestApi/Utils.pm

filter: Statementstmt 97.2% 142 / 146 Branchbran 90.7% 49 / 54 Conditioncond 57.6% 15 / 26 MC/DCmcdc 33.3% 5 / 15 Subroutinesub 96.6% 29 / 30 Podpod 0.0% 0 / 12 totaltotal 84.8% 240 / 283 CC 37 SCAR 15.2
CC
37
Cov
91%
resolve_config_file_path
10.0
paginate_api
8.2
flatten_range
6.1
CRAP 4.6
« RestApi/SheetsApi4/Request/Spreadsheet/Worksheet/Range.pm ↑ Summary RestApi.pm »
1 package Google::RestApi::Utils;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 our $VERSION = '2.2.5';
7  
8 2 ▶use feature 'state';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use File::Spec::Functions qw( catfile );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use File::Basename qw( dirname );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Hash::Merge ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Log::Log4perl qw( :easy );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Type::Params qw( signature );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use Types::Standard qw( Str StrMatch Int CodeRef HashRef HasMethods Any slurpy );
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use YAML::Any qw( Dump LoadFile );
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 2 ▶use Google::RestApi::Types qw( ReadableFile );
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 2 ▶no autovivification;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 2 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 our @EXPORT_OK = qw(
26 named_extra
27 merge_config_file resolve_config_file_path
28 flatten_range
29 bool
30 dims_any dims_all
31 cl_black cl_white
32 strip
33 paginate_api
34 paginated_list
35 );
36 2 our %EXPORT_TAGS = (all => [ @EXPORT_OK ]);
37  
38 # used by validation with type::params.
39 # similar to allow_extra in params::validate, simply returns the
40 # extra key/value pairs we aren't interested in in the checked
41 # argument hash.
42 1320 ▶sub named_extra {
Subroutine named_extra: called CC=4 SCAR=13.9
Pod undocumented
43 # v2 signature returns blessed hashref; dereference if needed.
44 1320 ▶ my @args = @_ == 1 && ref $_[0] ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 && ref $_[0] ? : 1318 2 1320
Condition66%2/3
@_ == 1 and ref $_[0]
!ll&&!rl&&r
201318
MC/DC50%1/2
@_ == 1 and ref $_[0]
@_ == 1 ref $_[0]
Truth table66%2/3
A and B
A= @_ == 1B= ref $_[0]
ABresult
0X0
100
111
45 1320 state $check = signature(
46 bless => !!0,
47 named => [
48 _extra_ => HashRef,
49 validated => slurpy HashRef,
50 ],
51 );
52 1320 my $p = $check->(@args);
53 1319 my $extra = delete $p->{_extra_};
54  
55 1319 my %p;
56 1319 ▶ %p = %{ $p->{validated} } if $p->{validated}; # these are validated by the caller.
Branch: 1 branch
BranchTrueFalseTotal
if $$p{"validated"} 1319 0 1319
57 1319 @p{ keys %$extra } = values %$extra; # stuff back the ones the caller wasn't interested in.
58 1319 return \%p;
59 }
60  
61 397 ▶sub merge_config_file {
Subroutine merge_config_file: called CC=4 SCAR=13.9
Pod undocumented
62 397 state $check = signature(
63 bless => !!0,
64 named => [
65 config_file => ReadableFile, { optional => 1 },
66 _extra_ => slurpy HashRef,
67 ],
68 );
69 397 my $passed_config = named_extra($check->(@_));
70  
71 395 my $config_file = $passed_config->{config_file};
72 395 ▶ return $passed_config if !$config_file;
Branch: 1 branch
BranchTrueFalseTotal
unless $config_file 147 248 395
73  
74 248 my $config_from_file = eval { LoadFile($config_file); };
75 248 ▶ LOGDIE "Unable to load config file '$config_file': $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 248 248
76  
77 # Support an optional 'google_restapi' top-level key so the config file can
78 # be shared with other apps. Use that section if present, else use the root.
79 248 ▶ $config_from_file = $config_from_file->{google_restapi} // $config_from_file;
Condition66%2/3
$$config_from_file{"google_restapi"} // $config_from_file
l!l&&r!l&&!r
22460
MC/DC0%0/2
$$config_from_file{"google_restapi"} // $config_from_file
$$config_from_file{"google_restapi"} $config_from_file
Truth table66%2/3
A // B
A= $$config_from_file{"google_restapi"}B= $config_from_file
ABresult
1X1
011
000
80  
81 # left_precedence, the passed config wins over anything in the file.
82 # can't merge coderefs, error comes from Storable buried deep in hash::merge.
83 248 my $merged_config = Hash::Merge::merge($passed_config, $config_from_file);
84 248 TRACE("Config used:\n". Dump($merged_config));
85  
86 248 return $merged_config;
87 }
88  
89 # a standard way to store file names in a config and resolve them
90 # to a full path. can be used in Auth configs, possibly others.
91 # see sub RestApi::auth for more.
92 541 ▶sub resolve_config_file_path {
Subroutine resolve_config_file_path: called CC=10 SCAR=23.0
Pod undocumented
93 541 state $check = signature(positional => [HashRef, Str]);
94 541 my ($config, $file_key) = $check->(@_);
95  
96 541 ▶ my $config_file = $config->{$file_key} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $config_file = $$config{$file_key} or return 149 392 541
97 149 ▶ return $config_file if -f $config_file;
Branch: 1 branch
BranchTrueFalseTotal
if -f $config_file 9 140 149
98  
99 140 my $full_file_path;
100 140 ▶ if ($file_key ne 'config_file' && $config->{config_file}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file_key ne "config_file" and $$config{"config_file"}) 3 137 140
Condition66%2/3
$file_key ne "config_file" and $$config{"config_file"}
!ll&&!rl&&r
01373
MC/DC50%1/2
$file_key ne "config_file" and $$config{"config_file"}
$file_key ne "config_file" $$config{"config_file"}
Truth table66%2/3
A and B
A= $file_key ne "config_file"B= $$config{"config_file"}
ABresult
0X0
100
111
101 3 my $dir = dirname($config->{config_file});
102 3 my $path = catfile($dir, $config_file);
103 3 ▶ $full_file_path = $path if -f $path
Branch: 1 branch
BranchTrueFalseTotal
if -f $path 1 2 3
104 }
105  
106 140 ▶ if (!$full_file_path) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($full_file_path) 139 1 140
107 139 my $dir = $config->{config_dir};
108 139 ▶ if ($dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir) 136 3 139
109 136 my $path = catfile($dir, $config_file);
110 136 ▶ $full_file_path = $path if -f $path
Branch: 1 branch
BranchTrueFalseTotal
if -f $path 136 0 136
111 }
112 }
113
114 140 ▶ LOGDIE("Unable to resolve config file '$file_key => $config_file' to a full file path")
Branch: 1 branch
BranchTrueFalseTotal
unless $full_file_path 3 137 140
115 if !$full_file_path;
116  
117 # action at a distance, but is convenient to stuff the real file name in the config here.
118 137 $config->{$file_key} = $full_file_path;
119
120 137 return $full_file_path;
121 }
122  
123 # these are just used for debug message just above
124 # to display the original range in a pretty format.
125 900 ▶sub flatten_range {
Subroutine flatten_range: called CC=6 SCAR=18.0
Pod undocumented
126 900 my $range = shift;
127 900 ▶ $range = $range->range_to_hash() if blessed($range);
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($range) 92 808 900
128 900 ▶ return 'False' if !$range;
Branch: 1 branch
BranchTrueFalseTotal
unless $range 43 857 900
129 857 ▶ return $range if !ref($range);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $range 606 251 857
130 251 ▶ return _flatten_range_hash($range) if ref($range) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $range eq "HASH" 150 101 251
131 101 ▶ return _flatten_range_array($range) if ref($range) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $range eq "ARRAY" 101 0 101
132 0 LOGDIE("Unable to flatten: " . ref($range));
133 }
134  
135 150 ▶sub _flatten_range_hash {
Subroutine _flatten_range_hash: called CC=1 SCAR=0.0
136 150 my $range = shift;
137 150 my @flat = map { "$_ => " . flatten_range($range->{$_}); } sort keys %$range;
138 150 my $flat = join(', ', @flat);
139 150 return "{ $flat }";
140 }
141  
142 101 ▶sub _flatten_range_array {
Subroutine _flatten_range_array: called CC=1 SCAR=0.0
143 101 my $range = shift;
144 101 my @flat = map { flatten_range($_); } @$range;
145 101 my $flat = join(', ', @flat);
146 101 return "[ $flat ]";
147 }
148  
149 # changes perl boolean to json boolean.
150 38 ▶sub bool {
Subroutine bool: called CC=4 SCAR=13.9
Pod undocumented
151 38 my $bool = shift;
152 38 ▶ return 'true' if !defined $bool; # bold() should turn on bold.
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bool 13 25 38
153 25 ▶ return 'false' if $bool =~ qr/^false$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $bool =~ qr/^false$/i 2 23 25
154 23 ▶ return $bool ? 'true' : 'false'; # converts bold(0) to 'false'.
Branch: 1 branch
BranchTrueFalseTotal
$bool ? : 12 11 23
155 }
156  
157 269 ▶sub dims_any {
Subroutine dims_any: called CC=2 SCAR=6.9
Pod undocumented
158 269 state $check = signature(positional => [StrMatch[qr/^(col|row)/i]]);
159 269 my ($dims) = $check->(@_);
160 268 ▶ return $dims =~ /^col/i ? "COLUMNS" : "ROWS";
Branch: 1 branch
BranchTrueFalseTotal
$dims =~ /^col/i ? : 50 218 268
161 }
162  
163 3 ▶sub dims_all {
Subroutine dims_all: called CC=2 SCAR=6.9
Pod undocumented
164 3 my $dims = eval { dims_any(@_); };
165 3 ▶ return $dims if $dims;
Branch: 1 branch
BranchTrueFalseTotal
if $dims 2 1 3
166 1 state $check = signature(positional => [StrMatch[qr/^all/i]]);
167 1 ($dims) = $check->(@_);
168 1 return "ALL";
169 }
170  
171 5 ▶sub cl_black { { red => 0, blue => 0, green => 0, alpha => 1 }; }
Subroutine cl_black: called CC=1 SCAR=0.0
Pod undocumented
172 5 ▶sub cl_white { { red => 1, blue => 1, green => 1, alpha => 1 }; }
Subroutine cl_white: called CC=1 SCAR=0.0
Pod undocumented
173  
174 0 ▶sub strip {
Subroutine strip: not called CC=2 SCAR=17.9
Pod undocumented
175 0 ▶ my $p = shift // '';
Condition0%0/2
shift() // ""
l!l
00
MC/DC0%0/1
shift() // ""
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
176 0 $p =~ s/^\s+|\s+$//g;
177 0 return $p;
178 }
179  
180 52 ▶sub paginate_api {
Subroutine paginate_api: called CC=8 SCAR=21.1
Pod undocumented
181 52 state $check = signature(
182 bless => !!0,
183 named => [
184 api_call => CodeRef,
185 result_key => Str,
186 max_pages => Int, { default => 0 },
187 page_callback => CodeRef, { optional => 1 },
188 ],
189 );
190 52 my $p = $check->(@_);
191  
192 52 my @list;
193 my $next_page_token;
194 52 my $page = 0;
195 52 my $keep_going = 1;
196 do {
197 53 my $result = $p->{api_call}->($next_page_token);
198 53 my $items = $result->{ $p->{result_key} };
199 53 ▶ push(@list, $items->@*) if $items;
Branch: 1 branch
BranchTrueFalseTotal
if $items 53 0 53
200 53 $next_page_token = $result->{nextPageToken};
201 53 $page++;
202 53 ▶ if ($p->{page_callback} && $items) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$p{"page_callback"} and $items) 7 46 53
Condition66%2/3
$$p{"page_callback"} and $items
!ll&&!rl&&r
4607
MC/DC50%1/2
$$p{"page_callback"} and $items
$$p{"page_callback"} $items
Truth table66%2/3
A and B
A= $$p{"page_callback"}B= $items
ABresult
0X0
100
111
203 7 $keep_going = $p->{page_callback}->($result);
204 }
205 52 ▶ } until !$keep_going || !$next_page_token || ($p->{max_pages} > 0 && $page >= $p->{max_pages});
Branch: 1 branch
BranchTrueFalseTotal
(not $keep_going && $next_page_token or $$p{"max_pages"} > 0 and $page >= $$p{"max_pages"}) or do { do { my $result = $$p{"api_call"}($next_page_token); my $items = $$result{$$p{"result_key"}}; push @list, @$items if $items; $next_page_token = $$result{"nextPageToken"}; ++$page; if ($$p{"page_callback"} and $items) { $keep_going = $$p{"page_callback"}($result); } } } 52 1 53
Condition66%6/9
not $keep_going && $next_page_token or $$p{"max_pages"} > 0 and $page >= $$p{"max_pages"}
l!l&&r!l&&!r
5201
$keep_going && $next_page_token
!ll&&!rl&&r
1511
$$p{"max_pages"} > 0 and $page >= $$p{"max_pages"}
!ll&&!rl&&r
100
MC/DC50%2/4
not $keep_going && $next_page_token or $$p{"max_pages"} > 0 and $page >= $$p{"max_pages"}
$keep_going $next_page_token $$p{"max_pages"} > 0 $page >= $$p{"max_pages"}
Truth table60%3/5
not(A && B) or C and D
A= $keep_goingB= $next_page_tokenC= $$p{"max_pages"} > 0D= $page >= $$p{"max_pages"}
ABCDresult
0XXX1
10XX1
11111
110X0
11100
206  
207 52 return @list;
208 }
209  
210 52 ▶sub paginated_list {
Subroutine paginated_list: called CC=3 SCAR=11.0
Pod undocumented
211 52 state $check = signature(
212 bless => !!0,
213 named => [
214 api => HasMethods['api'],
215 uri => Str,
216 result_key => Str,
217 default_fields => Str,
218 fields_prefix => Str, { default => 'nextPageToken' },
219 max_pages => Int, { default => 0 },
220 page_callback => CodeRef, { optional => 1 },
221 params => HashRef, { default => {} },
222 ],
223 );
224 52 my $p = $check->(@_);
225 52 my $params = $p->{params};
226 52 ▶ $params->{fields} //= $p->{default_fields};
Condition33%1/3
$$params{"fields"} //= $$p{"default_fields"}
l!l&&r!l&&!r
0520
MC/DC0%0/2
$$params{"fields"} //= $$p{"default_fields"}
$$params{"fields"} $$p{"default_fields"}
Truth table33%1/3
A //= B
A= $$params{"fields"}B= $$p{"default_fields"}
ABresult
1X1
011
000
227 52 $params->{fields} = "$p->{fields_prefix}, $params->{fields}";
228 52 my $api = $p->{api};
229 52 my $uri = $p->{uri};
230 return paginate_api(
231 53 ▶ api_call => sub { $params->{pageToken} = $_[0] if $_[0]; $api->api(uri => $uri, params => $params); },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] 1 52 53
232 result_key => $p->{result_key},
233 max_pages => $p->{max_pages},
234 52 ▶ ($p->{page_callback} ? (page_callback => $p->{page_callback}) : ()),
Branch: 1 branch
BranchTrueFalseTotal
$$p{'page_callback'} ? : 6 46 52
235 );
236 }
237  
238 2 1;
« RestApi/SheetsApi4/Request/Spreadsheet/Worksheet/Range.pm ↑ Summary RestApi.pm »