blib/lib/WWW/Shorten/GitHub.pm

filter: Statementstmt 86.8% 33 / 38 Branchbran 50.0% 7 / 14 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 7 / 7 Podpod 0.0% 0 / 2 totaltotal 75.7% 50 / 66 CC 9 SCAR 16.0
CC
9
Cov
76%
makeashorterlink
8.7
makealongerlink
5.2
BEGIN
1.0
CRAP 5.0
↑ Summary
1 package WWW::Shorten::GitHub;
2  
3 # ABSTRACT: Shorten GitHub URLs using GitHub's URL shortener - git.io
4  
5 =head1 NAME
6  
7 WWW::Shorten::GitHub - Shorten GitHub URLs using GitHub's URL shortener - git.io
8  
9 =head1 SYNOPSIS
10  
11 This module provides a perl interface to GitHub's URL shortening service, git.io.
12  
13 It allows you to shorten any GitHub URL, and also retrieve the original URL from
14 a pre-shortened URL
15  
16 =head1 USAGE
17  
18 use WWW::Shorten 'GitHub';
19  
20 my $long_url = 'https://github.com/LoonyPandora/WWW-Shorten-GitHub';
21  
22 my $short_url = makeashorterlink($long_url);
23  
24 =cut
25  
26  
27 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 3 ▶use base qw(WWW::Shorten::generic Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 3 our @EXPORT = qw(makeashorterlink makealongerlink);
32 3 our $VERSION = '0.1.6';
33  
34 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 3 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 sub makeashorterlink {
38 1 ▶ my $url = shift or croak 'No URL passed to makeashorterlink';
Subroutine makeashorterlink: called CC=5 SCAR=21.6
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
my $url = shift() or croak("No URL passed to makeashorterlink") 0 1 1
39  
40 1 my $host = URI->new($url)->host();
41 1 ▶ if ($host !~ m/^(gist\.)?github\.com$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($host =~ /^(gist\.)?github\.com$/) 0 1 1
42 0 croak "Git.io only shortens URLs under the github.com domain";
43 }
44  
45 1 my $ua = __PACKAGE__->ua();
46 1 ▶ my $response = $ua->post('https://git.io/create', [
Branch: 1 branch
BranchTrueFalseTotal
defined __PACKAGE__->VERSION ? : 1 0 1
47 url => $url,
48 source => 'PerlAPI-' . (defined __PACKAGE__->VERSION ? __PACKAGE__->VERSION : 'dev'),
49 format => 'simple',
50 ]);
51  
52 0 ▶ if ($response->header('Status') eq '200 OK') {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->header("Status") eq "200 OK") 0 0 0
53 0 return 'http://git.io/' . $response->decoded_content;
54 }
55  
56 0 return;
57 }
58  
59  
60 sub makealongerlink {
61 2 ▶ my $token = shift or croak 'No URL / Git.io token passed to makealongerlink';
Subroutine makealongerlink: called CC=5 SCAR=16.5
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
my $token = shift() or croak("No URL / Git.io token passed to makealongerlink") 0 2 2
62  
63 2 my $url = URI->new($token);
64  
65 2 ▶ unless ($url->scheme() && $url->host() eq 'git.io') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($url->scheme and $url->host eq "git.io") 1 1 2
Condition66%2/3
$url->scheme and $url->host eq "git.io"
!ll&&!rl&&r
101
MC/DC50%1/2
$url->scheme and $url->host eq "git.io"
$url->scheme $url->host eq "git.io"
Truth table66%2/3
A and B
A= $url->schemeB= $url->host eq "git.io"
ABresult
0X0
100
111
66 1 $url->scheme('https');
67 1 $url->host('git.io');
68 1 $url->path($token);
69 }
70  
71 2 my $ua = __PACKAGE__->ua();
72 2 my $response = $ua->get($url->as_string);
73  
74 2 ▶ if ($response->is_redirect) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_redirect) 2 0 2
75 2 return $response->header('Location');
76 }
77  
78 0 return;
79 }
80  
81 3 1;
82  
83 =head1 CAVEATS
84  
85 Git.io only shortens URLs on github.com and its subdomains.
86  
87 It is not a general purpose URL shortener.
88  
89 =head1 SEE ALSO
90  
91 L<WWW::Shorten>, L<http://git.io/help>
92  
93 =head1 AUTHOR
94  
95 James Aitken <jaitken@cpan.org>
96  
97 =head1 COPYRIGHT AND LICENSE
98  
99 This software is copyright (c) 2011 by James Aitken.
100  
101 This is free software; you can redistribute it and/or modify it under
102 the same terms as the Perl 5 programming language system itself.
103  
104 =cut
↑ Summary