blib/lib/WWW/Shorten/Googl.pm

filter: Statementstmt 61.4% 43 / 70 Branchbran 8.3% 2 / 24 Conditioncond 16.6% 1 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 78.5% 11 / 14 Podpod 100% 3 / 3 totaltotal 49.5% 60 / 121 SCAR 39.2
CC
15
Cov
46%
_authorize
30.0
makealongerlink
20.0
getlinkstats
12.0
CRAP 50.4
↑ Summary
1  
2 package WWW::Shorten::Googl;
3  
4 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use base qw( WWW::Shorten::generic Exporter );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 our @EXPORT = qw( makeashorterlink makealongerlink );
10 2 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 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 *{"main::getlinkstats"} = *{"WWW::Shorten::Googl::getlinkstats"};
18 }
19  
20 2 ▶use JSON::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 2 ▶use constant API_URL => 'https://www.googleapis.com/urlshortener/v1/url';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 use constant HISTORY_URL =>
25 2 ▶ 'https://www.googleapis.com/urlshortener/v1/url/history';
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 sub makeashorterlink ($) {
28 1 ▶ my $url = shift or croak 'No URL passed to makeashorterlink';
Subroutine makeashorterlink: called CC=3 SCAR=14.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
my $url = shift() or croak("No URL passed to makeashorterlink") 0 1 1
29  
30 1 my $json = JSON::Any->new;
31 1 my $content = $json->objToJson( { longUrl => $url, } );
32  
33 1 my $res = _request( 'post', API_URL, Content => $content );
34 0 ▶ return $res->{id} if ( $res->{id} );
Branch: 1 branch
BranchTrueFalseTotal
if $res->{"id"} 0 0 0
35 0 return undef;
36 }
37  
38 sub makealongerlink ($) {
39 0 ▶ my $url = shift
Subroutine makealongerlink: not called CC=4 SCAR=30.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
my $url = shift() or croak("No goo.gl key / URL passed to makealongerlink") 0 0 0
40 or croak 'No goo.gl key / URL passed to makealongerlink';
41  
42 0 ▶ $url = "http://goo.gl/$url"
Branch: 1 branch
BranchTrueFalseTotal
unless $url =~ m[^http://]i 0 0 0
43 unless $url =~ m!^http://!i;
44  
45 0 my $res = _request( 'get', API_URL . '?shortUrl=' . $url );
46 0 ▶ return $res->{longUrl} if ( $res->{longUrl} );
Branch: 1 branch
BranchTrueFalseTotal
if $res->{"longUrl"} 0 0 0
47 0 return undef;
48 }
49  
50 sub getlinkstats ($) {
51 0 ▶ my $url = shift
Subroutine getlinkstats: not called CC=3 SCAR=24.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
my $url = shift() or croak("No goo.gl key / URL passed to makealongerlink") 0 0 0
52 or croak 'No goo.gl key / URL passed to makealongerlink';
53  
54 0 ▶ $url = "http://goo.gl/$url"
Branch: 1 branch
BranchTrueFalseTotal
unless $url =~ m[^http://]i 0 0 0
55 unless $url =~ m!^http://!i;
56  
57 0 my $res = _request( 'get', API_URL . '?projection=FULL&shortUrl=' . $url );
58 0 return $res;
59 }
60  
61 sub _request {
62 1 ▶ my ( $method, $url, @args ) = @_;
Subroutine _request: called CC=4 SCAR=19.1
63  
64 1 my $ua = __PACKAGE__->ua();
65 1 my %headers = ();
66 1 ▶ if ( $ENV{GOOGLE_USERNAME} && $ENV{GOOGLE_PASSWORD} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"GOOGLE_USERNAME"} and $ENV{"GOOGLE_PASSWORD"}) 0 1 1
Condition33%1/3
$ENV{"GOOGLE_USERNAME"} and $ENV{"GOOGLE_PASSWORD"}
!ll&&!rl&&r
100
MC/DC0%0/2
$ENV{"GOOGLE_USERNAME"} and $ENV{"GOOGLE_PASSWORD"}
$ENV{"GOOGLE_USERNAME"} $ENV{"GOOGLE_PASSWORD"}
Truth table33%1/3
A and B
A= $ENV{"GOOGLE_USERNAME"}B= $ENV{"GOOGLE_PASSWORD"}
ABresult
0X0
100
111
67 $headers{Authorization} =
68 0 _authorize( $ENV{GOOGLE_USERNAME}, $ENV{GOOGLE_PASSWORD} );
69 }
70 1 $headers{'Content-Type'} = 'application/json';
71  
72 1 my $resp = $ua->$method( $url, %headers, @args );
73 0 ▶ die "Request failed - " . $resp->status_line unless $resp->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $resp->is_success 0 0 0
74  
75 0 my $json = JSON::Any->new;
76 0 my $obj = $json->jsonToObj( $resp->content );
77 0 return $obj;
78 }
79  
80 sub _authorize {
81 0 ▶ my ( $username, $password ) = @_;
Subroutine _authorize: not called CC=5 SCAR=34.0
82  
83 0 eval "require Net::Google::AuthSub";
84 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
85 0 die "You need to install Net::Google::AuthSub to enable URL tracking";
86 }
87  
88 0 my $auth = Net::Google::AuthSub->new(
89 service => 'urlshortener',
90 source => 'perl/www-shorten-googl',
91 );
92 0 my $res = $auth->login( $username, $password );
93 0 ▶ unless ( $res and $res->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res and $res->is_success) 0 0 0
Condition0%0/3
$res and $res->is_success
!ll&&!rl&&r
000
MC/DC0%0/2
$res and $res->is_success
$res $res->is_success
Truth table0%0/3
A and B
A= $resB= $res->is_success
ABresult
0X0
100
111
94 0 die "Authentication failed - " . $res->error;
95 }
96 0 ▶ unless ( $auth->authorized ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($auth->authorized) 0 0 0
97 0 die "Not authorized";
98 }
99 0 return 'GoogleLogin auth=' . $auth->auth_token;
100 }
101  
102 2 1;
103  
104 __END__
↑ Summary