File Coverage

lib/Google/RestApi/SheetsApi4/Request.pm
Criterion Covered Total %
statement 59 69 85.5
branch 19 38 50.0
condition 5 8 62.5
subroutine 9 10 90.0
pod 2 4 50.0
total 94 129 72.8


line stmt bran cond sub pod time code
1              
2             our $VERSION = '1.0.2';
3              
4             use Google::RestApi::Setup;
5 1     1   476  
  1         4  
  1         7  
6             use Hash::Merge ();
7 1     1   20046 use List::MoreUtils qw( first_index );
  1         3  
  1         19  
8 1     1   4 use Storable ();
  1         15  
  1         10  
9 1     1   869  
  1         3  
  1         1049  
10              
11 0     0 1 0 my $self = shift;
12              
13             my %request = @_;
14 191     191 1 328  
15             $self->{requests} ||= [];
16 191         377 my $requests = $self->{requests};
17              
18 191   100     471 if (%request) {
19 191         396 delete $self->{requests_response}; # any previous responses are no longer valid.
20             push(@$requests, \%request) if !$self->merge_request(\%request);
21 191 100       346 }
22 97         306  
23 97 100       253 return @$requests;
24             }
25              
26 191         588 my $self = shift;
27              
28             state $check = compile(HashRef);
29             my ($request) = $check->(@_);
30 97     97 0 144  
31             my $requests = $self->{requests} or return;
32 97         135 my ($index) = first_index {
33 97         866 $self->_can_merge($request, $_);
34             } @$requests;
35 97 50       885 return if $index < 0;
36              
37 80     80   280 my $other_request = $requests->[$index];
38 97         596  
39 97 100       391 my $key = (keys %$request)[0];
40             my $fields = $request->{$key}->{fields};
41 77         170 my $other_fields = $other_request->{$key}->{fields};
42              
43 77         178 my %fields;
44 77         181 %fields = map { $_ => 1 } split(',', $fields), split(',', $other_fields)
45 77         155 if $fields && $other_fields;
46             $other_request = Hash::Merge::merge($request, $other_request);
47 77         104 $other_request->{$key}->{fields} = join(',', sort keys %fields) if %fields;
48 77 100 66     353  
  201         413  
49             $requests->[$index] = $other_request;
50 77         265  
51 77 100       28772 return $other_request;
52             }
53 77         259  
54             my $self = shift;
55 77         249  
56             my ($request, $other_request) = @_;
57             my $key = (keys %$request)[0];
58             my $other_key = (keys %$other_request)[0];
59 80     80   144  
60             return if $key ne $other_key;
61 80         128 return if $key =~ /^(deleteProtected|deleteNamed|deleteEmbeded)/;
62 80         252  
63 80         201 if ($key =~ /^updateProtected/) {
64             my $id = $request->{$key}->{protectedRangeId} or return;
65 80 100       209 my $other_id = $other_request->{$key}->{protectedRangeId} or return;
66 77 50       200 return $id == $other_id;
67             }
68 77 50       165  
69 0 0       0 if ($key =~ /^updateNamedRange/) {
70 0 0       0 my $id = $request->{$key}->{namedRange}->{namedRangeId} or return;
71 0         0 my $other_id = $other_request->{$key}->{namedRange}->{namedRangeId} or return;
72             return $id == $other_id;
73             }
74 77 50       161  
75 0 0       0 if ($key =~ /^updateEmbededObject/) {
76 0 0       0 my $id = $request->{$key}->{objectId} or return;
77 0         0 my $other_id = $other_request->{$key}->{objectId} or return;
78             return $id == $other_id;
79             }
80 77 50       134  
81 0 0       0 return 1;
82 0 0       0 }
83 0         0  
84             my $self = shift;
85              
86 77         190 # we didn't ask for any requests, nothing more to do.
87             return if @_ && !$self->{requests};
88              
89             state $check = compile(ArrayRef, { optional => 1 });
90 9     9 0 18 my ($requests) = $check->(@_);
91             return $self->{requests_response} if !$requests;
92              
93 9 50 33     63 # strip off a response for each request made.
94             $self->{requests_response} = [];
95 9         24 push(@{ $self->{requests_response} }, shift @$requests)
96 9         650 for (1..scalar @{ $self->{requests} });
97 9 50       112  
98             # don't store the original requests now that they've been done.
99             delete $self->{requests};
100 9         21  
101 9         24 return $self->{requests_response};
102 9         15 }
  9         35  
103              
104             1;
105 9         25  
106              
107 9         26 =head1 NAME
108              
109             Google::RestApi::SheetsApi4::Request - A base class to build Google API's batchRequest.
110              
111             =head1 DESCRIPTION
112              
113             A Request is a lightweight object that is used to collect and then
114             submit a number of batch requests such as formatting, spreadsheet
115             properties, worksheet properties etc.
116              
117             Other classes in this api derive from this object and its child
118             objects. You would not normally have to interact with this object
119             directly as it is already built in to the other classes in this
120             api. It is documented here for background understanding.
121              
122             Batch requests are formulated and queued up to be submitted later
123             via 'submit_requests'. This class hierarchy encapsulates the
124             tedious work of constructing the complex, deep hashes required for
125             cell formatting or setting various properties.
126              
127             Spreadsheet: Derives from Request::Spreadsheet.
128             Worksheet: Derives from Request::Spreadsheet::Worksheet.
129             Range: Derives from Request::Spreadsheet::Worksheet::Range.
130              
131             A Spreadsheet object can only submit requests that have to do with
132             spreadsheets. A Worksheet object can submit requests that have to
133             do with worksheets, and also for parent spreadsheets. A Range object
134             can submit requests that have to do with ranges, worksheets, and
135             spreadsheets.
136              
137             In some cases, multiple calls for the same-named request can be
138             merged into a single request. For example $range->bold()->blue()->center()
139             targets three Google API requests of the same name: 'repeatCell'.
140             Instead of sending three small 'repeatCell' requests, these are
141             all merged together into one 'repeatCell' request for efficiency.
142              
143             See the description and synopsis at Google::RestApi::SheetsApi4.
144              
145             =head1 SUBROUTINES
146              
147             =over
148              
149             =item batch_requests(%request);
150              
151             Returns all the queued requests if none is passed, or adds the passed
152             request to the queue. A request may be merged into an already-existing
153             request of the same name.
154              
155             =item submit_requests(%args);
156              
157             This is a pure virtual function that must be overridden in the derived
158             class. The derived class must decide what to do when the queued requests
159             are ready to be sumitted. It must eventually pass the requests to the
160             parent SheetsApi4 object.
161              
162             =back
163              
164             =head1 AUTHORS
165              
166             =over
167              
168             =item
169              
170             Robin Murray mvsjes@cpan.org
171              
172             =back
173              
174             =head1 COPYRIGHT
175              
176             Copyright (c) 2021, Robin Murray. All rights reserved.
177              
178             This program is free software; you may redistribute it and/or modify it under the same terms as Perl itself.