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   77603 use strict;
  3         4  
  3         66  
4 3     3   9 use warnings;
  3         3  
  3         52  
5 3     3   9 use Carp ();
  3         2  
  3         29  
6 3     3   709 use HTTP::Request ();
  3         32574  
  3         46  
7 3     3   1224 use LWP::Protocol::https ();
  3         214888  
  3         79  
8 3     3   1145 use JSON::MaybeXS ();
  3         11125  
  3         53  
9 3     3   12 use URI ();
  3         4  
  3         46  
10              
11 3     3   8 use base qw( WWW::Shorten::generic Exporter );
  3         3  
  3         858  
12             our @EXPORT = qw( makeashorterlink makealongerlink );
13             our $VERSION = '1.030_001';
14             $VERSION = eval $VERSION;
15 3     3   18539 use constant API_URL => 'https://www.googleapis.com/urlshortener/v1/url';
  3         4  
  3         142  
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   32 no strict 'refs';
  3         2  
  3         1190  
21             *{"main::getlinkstats"} = *{"WWW::Shorten::Googl::getlinkstats"};
22             }
23              
24             sub makeashorterlink {
25 6 50   6 1 4577 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 3950 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 3843 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__