File Coverage

blib/lib/Google/Ads/GoogleAds/Utils/SearchGoogleAdsIterator.pm
Criterion Covered Total %
statement 40 40 100.0
branch 6 8 75.0
condition 4 6 66.6
subroutine 8 8 100.0
pod 2 3 66.6
total 60 65 92.3


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