Utils/SearchStreamHandler.pm

filter: Statementstmt 97.6% 42 / 43 Branchbran 50.0% 4 / 8 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 11 / 11 Podpod 50.0% 1 / 2 totaltotal 85.5% 59 / 69 CC 7 SCAR 11.7
CC
7
Cov
87%
__ANON__
6.9
START
2.1
process_contents
1.0
CRAP 3.2
« Utils/GoogleAdsHelper.pm ↑ Summary Utils/SearchGoogleAdsIterator.pm »
1 # Copyright 2020, 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 handler class to process the response of stream search.
16  
17 package Google::Ads::GoogleAds::Utils::SearchStreamHandler;
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 2 ▶use JSON::SL;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 2 ▶use constant JSON_POINTER_RESULTS => "/^/results";
Subroutine BEGIN: called CC=1 SCAR=0.0
31 2 ▶use constant JSON_POINTER_REQUEST_ID => "/^/requestId";
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 # Class::Std-style attributes. Most values are read from googleads.properties file.
34 # These need to go in the same line for older Perl interpreters to understand.
35 2 my %service_of : ATTR(:name<service> :default<>);
36 2 my %request_of : ATTR(:name<request> :default<>);
37 2 my %json_sl_of : ATTR(:name<json_sl> :default<>);
38  
39 # Automatically called by Class::Std after the values for all the attributes
40 # have been populated but before the constructor returns the new object.
41 2 ▶sub START {
Subroutine START: called CC=2 SCAR=7.6
Pod undocumented
42 2 my ($self, $ident) = @_;
43  
44 2 ▶ $json_sl_of{$ident} ||= JSON::SL->new();
Condition33%1/3
$json_sl_of{$ident} ||= "JSON::SL"->new
l!l&&r!l&&!r
020
MC/DC0%0/2
$json_sl_of{$ident} ||= "JSON::SL"->new
$json_sl_of{$ident} "JSON::SL"->new
Truth table33%1/3
A ||= B
A= $json_sl_of{$ident}B= "JSON::SL"->new
ABresult
1X1
011
000
45 # Set the query path to "results" for the JSON::SL object.
46 2 $json_sl_of{$ident}
47 ->set_jsonpointer([JSON_POINTER_RESULTS, JSON_POINTER_REQUEST_ID]);
48 }
49  
50 # Processes the response content of stream search.
51 # The $for_each_callback is invoked as a subroutine reference for each parsed GoogleAdsRow.
52 2 ▶sub process_contents {
Subroutine process_contents: called CC=1 SCAR=0.2
Pod documented
53 2 my ($self, $for_each_callback) = @_;
54  
55 $self->get_service()->search_stream(
56 $self->get_request(),
57 # The $content_callback subroutine defined below takes two arguments:
58 # $data - the chunk of data
59 # $response - the HTTP::Response
60 1 ▶ sub {
Subroutine __ANON__: called CC=6 SCAR=19.3
61 1 my ($data, $response) = @_;
62  
63 1 my $json_sl = $self->get_json_sl();
64 # Append the returned data chunk to the JSON input stream.
65 1 $json_sl->feed($data);
66  
67 # The fetch() method returns the remaining decoded JSON objects specified
68 # by the JSON pointers.
69 1 ▶ while (my $fetched = $json_sl->fetch()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fetched = $json_sl->fetch) 1 1 2
70 1 ▶ if ($fetched->{JSONPointer} eq JSON_POINTER_RESULTS) {
Branch: 2 branches
BranchTrueFalseTotal
if ($fetched->{'JSONPointer'} eq '/^/results') { } 1 0 1
elsif ($fetched->{'JSONPointer'} eq '/^/requestId') { } 0 0 0
71 1 foreach my $google_ads_row (@{$fetched->{Value}}) {
72 # Call the $for_each_callback subroutine for each parsed row.
73 8 ▶ $for_each_callback->($google_ads_row) if $for_each_callback;
Branch: 1 branch
BranchTrueFalseTotal
if $for_each_callback 8 0 8
74 }
75 } elsif ($fetched->{JSONPointer} eq JSON_POINTER_REQUEST_ID) {
76 # Add the request id from the JSON payload to the HTTP response header
77 # to be logged properly.
78 0 $response->header("request-id" => $fetched->{Value});
79 }
80 }
81 2 });
82 }
83  
84 2 1;
85  
86 =pod
87  
88 =head1 NAME
89  
90 Google::Ads::GoogleAds::Utils::SearchStreamHandler
91  
92 =head1 SYNOPSIS
93  
94 my $search_stream_request =
95 Google::Ads::GoogleAds::V26::Services::GoogleAdsService::SearchGoogleAdsStreamRequest
96 ->new({
97 customerId => $customer_id,
98 query => "SELECT campaign.id, campaign.name FROM campaign ORDER BY campaign.id"
99 });
100  
101 my $google_ads_service = $api_client->GoogleAdsService();
102  
103 my $search_stream_handler = Google::Ads::GoogleAds::Utils::SearchStreamHandler->new({
104 service => $google_ads_service,
105 request => $search_stream_request
106 });
107  
108 $search_stream_handler->process_contents(
109 sub {
110 my $google_ads_row = shift;
111 # Do something with each GooglerAdsRow object.
112 });
113  
114 =head1 DESCRIPTION
115  
116 The handler class to process the response of stream search. The handler should be
117 constructed with a L<Google::Ads::GoogleAds::V26::Services::GoogleAdsService> and a
118 L<Google::Ads::GoogleAds::V26::Services::GoogleAdsService::SearchGoogleAdsStreamRequest>.
119  
120 my $search_stream_handler = Google::Ads::GoogleAds::Utils::SearchStreamHandler->new({
121 service => $google_ads_service,
122 request => $search_stream_request
123 });
124  
125 =head1 METHODS
126  
127 =head2 process_contents
128  
129 Processes the response content of stream search.
130  
131 =head3 Parameters
132  
133 =over
134  
135 =item *
136  
137 I<for_each_callback>: The callback subroutine which is invoked to process each
138 parsed L<Google::Ads::GoogleAds::V26::Services::GoogleAdsService::GoogleAdsRow>.
139  
140 =back
141  
142 =head1 LICENSE AND COPYRIGHT
143  
144 Copyright 2020 Google LLC
145  
146 Licensed under the Apache License, Version 2.0 (the "License");
147 you may not use this file except in compliance with the License.
148 You may obtain a copy of the License at
149  
150 http://www.apache.org/licenses/LICENSE-2.0
151  
152 Unless required by applicable law or agreed to in writing, software
153 distributed under the License is distributed on an "AS IS" BASIS,
154 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
155 See the License for the specific language governing permissions and
156 limitations under the License.
157  
158 =head1 REPOSITORY INFORMATION
159  
160 $Rev: $
161 $LastChangedBy: $
162 $Id: $
163  
164 =cut
« Utils/GoogleAdsHelper.pm ↑ Summary Utils/SearchGoogleAdsIterator.pm »