| 1 | package WWW::Shorten::IsGd; | ||||||||||
| 2 | # ABSTRACT: shorten (or lengthen) URLs with http://is.gd | ||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 2 | ▶ | use 5.006; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 2 | our $VERSION = '0.003'; # VERSION | |||||||||
| 7 | |||||||||||
| 8 | |||||||||||
| 9 | 2 | ▶ | use base qw( WWW::Shorten::generic Exporter ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 2 | our @EXPORT = qw( makeashorterlink makealongerlink ); | |||||||||
| 11 | 2 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 2 | ▶ | use URI; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | |||||||||||
| 15 | sub makeashorterlink { | ||||||||||
| 16 | 1 | ▶ | my $url = shift or croak 'No URL passed to makeashorterlink'; | ||||||||
|
Subroutine
makeashorterlink: called CC=6 SCAR=27.1
Pod
documented
Branch: 1 branch
| |||||||||||
| 17 | 1 | my $ua = __PACKAGE__->ua(); | |||||||||
| 18 | 1 | ▶ | my $response = $ua->post('http://is.gd/create.php', [ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 19 | url => $url, | ||||||||||
| 20 | source => 'PerlAPI-' . (defined __PACKAGE__->VERSION ? __PACKAGE__->VERSION : 'dev'), | ||||||||||
| 21 | format => 'simple', | ||||||||||
| 22 | ]); | ||||||||||
| 23 | 1 | ▶ | return unless $response->is_success; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 24 | 0 | my $shorturl = $response->{_content}; | |||||||||
| 25 | 0 | ▶ | return if $shorturl =~ m/Error/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 26 | 0 | ▶ | if ($response->content =~ m{(\Qhttp://is.gd/\E[\w_]+)}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 27 | 0 | return $1; | |||||||||
| 28 | } | ||||||||||
| 29 | 0 | return; | |||||||||
| 30 | } | ||||||||||
| 31 | |||||||||||
| 32 | |||||||||||
| 33 | sub makealongerlink { | ||||||||||
| 34 | 0 | ▶ | my $url = shift or croak 'No is.gd key/URL passed to makealongerlink'; | ||||||||
|
Subroutine
makealongerlink: not called CC=4 SCAR=30.0
Pod
documented
Branch: 1 branch
| |||||||||||
| 35 | 0 | my $ua = __PACKAGE__->ua(); | |||||||||
| 36 | |||||||||||
| 37 | 0 | ▶ | $url = "http://is.gd/$url" unless $url =~ m{^https?://}i; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 38 | 0 | my $response = $ua->get($url); | |||||||||
| 39 | |||||||||||
| 40 | 0 | ▶ | return unless $response->is_redirect; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | 0 | return $response->header('Location'); | |||||||||
| 42 | } | ||||||||||
| 43 | |||||||||||
| 44 | 2 | 1; | |||||||||
| 45 | |||||||||||
| 46 | |||||||||||
| 47 | |||||||||||
| 48 | =pod | ||||||||||
| 49 | |||||||||||
| 50 | =encoding utf-8 | ||||||||||
| 51 | |||||||||||
| 52 | =head1 NAME | ||||||||||
| 53 | |||||||||||
| 54 | WWW::Shorten::IsGd - shorten (or lengthen) URLs with http://is.gd | ||||||||||
| 55 | |||||||||||
| 56 | =head1 VERSION | ||||||||||
| 57 | |||||||||||
| 58 | version 0.003 | ||||||||||
| 59 | |||||||||||
| 60 | =head1 SYNOPSIS | ||||||||||
| 61 | |||||||||||
| 62 | use WWW::Shorten::IsGd; | ||||||||||
| 63 | use WWW::Shorten 'IsGd'; | ||||||||||
| 64 | |||||||||||
| 65 | my $url = q{http://averylong.link/wow?thats=really&really=long}; | ||||||||||
| 66 | my $short_url = makeashorterlink($url); | ||||||||||
| 67 | my $long_url = makealongerlink($short_url); # eq $url | ||||||||||
| 68 | |||||||||||
| 69 | =head1 DESCRIPTION | ||||||||||
| 70 | |||||||||||
| 71 | A Perl interface to the web site L<http://is.gd>. is.gd simply maintains | ||||||||||
| 72 | a database of long URLs, each of which has a unique identifier. | ||||||||||
| 73 | |||||||||||
| 74 | =head1 FUNCTIONS | ||||||||||
| 75 | |||||||||||
| 76 | =head2 makeashorterlink | ||||||||||
| 77 | |||||||||||
| 78 | The function C<makeashorterlink> will call the is.gd web site passing | ||||||||||
| 79 | it your long URL and will return the shortened link. | ||||||||||
| 80 | |||||||||||
| 81 | =head2 makealongerlink | ||||||||||
| 82 | |||||||||||
| 83 | The function C<makealongerlink> does the reverse. C<makealongerlink> | ||||||||||
| 84 | will accept as an argument either the full TinyURL URL or just the | ||||||||||
| 85 | TinyURL identifier. | ||||||||||
| 86 | |||||||||||
| 87 | If anything goes wrong, then either function will return C<undef>. | ||||||||||
| 88 | |||||||||||
| 89 | =head1 AVAILABILITY | ||||||||||
| 90 | |||||||||||
| 91 | The project homepage is L<http://p3rl.org/WWW::Shorten::IsGd>. | ||||||||||
| 92 | |||||||||||
| 93 | The latest version of this module is available from the Comprehensive Perl | ||||||||||
| 94 | Archive Network (CPAN). Visit L<http://www.perl.com/CPAN/> to find a CPAN | ||||||||||
| 95 | site near you, or see L<http://search.cpan.org/dist/WWW-Shorten-IsGd/>. | ||||||||||
| 96 | |||||||||||
| 97 | The development version lives at L<http://github.com/doherty/WWW-Shorten-IsGd> | ||||||||||
| 98 | and may be cloned from L<git://github.com/doherty/WWW-Shorten-IsGd.git>. | ||||||||||
| 99 | Instead of sending patches, please fork this project using the standard | ||||||||||
| 100 | git and github infrastructure. | ||||||||||
| 101 | |||||||||||
| 102 | =head1 SOURCE | ||||||||||
| 103 | |||||||||||
| 104 | The development version is on github at L<http://github.com/doherty/WWW-Shorten-IsGd> | ||||||||||
| 105 | and may be cloned from L<git://github.com/doherty/WWW-Shorten-IsGd.git> | ||||||||||
| 106 | |||||||||||
| 107 | =head1 BUGS AND LIMITATIONS | ||||||||||
| 108 | |||||||||||
| 109 | No bugs have been reported. | ||||||||||
| 110 | |||||||||||
| 111 | Please report any bugs or feature requests through the web interface at | ||||||||||
| 112 | L<https://github.com/doherty/WWW-Shorten-IsGd/issues>. | ||||||||||
| 113 | |||||||||||
| 114 | =head1 AUTHOR | ||||||||||
| 115 | |||||||||||
| 116 | Mike Doherty <doherty@cpan.org> | ||||||||||
| 117 | |||||||||||
| 118 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 119 | |||||||||||
| 120 | This software is copyright (c) 2011 by Mike Doherty. | ||||||||||
| 121 | |||||||||||
| 122 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 123 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 124 | |||||||||||
| 125 | =cut | ||||||||||
| 126 | |||||||||||
| 127 | |||||||||||
| 128 | __END__ | ||||||||||