File Coverage

blib/lib/WWW/Google/URLShortener.pm
Criterion Covered Total %
statement 44 80 55.0
branch n/a
condition n/a
subroutine 16 17 94.1
pod 3 3 100.0
total 63 100 63.0


line stmt bran cond sub pod time code
1             package WWW::Google::URLShortener;
2              
3             $WWW::Google::URLShortener::VERSION = '0.19';
4              
5             =head1 NAME
6              
7             WWW::Google::URLShortener - Interface to Google URL Shortener API.
8              
9             =head1 VERSION
10              
11             Version 0.19
12              
13             =cut
14              
15 2     2   21469 use 5.006;
  2         7  
16 2     2   2066 use JSON;
  2         30728  
  2         10  
17 2     2   2238 use Data::Dumper;
  2         14310  
  2         129  
18              
19 2     2   1593 use WWW::Google::UserAgent;
  2         273550  
  2         68  
20 2     2   1254 use WWW::Google::URLShortener::Params qw(validate);
  2         10  
  2         114  
21 2     2   1373 use WWW::Google::URLShortener::Analytics;
  2         5  
  2         57  
22 2     2   1282 use WWW::Google::URLShortener::Analytics::Result;
  2         6  
  2         61  
23 2     2   1280 use WWW::Google::URLShortener::Analytics::Result::Country;
  2         4  
  2         59  
24 2     2   1267 use WWW::Google::URLShortener::Analytics::Result::Browser;
  2         6  
  2         53  
25 2     2   1325 use WWW::Google::URLShortener::Analytics::Result::Referrer;
  2         5  
  2         58  
26 2     2   1282 use WWW::Google::URLShortener::Analytics::Result::Platform;
  2         7  
  2         50  
27              
28 2     2   11 use Moo;
  2         3  
  2         8  
29 2     2   540 use namespace::clean;
  2         4  
  2         14  
30             extends 'WWW::Google::UserAgent';
31              
32             our $BASE_URL = 'https://www.googleapis.com/urlshortener/v1/url';
33              
34             =head1 DESCRIPTION
35              
36             The Google URL Shortener at goo.gl is a service that takes long URLs and squeezes
37             them into fewer characters to make a link that is easier to share, tweet or email
38             to friends. Currently it supports version v1.
39              
40             The official Google API document can be found L.
41              
42             IMPORTANT: The version v1 of Google URL Shortener API is in Labs and its features
43             might change unexpectedly until it graduates.
44              
45             =head1 CONSTRUCTOR
46              
47             The constructor expects your application API, get it for FREE from Google.
48              
49             use strict; use warnings;
50             use WWW::Google::URLShortener;
51              
52             my $api_key = 'Your_API_Key';
53             my $google = WWW::Google::URLShortener->new({ api_key => $api_key });
54              
55             =head1 METHODS
56              
57             =head2 shorten_url()
58              
59             Returns the shorten url for the given long url as provided by Google URL Shortener
60             API. This method expects one scalar parameter i.e. the long url.
61              
62             use strict; use warnings;
63             use WWW::Google::URLShortener;
64              
65             my $api_key = 'Your_API_Key';
66             my $google = WWW::Google::URLShortener->new({ api_key => $api_key });
67             print $google->shorten_url('http://www.google.com');
68              
69             =cut
70              
71             sub shorten_url {
72 2     2 1 3599 my ($self, $long_url) = @_;
73              
74 2         11 validate({ longUrl => 1 }, { longUrl => $long_url });
75              
76 0         0 my $url = sprintf("%s?key=%s", $BASE_URL, $self->api_key);
77 0         0 my $headers = { 'Content-Type' => 'application/json' };
78 0         0 my $content = to_json({ longUrl => $long_url });
79 0         0 my $response = $self->post($url, $headers, $content);
80 0         0 my $contents = from_json($response->{content});
81              
82 0         0 return $contents->{id};
83             }
84              
85             =head2 expand_url()
86              
87             Returns the expaned url for the given long url as provided by Google URL Shortener
88             API. This method expects one scalar parameter i.e. the short url.
89              
90             use strict; use warnings;
91             use WWW::Google::URLShortener;
92              
93             my $api_key = 'Your_API_Key';
94             my $google = WWW::Google::URLShortener->new({ api_key => $api_key });
95             print $google->expand_url('http://goo.gl/fbsS');
96              
97             =cut
98              
99             sub expand_url {
100 2     2 1 1919 my ($self, $short_url) = @_;
101              
102 2         9 validate({ shortUrl => 1 }, { shortUrl => $short_url });
103              
104 0         0 my $url = sprintf("%s?key=%s&shortUrl=%s", $BASE_URL, $self->api_key, $short_url);
105 0         0 my $response = $self->get($url);
106 0         0 my $content = from_json($response->{content});
107              
108 0         0 return $content->{longUrl};
109             }
110              
111             =head2 get_analytics()
112              
113             Returns the object of L.
114              
115             use strict; use warnings;
116             use WWW::Google::URLShortener;
117              
118             my $api_key = 'Your_API_Key';
119             my $google = WWW::Google::URLShortener->new({ api_key => $api_key });
120             my $analytics = $google->get_analytics('http://goo.gl/fbsS');
121              
122             =cut
123              
124             sub get_analytics {
125 2     2 1 918 my ($self, $short_url) = @_;
126              
127 2         9 validate({ shortUrl => 1 }, { shortUrl => $short_url });
128              
129 0           my $url = sprintf("%s?key=%s&shortUrl=%s&projection=FULL", $BASE_URL, $self->api_key, $short_url);
130 0           my $response = $self->get($url);
131 0           my $content = from_json($response->{content});
132              
133 0           return _analytics($content);
134             }
135              
136             sub _analytics {
137 0     0     my ($data) = @_;
138              
139 0           my $results = [];
140 0           foreach my $type (keys %{$data->{analytics}}) {
  0            
141              
142 0           my $countries = [];
143 0           foreach my $country (@{$data->{analytics}->{$type}->{countries}}) {
  0            
144 0           push @$countries, WWW::Google::URLShortener::Analytics::Result::Country->new($country);
145             }
146              
147 0           my $platforms = [];
148 0           foreach my $platform (@{$data->{analytics}->{$type}->{platforms}}) {
  0            
149 0           push @$platforms, WWW::Google::URLShortener::Analytics::Result::Platform->new($platform);
150             }
151              
152 0           my $browsers = [];
153 0           foreach my $browser (@{$data->{analytics}->{$type}->{browsers}}) {
  0            
154 0           push @$browsers, WWW::Google::URLShortener::Analytics::Result::Browser->new($browser);
155             }
156              
157 0           my $referrers = [];
158 0           foreach my $referrer (@{$data->{analytics}->{$type}->{referrers}}) {
  0            
159 0           push @$referrers, WWW::Google::URLShortener::Analytics::Result::Referrer->new($referrer);
160             }
161              
162             push @$results,
163             WWW::Google::URLShortener::Analytics::Result->new(
164             type => $type,
165             shortUrlClicks => $data->{analytics}->{$type}->{shortUrlClicks},
166             longUrlClicks => $data->{analytics}->{$type}->{longUrlClicks},
167 0           countries => $countries,
168             referrers => $referrers,
169             browsers => $browsers,
170             platforms => $platforms );
171             }
172              
173             return WWW::Google::URLShortener::Analytics->new(
174             id => $data->{id},
175             longUrl => $data->{longUrl},
176             created => $data->{created},
177             kind => $data->{kind},
178 0           result => $results );
179             }
180              
181             =head1 AUTHOR
182              
183             Mohammad S Anwar, C<< >>
184              
185             =head1 REPOSITORY
186              
187             L
188              
189             =head1 BUGS
190              
191             Please report any bugs or feature requests to C
192             rt.cpan.org>, or through the web interface at L.
193             I will be notified, and then you'll automatically be notified of progress on your
194             bug as I make changes.
195              
196             =head1 SUPPORT
197              
198             You can find documentation for this module with the perldoc command.
199              
200             perldoc WWW::Google::URLShortener
201              
202             You can also look for information at:
203              
204             =over 4
205              
206             =item * RT: CPAN's request tracker (report bugs here)
207              
208             L
209              
210             =item * AnnoCPAN: Annotated CPAN documentation
211              
212             L
213              
214             =item * CPAN Ratings
215              
216             L
217              
218             =item * Search CPAN
219              
220             L
221              
222             =back
223              
224             =head1 LICENSE AND COPYRIGHT
225              
226             Copyright (C) 2011 - 2015 Mohammad S Anwar.
227              
228             This program is free software; you can redistribute it and/or modify it under
229             the terms of the the Artistic License (2.0). You may obtain a copy of the full
230             license at:
231              
232             L
233              
234             Any use, modification, and distribution of the Standard or Modified Versions is
235             governed by this Artistic License.By using, modifying or distributing the Package,
236             you accept this license. Do not use, modify, or distribute the Package, if you do
237             not accept this license.
238              
239             If your Modified Version has been derived from a Modified Version made by someone
240             other than you,you are nevertheless required to ensure that your Modified Version
241             complies with the requirements of this license.
242              
243             This license does not grant you the right to use any trademark, service mark,
244             tradename, or logo of the Copyright Holder.
245              
246             This license includes the non-exclusive, worldwide, free-of-charge patent license
247             to make, have made, use, offer to sell, sell, import and otherwise transfer the
248             Package with respect to any patent claims licensable by the Copyright Holder that
249             are necessarily infringed by the Package. If you institute patent litigation
250             (including a cross-claim or counterclaim) against any party alleging that the
251             Package constitutes direct or contributory patent infringement,then this Artistic
252             License to you shall terminate on the date that such litigation is filed.
253              
254             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND
255             CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED
256             WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
257             NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS
258             REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT,
259             INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE
260             OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
261              
262             =cut
263              
264             1; # End of WWW::Google::URLShortener