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