File Coverage

blib/lib/WWW/Shorten/Googl.pm
Criterion Covered Total %
statement 12 12 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 16 16 100.0


line stmt bran cond sub pod time code
1              
2             package WWW::Shorten::Googl;
3              
4 2     2   46903 use 5.006;
  2         8  
  2         76  
5 2     2   12 use strict;
  2         2  
  2         73  
6 2     2   9 use warnings;
  2         9  
  2         64  
7              
8 2     2   11 use base qw( WWW::Shorten::generic Exporter );
  2         2  
  2         2974  
9             our @EXPORT = qw( makeashorterlink makealongerlink );
10             our $VERSION = '1.02';
11              
12             {
13              
14             # As docs advice you use this module as "use WWW::Shorten 'Googl'"
15             # that module takes care of the importing.. so let's hack this in here
16             no strict 'refs';
17             *{"main::getlinkstats"} = *{"WWW::Shorten::Googl::getlinkstats"};
18             }
19              
20             use JSON::Any;
21             use Carp;
22              
23             use constant API_URL => 'https://www.googleapis.com/urlshortener/v1/url';
24             use constant HISTORY_URL =>
25             'https://www.googleapis.com/urlshortener/v1/url/history';
26              
27             sub makeashorterlink ($) {
28             my $url = shift or croak 'No URL passed to makeashorterlink';
29              
30             my $json = JSON::Any->new;
31             my $content = $json->objToJson( { longUrl => $url, } );
32              
33             my $res = _request( 'post', API_URL, Content => $content );
34             return $res->{id} if ( $res->{id} );
35             return undef;
36             }
37              
38             sub makealongerlink ($) {
39             my $url = shift
40             or croak 'No goo.gl key / URL passed to makealongerlink';
41              
42             $url = "http://goo.gl/$url"
43             unless $url =~ m!^http://!i;
44              
45             my $res = _request( 'get', API_URL . '?shortUrl=' . $url );
46             return $res->{longUrl} if ( $res->{longUrl} );
47             return undef;
48             }
49              
50             sub getlinkstats ($) {
51             my $url = shift
52             or croak 'No goo.gl key / URL passed to makealongerlink';
53              
54             $url = "http://goo.gl/$url"
55             unless $url =~ m!^http://!i;
56              
57             my $res = _request( 'get', API_URL . '?projection=FULL&shortUrl=' . $url );
58             return $res;
59             }
60              
61             sub _request {
62             my ( $method, $url, @args ) = @_;
63              
64             my $ua = __PACKAGE__->ua();
65             my %headers = ();
66             if ( $ENV{GOOGLE_USERNAME} && $ENV{GOOGLE_PASSWORD} ) {
67             $headers{Authorization} =
68             _authorize( $ENV{GOOGLE_USERNAME}, $ENV{GOOGLE_PASSWORD} );
69             }
70             $headers{'Content-Type'} = 'application/json';
71              
72             my $resp = $ua->$method( $url, %headers, @args );
73             die "Request failed - " . $resp->status_line unless $resp->is_success;
74              
75             my $json = JSON::Any->new;
76             my $obj = $json->jsonToObj( $resp->content );
77             return $obj;
78             }
79              
80             sub _authorize {
81             my ( $username, $password ) = @_;
82              
83             eval "require Net::Google::AuthSub";
84             if ($@) {
85             die "You need to install Net::Google::AuthSub to enable URL tracking";
86             }
87              
88             my $auth = Net::Google::AuthSub->new(
89             service => 'urlshortener',
90             source => 'perl/www-shorten-googl',
91             );
92             my $res = $auth->login( $username, $password );
93             unless ( $res and $res->is_success ) {
94             die "Authentication failed - " . $res->error;
95             }
96             unless ( $auth->authorized ) {
97             die "Not authorized";
98             }
99             return 'GoogleLogin auth=' . $auth->auth_token;
100             }
101              
102             1;
103              
104             __END__