File Coverage

blib/lib/Google/Ads/GoogleAds/Utils/MediaUtils.pm
Criterion Covered Total %
statement 31 31 100.0
branch 1 2 50.0
condition n/a
subroutine 10 10 100.0
pod 1 1 100.0
total 43 44 97.7


line stmt bran cond sub pod time code
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 contains utility methods for media processing.
16              
17              
18             use strict;
19 2     2   1533 use warnings;
  2         5  
  2         56  
20 2     2   8 use version;
  2         4  
  2         53  
21 2     2   362  
  2         1723  
  2         9  
22             # The following needs to be on one line because CPAN uses a particularly hacky
23             # eval() to determine module versions.
24             use Google::Ads::GoogleAds::Constants; our $VERSION = ${Google::Ads::GoogleAds::Constants::VERSION};
25 2     2   420  
  2         4  
  2         73  
26             use Exporter 'import';
27 2     2   9 our @EXPORT = qw(get_base64_data_from_url);
  2         4  
  2         86  
28              
29             use HTTP::Request;
30 2     2   375 use LWP::UserAgent;
  2         18939  
  2         65  
31 2     2   574 use URI::Escape;
  2         30474  
  2         55  
32 2     2   24 use MIME::Base64;
  2         4  
  2         112  
33 2     2   643  
  2         572  
  2         212  
34             # Gets the image data (byte representation) from a given URL.
35             my $url = shift;
36             my $request = HTTP::Request->new(GET => $url);
37 2     2 1 1144 my $response = LWP::UserAgent->new->request($request);
38 2         13 return encode_base64($response->content, '') if $response->is_success;
39 2         7664 }
40 2 50       180482  
41             1;
42              
43             =pod
44              
45             =head1 NAME
46              
47             Google::Ads::GoogleAds::Utils::MediaUtils
48              
49             =head1 SYNOPSIS
50              
51             use Google::Ads::GoogleAds::Utils::MediaUtils;
52              
53             my $image_data = get_base64_data_from_url("https://gaagl.page.link/Eit5");
54              
55             if ($image_data) {
56             # Make use of $image_data.
57             }
58              
59             =head1 DESCRIPTION
60              
61             This module contains utility methods for media processing.
62              
63             =head1 METHODS
64              
65             =head2 get_base64_data_from_url
66              
67             Gets the image data (byte representation) from a given URL.
68              
69             =head3 Parameters
70              
71             The URL from which the data will be retrieved.
72              
73             =head3 Returns
74              
75             A base64 encoded string of the image data. Or undef if the image is not found.
76              
77             =head1 LICENSE AND COPYRIGHT
78              
79             Copyright 2019 Google LLC
80              
81             Licensed under the Apache License, Version 2.0 (the "License");
82             you may not use this file except in compliance with the License.
83             You may obtain a copy of the License at
84              
85             http://www.apache.org/licenses/LICENSE-2.0
86              
87             Unless required by applicable law or agreed to in writing, software
88             distributed under the License is distributed on an "AS IS" BASIS,
89             WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
90             See the License for the specific language governing permissions and
91             limitations under the License.
92              
93             =head1 REPOSITORY INFORMATION
94              
95             $Rev: $
96             $LastChangedBy: $
97             $Id: $
98              
99             =cut