File Coverage

lib/Google/Ads/Common/MediaUtils.pm
Criterion Covered Total %
statement 24 32 75.0
branch 0 2 0.0
condition n/a
subroutine 8 9 88.8
pod 0 1 0.0
total 32 44 72.7


line stmt bran cond sub pod time code
1             # Copyright 2013, 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             package Google::Ads::Common::MediaUtils;
16              
17 3     3   181434 use strict;
  3         16  
  3         87  
18 3     3   18 use warnings;
  3         5  
  3         74  
19 3     3   452 use version;
  3         2720  
  3         16  
20              
21             # The following needs to be on one line because CPAN uses a particularly hacky
22             # eval() to determine module versions.
23 3     3   760 use Google::Ads::Common::Constants; our $VERSION = ${Google::Ads::Common::Constants::VERSION};
  3         5  
  3         113  
24              
25 3     3   542 use HTTP::Request;
  3         20879  
  3         102  
26 3     3   926 use LWP::UserAgent;
  3         37969  
  3         110  
27 3     3   26 use URI::Escape;
  3         10  
  3         198  
28 3     3   752 use MIME::Base64;
  3         1249  
  3         533  
29              
30             sub get_base64_data_from_url (%) {
31 0     0 0   my $url = shift;
32 0           my $request = HTTP::Request->new(GET => $url);
33 0           my $userAgent = LWP::UserAgent->new();
34 0           $userAgent->agent(sprintf("%s: %s", __PACKAGE__, $0));
35 0           my $response = $userAgent->request($request);
36 0 0         if ($response->is_success()) {
37 0           my $content = $response->content();
38 0           return encode_base64($content);
39             }
40             }
41              
42             return 1;
43              
44             =pod
45              
46             =head1 NAME
47              
48             Google::Ads::Common::ImageUtils
49              
50             =head1 SYNOPSIS
51              
52             use Google::Ads::Common::ImageUtils;
53              
54             my $image_data;
55             eval {
56             $image_data = Google::Ads::Common::ImageUtils::get_image_data_from_url({
57             url => 'https://sandbox.google.com/sandboximages/image.jpg'
58             });
59             };
60             if ($@) {
61             # $@ will contain a string explaining why the image data request failed.
62             } else {
63             # Make use of $image_data.
64             }
65              
66             =head1 DESCRIPTION
67              
68              
69             =head1 SUBROUTINES
70              
71             =head2 get_image_data_from_url
72              
73             Gets the image data (byte representation) from a given URL.
74              
75             =head3 Parameters
76              
77             The URL from which the data will be retrieved.
78              
79             =head3 Returns
80              
81             A byte array of the image data. Or no return if the image is not found.
82              
83             =head1 LICENSE AND COPYRIGHT
84              
85             Copyright 2013 Google Inc.
86              
87             Licensed under the Apache License, Version 2.0 (the "License");
88             you may not use this file except in compliance with the License.
89             You may obtain a copy of the License at
90              
91             http://www.apache.org/licenses/LICENSE-2.0
92              
93             Unless required by applicable law or agreed to in writing, software
94             distributed under the License is distributed on an "AS IS" BASIS,
95             WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
96             See the License for the specific language governing permissions and
97             limitations under the License.
98              
99             =head1 REPOSITORY INFORMATION
100              
101             $Rev: $
102             $LastChangedBy: $
103             $Id: $
104              
105             =cut