RestApi/SheetsApi4/Spreadsheet.pm

filter: Statementstmt 87.6% 163 / 186 Branchbran 65.9% 29 / 44 Conditioncond 75.0% 15 / 20 MC/DCmcdc 33.3% 4 / 12 Subroutinesub 86.8% 33 / 38 Podpod 80.7% 21 / 26 totaltotal 81.2% 265 / 326 CC 34 SCAR 14.4
CC
34
Cov
83%
delete_all_protected_ranges
20.0
submit_requests
6.0
spreadsheet_id
6.0
CRAP 4.2
« RestApi.pm ↑ Summary RestApi/GmailApi1/Label.pm »
1 package Google::RestApi::SheetsApi4::Spreadsheet;
2  
3 2 our $VERSION = '2.2.5';
4  
5 2 ▶use Google::RestApi::Setup;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use Cache::Memory::Simple ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use aliased 'Google::RestApi::SheetsApi4';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use aliased 'Google::RestApi::SheetsApi4::Worksheet';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use aliased 'Google::RestApi::SheetsApi4::RangeGroup';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use aliased 'Google::RestApi::SheetsApi4::RangeGroup::Tie';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 ▶use parent "Google::RestApi::SheetsApi4::Request::Spreadsheet";
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 45 ▶sub new {
Subroutine new: called CC=5 SCAR=16.1
Pod documented
17 45 my $class = shift;
18  
19 45 my $qr_id = $Google::RestApi::SheetsApi4::Spreadsheet_Id;
20 45 my $qr_uri = $Google::RestApi::SheetsApi4::Spreadsheet_Uri;
21 # pass one of id/name/title/uri and this will work out the others.
22 45 state $check = signature(
23 bless => !!0,
24 named => [
25 sheets_api => HasApi,
26 # https://developers.google.com/sheets/api/guides/concepts
27 id => StrMatch[qr/^$qr_id$/], { optional => 1 },
28 name => Str, { optional => 1 },
29 title => Str, { optional => 1 },
30 uri => StrMatch[qr|^$qr_uri/$qr_id/?|], { optional => 1 },
31 cache_seconds => PositiveOrZeroNum, { default => 5 },
32 ],
33 );
34 45 my $self = $check->(@_);
35  
36 45 $self = bless $self, $class;
37 45 ▶ $self->{name} ||= $self->{title};
Condition66%2/3
$$self{"name"} ||= $$self{"title"}
l!l&&r!l&&!r
5040
MC/DC50%1/2
$$self{"name"} ||= $$self{"title"}
$$self{"name"} $$self{"title"}
Truth table66%2/3
A ||= B
A= $$self{"name"}B= $$self{"title"}
ABresult
1X1
011
000
38 45 delete $self->{title};
39  
40 45 ▶ $self->{id} || $self->{name} || $self->{uri} or LOGDIE "At least one of id, name, or uri must be specified";
Branch: 1 branch
BranchTrueFalseTotal
$$self{"id"} or $$self{"name"} or $$self{"uri"} or LOGDIE("At least one of id, name, or uri must be specified") 44 1 45
Condition100%6/6
$$self{"id"} or $$self{"name"} or $$self{"uri"}
l!l&&r!l&&!r
4041
$$self{"id"} or $$self{"name"}
l!l&&r!l&&!r
3555
MC/DC100%3/3
$$self{"id"} or $$self{"name"} or $$self{"uri"}
$$self{"id"} $$self{"name"} $$self{"uri"}
Truth table100%4/4
A or B or C
A= $$self{"id"}B= $$self{"name"}C= $$self{"uri"}
ABCresult
1XX1
01X1
0011
0000
41  
42 44 return $self;
43 }
44  
45 # take the passed uri from worksheet/range/rangegroup etc, and tack on the spreadsheet id,
46 # then pass it up to G::R::SheetsApi4 which will tack on the endpoint.
47 237 ▶sub api {
Subroutine api: called CC=1 SCAR=0.0
Pod documented
48 237 my $self = shift;
49 237 state $check = signature(
50 bless => !!0,
51 named => [
52 uri => Str, { default => '' },
53 _extra_ => slurpy HashRef, # we'll just pass the params/content etc up for processing.
54 ],
55 );
56 237 my $p = named_extra($check->(@_));
57 237 $p->{uri} = $self->spreadsheet_id() . $p->{uri};
58 237 return $self->sheets_api()->api(%$p);
59 }
60  
61 # work out the id from the uri or the name/title.
62 303 ▶sub spreadsheet_id {
Subroutine spreadsheet_id: called CC=6 SCAR=18.0
Pod documented
63 303 my $self = shift;
64  
65 303 ▶ if (!$self->{id}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($$self{"id"}) 5 298 303
66 5 ▶ if ($self->{uri}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$self{'uri'}) { } 2 3 5
67 2 my $qr_id = $Google::RestApi::SheetsApi4::Spreadsheet_Id;
68 2 my $qr_uri = $Google::RestApi::SheetsApi4::Spreadsheet_Uri;
69 2 ($self->{id}) = $self->{uri} =~ m|^$qr_uri/($qr_id)|; # can end with '/edit'
70 2 ▶ LOGDIE "Unable to extract a sheet id from uri" if !$self->{id};
Branch: 1 branch
BranchTrueFalseTotal
unless $$self{"id"} 0 2 2
71 2 DEBUG("Got sheet ID '$self->{id}' via URI '$self->{uri}'.");
72 } else {
73 3 my @spreadsheets = $self->sheets_api()->spreadsheets(name => $self->{name});
74 3 ▶ LOGDIE "Sheet '$self->{name}' not found on Google Drive" unless @spreadsheets;
Branch: 1 branch
BranchTrueFalseTotal
unless @spreadsheets 1 2 3
75 2 ▶ LOGDIE "More than one spreadsheet found with name '$self->{name}'. Specify 'id' or 'uri' instead."
Branch: 1 branch
BranchTrueFalseTotal
if scalar @spreadsheets > 1 0 2 2
76 if scalar @spreadsheets > 1;
77 2 $self->{id} = $spreadsheets[0]->{id};
78 2 DEBUG("Got sheet id '$self->{id}' via spreadsheet list.");
79 }
80 }
81  
82 302 return $self->{id};
83 }
84  
85 # when 'api' is eventually called, id will be worked out if we don't already have it.
86 # the resolving of id/name/title/uri is deferred until the first api call.
87 5 ▶sub spreadsheet_name {
Subroutine spreadsheet_name: called CC=3 SCAR=11.4
Pod documented
88 5 my $self = shift;
89 $self->{name} ||= $self->properties('title')->{title}
90 5 ▶ or LOGDIE "No properties title present in properties";
Branch: 1 branch
BranchTrueFalseTotal
$$self{"name"} ||= $self->properties("title")->{"title"} or LOGDIE("No properties title present in properties") 5 0 5
Condition66%2/3
$$self{"name"} ||= $self->properties("title")->{"title"}
l!l&&r!l&&!r
230
MC/DC0%0/2
$$self{"name"} ||= $self->properties("title")->{"title"}
$$self{"name"} $self->properties("title")->{"title"}
Truth table66%2/3
A ||= B
A= $$self{"name"}B= $self->properties("title")->{"title"}
ABresult
1X1
011
000
91 5 return $self->{name};
92 }
93 1 ▶sub spreadsheet_title { spreadsheet_name(@_); }
Subroutine spreadsheet_title: called CC=1 SCAR=0.0
Pod documented
94  
95 # see above routine.
96 12 ▶sub spreadsheet_uri {
Subroutine spreadsheet_uri: called CC=3 SCAR=11.3
Pod documented
97 12 my $self = shift;
98 $self->{uri} ||= $self->attrs('spreadsheetUrl')->{spreadsheetUrl}
99 12 ▶ or LOGDIE "No spreadsheet URI found from get results";
Branch: 1 branch
BranchTrueFalseTotal
$$self{"uri"} ||= $self->attrs("spreadsheetUrl")->{"spreadsheetUrl"} or LOGDIE("No spreadsheet URI found from get results") 12 0 12
Condition66%2/3
$$self{"uri"} ||= $self->attrs("spreadsheetUrl")->{"spreadsheetUrl"}
l!l&&r!l&&!r
1020
MC/DC0%0/2
$$self{"uri"} ||= $self->attrs("spreadsheetUrl")->{"spreadsheetUrl"}
$$self{"uri"} $self->attrs("spreadsheetUrl")->{"spreadsheetUrl"}
Truth table66%2/3
A ||= B
A= $$self{"uri"}B= $self->attrs("spreadsheetUrl")->{"spreadsheetUrl"}
ABresult
1X1
011
000
100 12 $self->{uri} =~ s[/(edit|copy)$][]; # this isn't necessary but keeps things cleaner.
101 12 return $self->{uri};
102 }
103  
104 # return one of the attributes of the spreadsheet.
105 112 ▶sub attrs {
Subroutine attrs: called CC=1 SCAR=0.0
Pod documented
106 112 my $self = shift;
107 112 my $fields = shift;
108 79 ▶ return $self->_cache($fields, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
109 79 $self->api(params => { fields => $fields })
110 112 });
111 }
112  
113 # return one of the property attributes of the spreadsheet.
114 12 ▶sub properties {
Subroutine properties: called CC=1 SCAR=0.0
Pod documented
115 12 my $self = shift;
116 12 state $check = signature(positional => [Str]);
117 12 my ($what) = $check->(@_);
118 12 my $fields = _fields('properties', $what);
119 12 return $self->attrs($fields)->{properties};
120 }
121  
122 # GET https://sheets.googleapis.com/v4/spreadsheets/spreadsheetId?&fields=sheets.properties
123 # returns properties for each worksheet in the spreadsheet.
124 96 ▶sub worksheet_properties {
Subroutine worksheet_properties: called CC=1 SCAR=0.0
Pod documented
125 96 my $self = shift;
126 96 state $check = signature(positional => [Str]);
127 96 my ($what) = $check->(@_);
128 96 my $fields = _fields('sheets.properties', $what);
129 96 my $properties = $self->attrs($fields)->{sheets};
130 96 my @properties = map { $_->{properties}; } @$properties;
131 96 return \@properties;
132 }
133  
134 108 ▶sub _fields {
Subroutine _fields: called CC=2 SCAR=6.9
135 108 my ($fields, $what) = @_;
136 108 ▶ if ($what =~ /^\(/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($what =~ /^\(/) { } 94 14 108
137 94 $fields .= $what;
138 } else {
139 14 $fields .= ".$what";
140 }
141 108 return $fields;
142 }
143  
144 # if multiple attributes are called for, it can hammer the api unnecessarily, so cache the results
145 # for a few seconds (default 5).
146 112 ▶sub _cache {
Subroutine _cache: called CC=3 SCAR=11.0
147 112 my $self = shift;
148  
149 112 state $check = signature(positional => [Str, CodeRef]);
150 112 my ($key, $code) = $check->(@_);
151 112 ▶ return $code->() if !$self->{cache_seconds};
Branch: 1 branch
BranchTrueFalseTotal
unless $$self{"cache_seconds"} 21 91 112
152  
153 91 ▶ $self->{_cache} ||= Cache::Memory::Simple->new();
Condition66%2/3
$$self{"_cache"} ||= "Cache::Memory::Simple"->new
l!l&&r!l&&!r
36550
MC/DC0%0/2
$$self{"_cache"} ||= "Cache::Memory::Simple"->new
$$self{"_cache"} "Cache::Memory::Simple"->new
Truth table66%2/3
A ||= B
A= $$self{"_cache"}B= "Cache::Memory::Simple"->new
ABresult
1X1
011
000
154 # will run the code and store the result for x seconds.
155 return $self->{_cache}->get_or_set(
156 $key, $code, $self->{cache_seconds}
157 91 );
158 }
159  
160 88 ▶sub _cache_delete {
Subroutine _cache_delete: called CC=1 SCAR=0.0
161 88 my $self = shift;
162 88 delete $self->{_cache};
163 88 return;
164 }
165  
166 # sets the number of seconds that attrs will be cached. this can be adjusted
167 # to suit your use-case. if there are lots of people updating things, then a
168 # lower cache value would suit. if you know you are the only one using it, then
169 # a higher cache value will prevent unnecessary calls to the api.
170 11 ▶sub cache_seconds {
Subroutine cache_seconds: called CC=3 SCAR=11.0
Pod documented
171 11 my $self = shift;
172  
173 11 state $check = signature(positional => [PositiveOrZeroNum]);
174 11 my ($cache_seconds) = $check->(@_);
175  
176 11 ▶ $self->{_cache}->delete_all() if $self->{_cache};
Branch: 1 branch
BranchTrueFalseTotal
if $$self{"_cache"} 3 8 11
177  
178 11 ▶ if (!$cache_seconds) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $cache_seconds) { } 9 2 11
179 9 $self->_cache_delete();
180 9 delete $self->{cache_seconds};
181 } else {
182 2 $self->{cache_seconds} = $cache_seconds;
183 }
184  
185 11 return;
186 }
187  
188 4 ▶sub copy_spreadsheet {
Subroutine copy_spreadsheet: called CC=1 SCAR=0.0
Pod documented
189 4 my $self = shift;
190 4 return $self->sheets_api()->copy_spreadsheet(
191 spreadsheet_id => $self->spreadsheet_id(), @_,
192 );
193 }
194  
195 4 ▶sub delete_spreadsheet {
Subroutine delete_spreadsheet: called CC=1 SCAR=0.0
Pod documented
196 4 my $self = shift;
197 4 return $self->sheets_api()->delete_spreadsheet($self->spreadsheet_id());
198 }
199  
200 24 ▶sub range_group {
Subroutine range_group: called CC=1 SCAR=0.0
Pod documented
201 24 my $self = shift;
202 24 state $check = signature(positional => [slurpy ArrayRef[HasRange]]);
203 24 my ($ranges) = $check->(@_);
204 24 return RangeGroup->new(
205 spreadsheet => $self,
206 ranges => $ranges,
207 );
208 }
209  
210 # ties a key => range set of pairs to this spreadsheet.
211 17 ▶sub tie {
Subroutine tie: called CC=1 SCAR=0.0
Pod documented
212 17 my $self = shift;
213 17 my %ranges = @_;
214 17 tie my %tie, Tie, $self;
215 17 tied(%tie)->add_ranges(%ranges);
216 17 return \%tie;
217 }
218  
219 # this is done simply to allow open_worksheet to return the same worksheet instance
220 # each time it's called for the same remote worksheet. this is to avoid working on
221 # multiple local copies of the same remote worksheet.
222 # TODO: if worksheet is renamed, registration should be updated too.
223 94 ▶sub _register_worksheet {
Subroutine _register_worksheet: called CC=2 SCAR=6.9
224 94 my $self = shift;
225 94 state $check = signature(positional => [HasApi]);
226 94 my ($worksheet) = $check->(@_);
227 94 my $name = $worksheet->worksheet_name();
228 94 ▶ return $self->{registered_worksheet}->{$name} if $self->{registered_worksheet}->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $$self{"registered_worksheet"}{$name} 82 12 94
229 12 $self->{registered_worksheet}->{$name} = $worksheet;
230 12 return $worksheet;
231 }
232  
233 # sends batch values to the api.
234 10 ▶sub submit_values {
Subroutine submit_values: called CC=5 SCAR=16.2
Pod documented
235 10 my $self = shift;
236  
237 10 state $check = signature(
238 bless => !!0,
239 named => [
240 ranges => ArrayRef[HasMethods[qw(has_values batch_values values_response_from_api)]],
241 content => HashRef, { default => {} },
242 ],
243 );
244 10 my $p = $check->(@_);
245  
246 # find out which ranges have something to send.
247 10 my @ranges = grep { $_->has_values(); } @{ delete $p->{ranges} };
248 10 my @values = map { $_->batch_values(); } @ranges;
249 10 ▶ return if !@values;
Branch: 1 branch
BranchTrueFalseTotal
unless @values 0 10 10
250  
251 10 $p->{content}->{data} = \@values;
252 10 ▶ $p->{content}->{valueInputOption} //= 'USER_ENTERED';
Condition50%1/2
$$p{"content"}{"valueInputOption"} //= "USER_ENTERED"
l!l
010
MC/DC0%0/1
$$p{"content"}{"valueInputOption"} //= "USER_ENTERED"
$$p{"content"}{"valueInputOption"}
Truth table50%1/2
A
A= $$p{"content"}{"valueInputOption"}
Aresult
11
00
253 10 $p->{method} = 'post';
254 10 $p->{uri} = "/values:batchUpdate";
255 10 my $api = $self->api(%$p); # this is where the rubber hits the road.
256  
257 # each range that had values should strip off the response from the api's
258 # responses array. if everything is in sync, there should be no responses left.
259 10 my $responses = delete $api->{responses};
260 10 $_->values_response_from_api($responses) foreach @ranges;
261 10 ▶ LOGDIE "Returned batch values update responses were not consumed" if @$responses;
Branch: 1 branch
BranchTrueFalseTotal
if @$responses 0 10 10
262  
263 # return whatever's left over.
264 10 return $api;
265 }
266  
267 # sends batch requests (formatting etc) to the api.
268 79 ▶sub submit_requests {
Subroutine submit_requests: called CC=6 SCAR=18.0
Pod documented
269 79 my $self = shift;
270  
271 79 state $check = signature(
272 bless => !!0,
273 named => [
274 ranges => ArrayRef[HasMethods[qw(batch_requests requests_response_from_api)]], { default => [] }, # might just be self.
275 content => HashRef, { default => {} },
276 ],
277 );
278 79 my $p = $check->(@_);
279  
280 79 my @all_requests = (@{ delete $p->{ranges} }, $self); # add myself to the list.
281  
282 # for each object that has requests to submit, store them so that
283 # they can process the responses that come back.
284 my @ranges = map {
285 79 ▶ $_->batch_requests() ? $_ : ();
Branch: 1 branch
BranchTrueFalseTotal
$_->batch_requests ? : 79 70 149
286 } @all_requests;
287 79 ▶ return if !@ranges;
Branch: 1 branch
BranchTrueFalseTotal
unless @ranges 0 79 79
288  
289 # pull out the requests hashes to be sent to the rest api.
290 my @batch_requests = map {
291 79 $_->batch_requests();
292 } @all_requests;
293 79 ▶ return if !@batch_requests;
Branch: 1 branch
BranchTrueFalseTotal
unless @batch_requests 0 79 79
294  
295 # we're about to do a bunch of requests that could affect what's in the cache.
296 # TODO: selectively invalidate the cache based on what's being submitted.
297 79 $self->_cache_delete();
298  
299 # call the batch request api.
300 79 $p->{content}->{requests} = \@batch_requests;
301 79 $p->{method} = 'post';
302 79 $p->{uri} = ':batchUpdate';
303 79 my $api = $self->api(%$p);
304  
305 # grab the json decoded replies from the response.
306 79 my $responses = delete $api->{replies};
307 # present the responses back to those who are waiting, each will strip off the ones they requested.
308 79 $_->requests_response_from_api($responses) foreach @ranges;
309 # if there are any left over, it sux. we are out of sync somewhere. all requestors should
310 # process their corresponding response.
311 79 ▶ LOGDIE "Returned batch request responses were not consumed" if @$responses;
Branch: 1 branch
BranchTrueFalseTotal
if @$responses 0 79 79
312  
313 79 return $api;
314 }
315  
316 0 ▶sub named_ranges {
Subroutine named_ranges: not called CC=2 SCAR=17.9
Pod documented
317 0 my $self = shift;
318  
319 0 state $check = signature(positional => [RangeNamed, { optional => 1 }]);
320 0 my ($named_range_name) = $check->(@_);
321  
322 0 my $named_ranges = $self->attrs('namedRanges')->{namedRanges};
323 0 ▶ return $named_ranges if !$named_range_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $named_range_name 0 0 0
324  
325 0 my ($named_range) = grep { $_->{name} eq $named_range_name; } @$named_ranges;
326 0 return $named_range;
327 }
328  
329 # takes a named range and works out the actual range it represents.
330 0 ▶sub normalize_named {
Subroutine normalize_named: not called CC=2 SCAR=17.9
Pod undocumented
331 0 my $self = shift;
332  
333 0 state $check = signature(positional => [RangeNamed]);
334 0 my ($named_range_name) = $check->(@_);
335  
336 0 ▶ my $named_range = $self->named_ranges($named_range_name) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $named_range = $self->named_ranges($named_range_name) or return 0 0 0
337 0 $named_range = $named_range->{range};
338 my $range = [
339 [ $named_range->{startColumnIndex} + 1, $named_range->{startRowIndex} + 1 ],
340 0 [ $named_range->{endColumnIndex}, $named_range->{endRowIndex} ],
341 ];
342  
343 0 return ($named_range->{sheetId}, $range);
344 }
345  
346 0 ▶sub protected_ranges { shift->attrs('sheets.protectedRanges')->{sheets}; }
Subroutine protected_ranges: not called CC=1 SCAR=6.9
Pod documented
347  
348 # each worksheet has an entry:
349 # ---
350 # - protectedRanges:
351 # - editors:
352 # users:
353 # - xxx@gmail.com
354 # - yyy@gmail.com
355 # protectedRangeId: 1161285259
356 # range: {}
357 # requestingUserCanEdit: !!perl/scalar:JSON::PP::Boolean 1
358 # warningOnly: !!perl/scalar:JSON::PP::Boolean 1
359 # - {}
360 # - {}
361 # submit_requests needs to be called by the caller after this.
362 0 ▶sub delete_all_protected_ranges {
Subroutine delete_all_protected_ranges: not called CC=4 SCAR=30.0
Pod documented
363 0 my $self = shift;
364 0 foreach my $worksheet (@{ $self->protected_ranges() }) {
365 0 ▶ my $ranges = $worksheet->{protectedRanges} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $ranges = $$worksheet{"protectedRanges"} or next 0 0 0
366 0 $self->delete_protected_range($_->{protectedRangeId}) foreach (@$ranges);
367 }
368 0 return $self;
369 }
370  
371 91 ▶sub open_worksheet { Worksheet->new(spreadsheet => shift, @_); }
Subroutine open_worksheet: called CC=1 SCAR=0.0
Pod documented
372 263 ▶sub sheets_api { shift->{sheets_api}; }
Subroutine sheets_api: called CC=1 SCAR=0.0
Pod documented
373 8 ▶sub rest_api { shift->sheets_api()->rest_api(); }
Subroutine rest_api: called CC=1 SCAR=0.0
Pod undocumented
374 0 ▶sub transaction { shift->sheets_api()->transaction(); }
Subroutine transaction: not called CC=1 SCAR=6.9
Pod undocumented
375 6 ▶sub stats { shift->sheets_api()->stats(); }
Subroutine stats: called CC=1 SCAR=0.0
Pod undocumented
376 1 ▶sub reset_stats { shift->sheets_api->reset_stats(); }
Subroutine reset_stats: called CC=1 SCAR=0.0
Pod undocumented
377  
378 2 1;
379  
380 __END__
« RestApi.pm ↑ Summary RestApi/GmailApi1/Label.pm »