File Coverage

blib/lib/WWW/Shorten/Googl.pm
Criterion Covered Total %
statement 33 65 50.7
branch 3 30 10.0
condition n/a
subroutine 13 15 86.6
pod 3 3 100.0
total 52 113 46.0


line stmt bran cond sub pod time code
1             package WWW::Shorten::Googl;
2              
3 3     3   86125 use strict;
  3         3  
  3         67  
4 3     3   9 use warnings;
  3         4  
  3         54  
5 3     3   8 use Carp ();
  3         3  
  3         29  
6 3     3   737 use HTTP::Request ();
  3         33192  
  3         44  
7 3     3   1197 use LWP::Protocol::https ();
  3         216609  
  3         77  
8 3     3   1213 use JSON::MaybeXS ();
  3         11718  
  3         54  
9 3     3   13 use URI ();
  3         3  
  3         45  
10              
11 3     3   9 use base qw( WWW::Shorten::generic Exporter );
  3         3  
  3         880  
12             our @EXPORT = qw( makeashorterlink makealongerlink );
13             our $VERSION = '1.100';
14             $VERSION = eval $VERSION;
15 3     3   19178 use constant API_URL => 'https://www.googleapis.com/urlshortener/v1/url';
  3         4  
  3         154  
16              
17             {
18             # As docs advice you use this module as "use WWW::Shorten 'Googl'"
19             # that module takes care of the importing.. so let's hack this in here
20 3     3   34 no strict 'refs';
  3         3  
  3         1148  
21             *{"main::getlinkstats"} = *{"WWW::Shorten::Googl::getlinkstats"};
22             }
23              
24             sub makeashorterlink {
25 6 50   6 1 3305 my $url = shift or Carp::croak('No URL passed to makeashorterlink');
26              
27 0         0 my $request = HTTP::Request->new('POST', URI->new(API_URL));
28 0         0 $request->content(JSON::MaybeXS::encode_json({longUrl => $url}));
29 0 0       0 if (my $res = _req($request)) {
30 0 0       0 return $res->{id} if ($res->{id});
31 0         0 Carp::croak("Couldn't find the shorter URL");
32             }
33 0         0 Carp::croak("Unable to get a response");
34             }
35              
36             sub makealongerlink {
37 6 50   6 1 2831 my $url = shift or Carp::croak('No URL passed to makealongerlink');
38 0 0       0 $url = "http://goo.gl/$url" unless $url =~ m!^http://!i;
39              
40 0         0 my $endpoint = URI->new(API_URL);
41 0         0 $endpoint->query_form(shortUrl => $url);
42 0         0 my $request = HTTP::Request->new('GET', $endpoint);
43              
44 0 0       0 if (my $res = _req($request)) {
45 0 0       0 return $res->{longUrl} if ($res->{longUrl});
46 0         0 Carp::croak("Couldn't find the longer URL");
47             }
48 0         0 Carp::croak("Unable to get a response");
49             }
50              
51             sub getlinkstats {
52 6 50   6 1 2667 my $url = shift or Carp::croak('No URL passed to getlinkstats');
53 0 0         $url = "http://goo.gl/$url" unless $url =~ m!^http://!i;
54              
55 0           my $endpoint = URI->new(API_URL);
56 0           $endpoint->query_form(projection => 'FULL', shortUrl => $url);
57 0           my $request = HTTP::Request->new('GET', $endpoint);
58              
59 0 0         if (my $res = _req($request)) {
60 0           return $res;
61             }
62 0           Carp::croak("Unable to get a response");
63             }
64              
65 0 0   0     sub _api_key { $ENV{GOOGLE_API_KEY} || '' }
66              
67             sub _req {
68 0 0   0     my $request = shift or Carp::croak("Expected an HTTP::Request object");
69 0           $request->header('Content-Type' => 'application/json');
70 0           $request->uri->query_form($request->uri->query_form(), key => _api_key());
71              
72 0           my $ua = __PACKAGE__->ua();
73              
74 0 0         if (my $res = $ua->request($request)) {
75 0 0         if ($res->is_success()) {
76 0 0         Carp::croak("Couldn't parse JSON response")
77             unless (my $data = JSON::MaybeXS::decode_json($res->content));
78 0           return $data;
79             }
80             else {
81 0           Carp::croak("Request failed - " . $res->status_line);
82             }
83             }
84             else {
85 0           Carp::croak("Unable to get response");
86             }
87             }
88              
89             1;
90              
91             __END__