| 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
| |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 1 | my $host = URI->new($url)->host(); | |||||||||||||||||||||||||||
| 41 | 1 | ▶ | if ($host !~ m/^(gist\.)?github\.com$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 2 | my $url = URI->new($token); | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 2 | ▶ | unless ($url->scheme() && $url->host() eq 'git.io') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$url->scheme and $url->host eq "git.io"
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"
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||