blib/lib/Google/Ads/Common/Utilities/AdsUtilityRegistry.pm untested

filter: Statementstmt 0.0% 0 / 41 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 3 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 64 CC 3 SCAR 11.5
CC
3
Cov
0%
add_ads_utilities
6.0
get_and_reset_ads_utility_registry_string
6.0
__get_ads_utilities
2.0
CRAP 3.1
1 # Copyright 2016, Google Inc. All Rights Reserved.
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 # Description: This is a global object that maintains a registry of the
16 # utilities that have recently been used. This registry of utilities is
17 # primarily collected to be put in the userAgent of the header when a
18 # request is sent. The registry is cleared every time the string representation
19 # of the registry is retrieved.
20  
21 package Google::Ads::Common::Utilities::AdsUtilityRegistry;
22  
23 use strict;
24 use warnings;
25 use utf8;
26 use version;
27  
28 # The following needs to be on one line because CPAN uses a particularly hacky
29 # eval() to determine module versions.
30 use Google::Ads::AdWords::Constants; our $VERSION = ${Google::Ads::AdWords::Constants::VERSION};
31 use Google::Ads::AdWords::Logging;
32  
33 use Class::Std::Fast;
34 use Log::Log4perl qw(:levels);
35  
36 # The Mapping of utility class names to a generic name used by all the client
37 # libraries. This string will be passed into the user agent.
38 our %ADS_UTILITIES = (
39 BatchJobHandler => "BatchJobHelper",
40 LoggingEnabled => "Logging/Enabled",
41 LoggingDisabled => "Logging/Disabled",
42 PageProcessor => "PageProcessor",
43 ReportDownloaderFile => "ReportDownloader/file",
44 ReportDownloaderStream => "ReportDownloader/stream",
45 ReportDownloaderString => "ReportDownloader/string",
46 ReportQueryBuilder => "ReportQueryBuilder",
47 ServiceQueryBuilder => "ServiceQueryBuilder"
48 );
49  
50 {
51 # This is a globally static variable.
52 my %ads_utility_registry = ();
53  
54 # Add one or more utilities to the utility registry.
55 # Example:
56 # Google::Ads::Common::Utilities::AdsUtilityRegistry->add_ads_utilities(
57 # "ReportDownloaderStream");
58 sub add_ads_utilities {
59 my ($class, @params) = @_;
60 # Verify that the value passed is a key in %ADS_UTILITIES. Don't continue
61 # if the value passed in is incorrect. Avoid duplicates with a hash.
62 foreach my $param (@params) {
63 if (exists $ADS_UTILITIES{$param}) {
64 $ads_utility_registry{$ADS_UTILITIES{$param}} = $ADS_UTILITIES{$param};
65 } else {
66 die sprintf("Parameter incorrect: %s\nChoose from values(s): %s",
67 $param, join(", ", (keys %ADS_UTILITIES)));
68 }
69 }
70 }
71  
72 # Get the list of non-repeated utilities sorted in alphabetical order.
73 sub __get_ads_utilities {
74 return sort (keys %ads_utility_registry);
75 }
76  
77 # Reset the registry of ads utilities.
78 sub __reset_ads_utilities {
79 %ads_utility_registry = ();
80 }
81  
82 # This method returns an alphabetical, comma-separated string of non-repeated
83 # ads utilities currently registered.
84 # This method also clears the utility registry.
85 sub get_and_reset_ads_utility_registry_string {
86 # Update the logging status.
87 my $log_utility =
88 ( !Google::Ads::AdWords::Logging::get_awapi_logger()->level()
89 || Google::Ads::AdWords::Logging::get_awapi_logger()->level() == $OFF)
90 ? "LoggingDisabled"
91 : "LoggingEnabled";
92 Google::Ads::Common::Utilities::AdsUtilityRegistry->add_ads_utilities(
93 $log_utility);
94  
95 # Retrieve the list and reset the utilities registry.
96 my @utilities = __get_ads_utilities();
97 __reset_ads_utilities();
98 return join(", ", @utilities);
99 }
100 }
101  
102 1;
103  
104 =pod
105  
106 =head1 NAME
107  
108 Google::Ads::Common::Utilities::AdsUtilityRegsitry
109  
110 =head1 DESCRIPTION
111  
112 This is a global object that maintains a registry of the utilities that have
113 recently been used. This registry of utilities is primarily collected to be put
114 in the userAgent of the header when a request is sent. The registry is cleared
115 every time the string representation of the registry is retrieved.
116  
117 =head2 PROPERTIES
118  
119 The following properties may be accessed using get_PROPERTY methods:
120  
121 =head1 METHODS
122  
123 =head2 add_ads_utilities
124  
125 Add one or more utilities to the utility registry. The possible values passed
126 in are in the keys of
127 Google::Ads::Common::Utilities::AdsUtilityRegistry::ADS_UTILITIES. The method
128 will error if one of the values passed in is incorrect.
129 Example:
130 Google::Ads::Common::Utilities::AdsUtilityRegistry->add_ads_utilities(
131 "ReportDownloaderStream");
132  
133 =head3 Parameters
134  
135 =over
136  
137 =item *
138  
139 One or more strings representing ad utilities. These strings can be found in
140 the keys of Google::Ads::Common::Utilities::AdsUtilityRegistry::ADS_UTILITIES.
141  
142 =back
143  
144 =head2 __get_ads_utilities
145  
146 (Private) This method returns the list of non-repeated ads utilities in
147 alphabetical order. Possible values can be found in the values of
148 Google::Ads::Common::Utilities::AdsUtilityRegistry::ADS_UTILITIES.
149  
150 =head3 Returns
151  
152 An array of non-repeated utilities that have been added via add_ads_utilities in
153 alphabetic order. Possible values can be found in the values of
154 Google::Ads::Common::Utilities::AdsUtilityRegistry::ADS_UTILITIES.
155  
156 =head2 __reset_ads_utilities
157  
158 (Private) This method clears the registry of ads utilities.
159  
160 =head2 get_and_reset_ads_utility_registry_string
161  
162 This method returns an alphabetical, comma-separated string of non-repeated ads
163 utilities currently registered.
164 This method also clears the registry of ads utilities.
165  
166 =head3 Returns
167  
168 Returns an alphabetical, comma-separated string of non-repeated ads utilities
169 currently registered.
170  
171 =cut