File Coverage

blib/lib/Google/Ads/Common/Utilities/AdsUtilityRegistry.pm
Criterion Covered Total %
statement 36 36 100.0
branch 4 4 100.0
condition 2 3 66.6
subroutine 12 12 100.0
pod 2 2 100.0
total 56 57 98.2


line stmt bran cond sub pod time code
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 3     3   1652 use strict;
  3         7  
  3         83  
24 3     3   14 use warnings;
  3         6  
  3         103  
25 3     3   563 use utf8;
  3         16  
  3         24  
26 3     3   292 use version;
  3         1339  
  3         19  
27              
28             # The following needs to be on one line because CPAN uses a particularly hacky
29             # eval() to determine module versions.
30 3     3   494 use Google::Ads::AdWords::Constants; our $VERSION = ${Google::Ads::AdWords::Constants::VERSION};
  3         6  
  3         134  
31 3     3   282 use Google::Ads::AdWords::Logging;
  3         73  
  3         75  
32              
33 3     3   438 use Class::Std::Fast;
  3         10525  
  3         23  
34 3     3   411 use Log::Log4perl qw(:levels);
  3         6  
  3         27  
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             );
47              
48             {
49             # This is a globally static variable.
50             my %ads_utility_registry = ();
51              
52             # Add one or more utilities to the utility registry.
53             # Example:
54             # Google::Ads::Common::Utilities::AdsUtilityRegistry->add_ads_utilities(
55             # "ReportDownloaderStream");
56             sub add_ads_utilities {
57 15     15 1 76 my ($class, @params) = @_;
58             # Verify that the value passed is a key in %ADS_UTILITIES. Don't continue
59             # if the value passed in is incorrect. Avoid duplicates with a hash.
60 15         26 foreach my $param (@params) {
61 16 100       29 if (exists $ADS_UTILITIES{$param}) {
62 15         46 $ads_utility_registry{$ADS_UTILITIES{$param}} = $ADS_UTILITIES{$param};
63             } else {
64 1         17 die sprintf("Parameter incorrect: %s\nChoose from values(s): %s",
65             $param, join(", ", (keys %ADS_UTILITIES)));
66             }
67             }
68             }
69              
70             # Get the list of non-repeated utilities sorted in alphabetical order.
71             sub __get_ads_utilities {
72 8     8   29 return sort (keys %ads_utility_registry);
73             }
74              
75             # Reset the registry of ads utilities.
76             sub __reset_ads_utilities {
77 8     8   13 %ads_utility_registry = ();
78             }
79              
80             # This method returns an alphabetical, comma-separated string of non-repeated
81             # ads utilities currently registered.
82             # This method also clears the utility registry.
83             sub get_and_reset_ads_utility_registry_string {
84             # Update the logging status.
85 8 100 66 8 1 941 my $log_utility =
86             ( !Google::Ads::AdWords::Logging::get_awapi_logger()->level()
87             || Google::Ads::AdWords::Logging::get_awapi_logger()->level() == $OFF)
88             ? "LoggingDisabled"
89             : "LoggingEnabled";
90 8         86 Google::Ads::Common::Utilities::AdsUtilityRegistry->add_ads_utilities(
91             $log_utility);
92              
93             # Retrieve the list and reset the utilities registry.
94 8         16 my @utilities = __get_ads_utilities();
95 8         20 __reset_ads_utilities();
96 8         46 return join(", ", @utilities);
97             }
98             }
99              
100             1;
101              
102             =pod
103              
104             =head1 NAME
105              
106             Google::Ads::Common::Utilities::AdsUtilityRegsitry
107              
108             =head1 DESCRIPTION
109              
110             This is a global object that maintains a registry of the utilities that have
111             recently been used. This registry of utilities is primarily collected to be put
112             in the userAgent of the header when a request is sent. The registry is cleared
113             every time the string representation of the registry is retrieved.
114              
115             =head2 PROPERTIES
116              
117             The following properties may be accessed using get_PROPERTY methods:
118              
119             =head1 METHODS
120              
121             =head2 add_ads_utilities
122              
123             Add one or more utilities to the utility registry. The possible values passed
124             in are in the keys of
125             Google::Ads::Common::Utilities::AdsUtilityRegistry::ADS_UTILITIES. The method
126             will error if one of the values passed in is incorrect.
127             Example:
128             Google::Ads::Common::Utilities::AdsUtilityRegistry->add_ads_utilities(
129             "ReportDownloaderStream");
130              
131             =head3 Parameters
132              
133             =over
134              
135             =item *
136              
137             One or more strings representing ad utilities. These strings can be found in
138             the keys of Google::Ads::Common::Utilities::AdsUtilityRegistry::ADS_UTILITIES.
139              
140             =back
141              
142             =head2 __get_ads_utilities
143              
144             (Private) This method returns the list of non-repeated ads utilities in
145             alphabetical order. Possible values can be found in the values of
146             Google::Ads::Common::Utilities::AdsUtilityRegistry::ADS_UTILITIES.
147              
148             =head3 Returns
149              
150             An array of non-repeated utilities that have been added via add_ads_utilities in
151             alphabetic order. Possible values can be found in the values of
152             Google::Ads::Common::Utilities::AdsUtilityRegistry::ADS_UTILITIES.
153              
154             =head2 __reset_ads_utilities
155              
156             (Private) This method clears the registry of ads utilities.
157              
158             =head2 get_and_reset_ads_utility_registry_string
159              
160             This method returns an alphabetical, comma-separated string of non-repeated ads
161             utilities currently registered.
162             This method also clears the registry of ads utilities.
163              
164             =head3 Returns
165              
166             Returns an alphabetical, comma-separated string of non-repeated ads utilities
167             currently registered.
168              
169             =cut