Utils/SearchGoogleAdsIterator.pm

filter: Statementstmt 100% 46 / 46 Branchbran 75.0% 6 / 8 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 8 / 8 Podpod 66.6% 2 / 3 totaltotal 90.6% 68 / 75 CC 7 SCAR 10.7
CC
7
Cov
93%
has_next
5.1
next
3.0
BEGIN
1.0
CRAP 2.9
« Utils/SearchStreamHandler.pm ↑ Summary V23/Utils/ResourceNames.pm »
1 # Copyright 2019, Google LLC
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 #
15 # The iterator class to access all rows that match the search query.
16  
17 package Google::Ads::GoogleAds::Utils::SearchGoogleAdsIterator;
18  
19 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 # The following needs to be on one line because CPAN uses a particularly hacky
24 # eval() to determine module versions.
25 2 ▶use Google::Ads::GoogleAds::Constants; our $VERSION = ${Google::Ads::GoogleAds::Constants::VERSION};
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 2 ▶use Class::Std::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 # Class::Std-style attributes. Most values are read from googleads.properties file.
30 # These need to go in the same line for older Perl interpreters to understand.
31 2 my %service_of : ATTR(:name<service> :default<>);
32 2 my %request_of : ATTR(:name<request> :default<>);
33 2 my %current_response_of : ATTR(:name<current_response> :default<>);
34 2 my %current_cursor_of : ATTR(:name<current_cursor> :default<0>);
35  
36 # Automatically called by Class::Std after the values for all the attributes
37 # have been populated but before the constructor returns the new object.
38 1 ▶sub START {
Subroutine START: called CC=1 SCAR=0.0
Pod undocumented
39 1 my ($self, $ident) = @_;
40  
41 1 my $response = $service_of{$ident}->search($request_of{$ident});
42 1 $current_response_of{$ident} = $response;
43 1 $current_cursor_of{$ident} = 0;
44 }
45  
46 # Checks whether the iteration has more elements.
47 17 ▶sub has_next {
Subroutine has_next: called CC=5 SCAR=16.4
Pod documented
48 17 my $self = shift;
49  
50 17 my $current_response = $self->get_current_response();
51 17 ▶ return 0 if !$current_response or !$current_response->{results};
Branch: 1 branch
BranchTrueFalseTotal
unless $current_response and $current_response->{"results"} 0 17 17
Condition33%1/3
$current_response and $current_response->{"results"}
!ll&&!rl&&r
0017
MC/DC0%0/2
$current_response and $current_response->{"results"}
$current_response $current_response->{"results"}
Truth table33%1/3
A and B
A= $current_responseB= $current_response->{"results"}
ABresult
0X0
100
111
52  
53 17 my $current_cursor = $self->get_current_cursor();
54 return 0
55 unless $current_response->{nextPageToken}
56 17 ▶ or scalar @{$current_response->{results}} > $current_cursor;
Branch: 1 branch
BranchTrueFalseTotal
unless $current_response->{"nextPageToken"} or scalar @{$current_response->{"results"};} > $current_cursor 1 16 17
Condition100%3/3
$current_response->{"nextPageToken"} or scalar @{$current_response->{"results"};} > $current_cursor
l!l&&r!l&&!r
1421
MC/DC100%2/2
$current_response->{"nextPageToken"} or scalar @{$current_response->{"results"};} > $current_cursor
$current_response->{"nextPageToken"} scalar @{$current_response->{"results"};} > $current_cursor
Truth table100%3/3
A or B
A= $current_response->{"nextPageToken"}B= scalar @{$current_response->{"results"};} > $current_cursor
ABresult
1X1
011
000
57  
58 16 return 1;
59 }
60  
61 # Returns the next element in the iteration.
62 8 ▶sub next {
Subroutine next: called CC=3 SCAR=11.0
Pod documented
63 8 my $self = shift;
64 8 ▶ return undef if !$self->has_next();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_next 0 8 8
65  
66 8 my $current_response = $self->get_current_response();
67 8 my $current_cursor = $self->get_current_cursor();
68  
69 8 ▶ if (scalar @{$current_response->{results}} == $current_cursor) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$current_response->{'results'};} == $current_cursor) { } 2 6 8
70 2 my $request = $self->get_request();
71 2 $request->{pageToken} = $current_response->{nextPageToken};
72  
73 2 my $response = $self->get_service()->search($request);
74 2 $self->set_current_response($response);
75  
76 2 $self->set_current_cursor(1);
77 2 return $response->{results}[0];
78 } else {
79 6 $self->set_current_cursor($current_cursor + 1);
80 6 return $current_response->{results}[$current_cursor];
81 }
82 }
83  
84 2 1;
85  
86 =pod
87  
88 =head1 NAME
89  
90 Google::Ads::GoogleAds::Utils::SearchGoogleAdsIterator
91  
92 =head1 SYNOPSIS
93  
94 my $search_request =
95 Google::Ads::GoogleAds::V26::Services::GoogleAdsService::SearchGoogleAdsRequest
96 ->new({
97 customerId => $customer_id,
98 query => "SELECT campaign.id, campaign.name FROM campaign ORDER BY campaign.id",
99 pageSize => 100
100 });
101  
102 my $google_ads_service = $api_client->GoogleAdsService();
103  
104 my $iterator = Google::Ads::GoogleAds::Utils::SearchGoogleAdsIterator->new({
105 service => $google_ads_service,
106 request => $search_request
107 });
108  
109 while ($iterator->has_next) {
110 my $google_ads_row = $iterator->next;
111 # Do something with each GooglerAdsRow object.
112 }
113  
114 =head1 DESCRIPTION
115  
116 The iterator class to access all rows that match the search query. The iterator
117 should be constructed with a L<Google::Ads::GoogleAds::V26::Services::GoogleAdsService>
118 and a L<Google::Ads::GoogleAds::V26::Services::GoogleAdsService::SearchGoogleAdsRequest>.
119  
120 my $iterator = Google::Ads::GoogleAds::Utils::SearchGoogleAdsIterator->new({
121 service => $google_ads_service,
122 request => $search_request
123 });
124  
125 =head1 METHODS
126  
127 =head2 has_next
128  
129 Checks whether the iteration has more elements.
130  
131 =head3 Returns
132  
133 True, if the iteration has more elements. False, otherwise.
134  
135 =head2 next
136  
137 Returns the next element in the iteration.
138  
139 =head3 Returns
140  
141 The next element in the iteration if there's any, otherwise undef.
142  
143 =head1 LICENSE AND COPYRIGHT
144  
145 Copyright 2019 Google LLC
146  
147 Licensed under the Apache License, Version 2.0 (the "License");
148 you may not use this file except in compliance with the License.
149 You may obtain a copy of the License at
150  
151 http://www.apache.org/licenses/LICENSE-2.0
152  
153 Unless required by applicable law or agreed to in writing, software
154 distributed under the License is distributed on an "AS IS" BASIS,
155 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
156 See the License for the specific language governing permissions and
157 limitations under the License.
158  
159 =head1 REPOSITORY INFORMATION
160  
161 $Rev: $
162 $LastChangedBy: $
163 $Id: $
164  
165 =cut
« Utils/SearchStreamHandler.pm ↑ Summary V23/Utils/ResourceNames.pm »