| 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 | # This module provides utility methods to other services. | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | package Google::Ads::GoogleAds::Utils::GoogleAdsHelper; | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 10 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 10 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 10 | ▶ | 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 | 10 | ▶ | use Google::Ads::GoogleAds::Constants; our $VERSION = ${Google::Ads::GoogleAds::Constants::VERSION}; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 10 | ▶ | use Exporter::Auto; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 10 | ▶ | use Storable qw(dclone); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | # Deletes the unassigned fields in the hash reference. | ||||||||||||||||||||||||||||
| 31 | 38 | ▶ | sub remove_unassigned_fields { | ||||||||||||||||||||||||||
|
Subroutine
remove_unassigned_fields: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 32 | 38 | my ($hash_ref, $args) = @_; | |||||||||||||||||||||||||||
| 33 | 38 | delete @{$hash_ref}{grep { not exists $args->{$_} } keys %$hash_ref}; | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | # Checks whether the scripts parameters are not the "INSERT_..._HERE" values. | ||||||||||||||||||||||||||||
| 37 | 9 | ▶ | sub check_params { | ||||||||||||||||||||||||||
|
Subroutine
check_params: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 38 | 9 | my (@params) = @_; | |||||||||||||||||||||||||||
| 39 | 9 | foreach my $param (@params) { | |||||||||||||||||||||||||||
| 40 | 14 | ▶ | if (ref $param eq "ARRAY") { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 41 | 3 | ▶ | return 0 if !(@$param && check_params(@$param)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@$param and check_params(@$param)
MC/DC100%2/2
@$param and check_params(@$param)
@$param
check_params(@$param)
Truth table100%3/3
A and B
A= @$paramB= check_params(@$param)
| |||||||||||||||||||||||||||||
| 42 | ▶ | } elsif (!defined $param || $param =~ /INSERT_.*_HERE/) { | |||||||||||||||||||||||||||
|
Condition100%3/3
not defined $param or $param =~ /INSERT_.*_HERE/
MC/DC100%2/2
not defined $param or $param =~ /INSERT_.*_HERE/
not defined $param
$param =~ /INSERT_.*_HERE/
Truth table100%3/3
not(A) or B
A= not defined $paramB= $param =~ /INSERT_.*_HERE/
| |||||||||||||||||||||||||||||
| 43 | 4 | return 0; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | 3 | return 1; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | # Removes the leading and trailing spaces and line breaks from a string. | ||||||||||||||||||||||||||||
| 50 | 2 | ▶ | sub trim { | ||||||||||||||||||||||||||
|
Subroutine
trim: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 51 | 2 | my $str = shift; | |||||||||||||||||||||||||||
| 52 | 2 | ▶ | return $str if !defined $str; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 1 | $str =~ s/^\s*(.*?)\s*$/$1/; | |||||||||||||||||||||||||||
| 54 | 1 | return $str; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | # Expands a path template by replacing the parameters in braces with the given | ||||||||||||||||||||||||||||
| 58 | # arguments. | ||||||||||||||||||||||||||||
| 59 | 38 | ▶ | sub expand_path_template { | ||||||||||||||||||||||||||
|
Subroutine
expand_path_template: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 60 | 38 | my ($path_template, $args) = @_; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | # To support the {+customers} format template. | ||||||||||||||||||||||||||||
| 63 | 38 | $path_template =~ s/\{\+/\{/g; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 38 | ▶ | if (not ref $args) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 66 | 3 | ▶ | $path_template =~ s/\{\w+}/$args/ if defined $args; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | } elsif (ref $args eq "ARRAY") { | ||||||||||||||||||||||||||||
| 68 | 32 | ▶ | $path_template =~ s/\{\w+}/shift @$args if @$args/eg; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | } else { | ||||||||||||||||||||||||||||
| 70 | 3 | ▶ | $path_template =~ s/\{(\w+)}/delete $args->{$1} if exists $args->{$1}/eg; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 38 | return $path_template; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | # Copies a hash reference to a new object. | ||||||||||||||||||||||||||||
| 77 | 2 | ▶ | sub copy_from { | ||||||||||||||||||||||||||
|
Subroutine
copy_from: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 78 | 2 | my ($original) = shift; | |||||||||||||||||||||||||||
| 79 | 2 | ▶ | return undef if !$original; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 1 | return dclone($original); | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | # Converts a string to lower underscore case. | ||||||||||||||||||||||||||||
| 84 | 72 | ▶ | sub to_lower_underscore { | ||||||||||||||||||||||||||
|
Subroutine
to_lower_underscore: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 85 | 72 | my $str = shift; | |||||||||||||||||||||||||||
| 86 | 72 | ▶ | return $str if !defined $str; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 71 | $str =~ | |||||||||||||||||||||||||||
| 88 | 53 | ▶ | s/(?:\b|(?<=([a-z])))([A-Z][a-z]+)/(defined($1) ? "_" : "") . lc($2)/eg; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 71 | return $str; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | # Converts a scalar to boolean string. | ||||||||||||||||||||||||||||
| 93 | 5 | ▶ | sub to_boolean { | ||||||||||||||||||||||||||
|
Subroutine
to_boolean: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 94 | 5 | ▶ | shift ? "true" : "false"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | # Dies with a specified exit code. | ||||||||||||||||||||||||||||
| 98 | 1 | ▶ | sub die_with_code { | ||||||||||||||||||||||||||
|
Subroutine
die_with_code: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 99 | 1 | my $exit_code = shift; | |||||||||||||||||||||||||||
| 100 | 1 | $! = $exit_code; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 1 | die @_; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 10 | 1; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | =pod | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | =head1 NAME | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | Google::Ads::GoogleAds::Utils::GoogleAdsHelper | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | This module provides utility methods to other services. | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | =head1 METHODS | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | =head2 remove_unassigned_fields | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | Removes the fields not presenting in the constructor arguments from a newly created | ||||||||||||||||||||||||||||
| 122 | Google Ads API entity. These fields will be excluded when encoding the JSON HTTP | ||||||||||||||||||||||||||||
| 123 | request payload. | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | =head3 Parameters | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | =over | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | =item * | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | I<hash_ref>: a hash reference to the newly created Google Ads API entity. | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | =item * | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | I<args>: the arguments for the constructor of a Google Ads API entity. | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | =back | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | =head2 check_params | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | Checks whether the parameters in the code sample are correctly specified. The | ||||||||||||||||||||||||||||
| 142 | values can either be set in the source code or passed in from the command line. | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | =head3 Parameters | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | =over | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | =item * | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | I<params>: an array of parameters in the code sample to verify. | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | =back | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | =head3 Returns | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | True, if all the parameters are correctly specified. False, otherwise. | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | =head2 trim | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | Removes the leading and trailing spaces and line breaks from a string. | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | =head3 Parameters | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | =over | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | =item * | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | The original input string. | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | =back | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | =head3 Returns | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | The trimmed string without leading and trailing white spaces. | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | =head2 expand_path_template | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | Expands a path template by replacing the parameters in braces with the given | ||||||||||||||||||||||||||||
| 179 | arguments. | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | =head3 Parameters | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | =over | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | =item * | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | I<path_template>: the path template to expand. The format could be: | ||||||||||||||||||||||||||||
| 188 | 'customers/{customer_id}/adGroups/{ad_group_id}' or | ||||||||||||||||||||||||||||
| 189 | 'v26/customers/{+customerId}/adGroups:mutate'. | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | =item * | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | I<args>: the args in scalar or array/hash reference used to expand the template. | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | =back | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | =head3 Returns | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | The expanded path template. | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | =head2 copy_from | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | Copies a hash reference deeply to a new object. | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | =head3 Parameters | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | =over | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | =item * | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | I<original>: the original hash reference to copy from. | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | =back | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | =head3 Returns | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | A deeply copied object based on the C<original> hash reference. | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | =head2 to_lower_underscore | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | Converts a string to lower underscore case. | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | =head3 Parameters | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | =over | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | =item * | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | The original input string. | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | =back | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | =head3 Returns | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | The result string in lower underscore case. | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | =head2 to_boolean | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | Converts a scalar to boolean string. | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | =head3 Parameters | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | =over | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | =item * | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | The original input scalar value. | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | =back | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | =head3 Returns | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | "true" if the input value is valid. "false", otherwise. | ||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | =head2 die_with_code | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | Dies with a specified exit code. | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | =head3 Parameters | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | =over | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | =item * | ||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | I<exit_code>: the exit code. | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | =item * | ||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | I<list>: list of one or more items, which will be stringified and concatenated | ||||||||||||||||||||||||||||
| 270 | to make the exception. | ||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | =back | ||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | Copyright 2019 Google LLC | ||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | Licensed under the Apache License, Version 2.0 (the "License"); | ||||||||||||||||||||||||||||
| 279 | you may not use this file except in compliance with the License. | ||||||||||||||||||||||||||||
| 280 | You may obtain a copy of the License at | ||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||
| 282 | http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | Unless required by applicable law or agreed to in writing, software | ||||||||||||||||||||||||||||
| 285 | distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||||||||||||||||||||
| 286 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||||||||||||||||||||
| 287 | See the License for the specific language governing permissions and | ||||||||||||||||||||||||||||
| 288 | limitations under the License. | ||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | =head1 REPOSITORY INFORMATION | ||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | $Rev: $ | ||||||||||||||||||||||||||||
| 293 | $LastChangedBy: $ | ||||||||||||||||||||||||||||
| 294 | $Id: $ | ||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | =cut | ||||||||||||||||||||||||||||