| 1 | package WWW::Shorten::TinyURL; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 7 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 7 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 7 | ▶ | use Carp (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | |||||||||||||||
| 7 | 7 | ▶ | use base qw( WWW::Shorten::generic Exporter ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 7 | our $_error_message = ''; | |||||||||||||
| 9 | 7 | our @EXPORT = qw( makeashorterlink makealongerlink ); | |||||||||||||
| 10 | 7 | our $VERSION = '3.093'; | |||||||||||||
| 11 | 7 | $VERSION = eval $VERSION; | |||||||||||||
| 12 | |||||||||||||||
| 13 | sub makeashorterlink { | ||||||||||||||
| 14 | 3 | ▶ | my $url = shift or Carp::croak('No URL passed to makeashorterlink'); | ||||||||||||
|
Subroutine
makeashorterlink: called CC=9 SCAR=35.9
Pod
documented
Branch: 1 branch
| |||||||||||||||
| 15 | 2 | $_error_message = ''; | |||||||||||||
| 16 | |||||||||||||||
| 17 | # terrible, bad! skip live testing for now. | ||||||||||||||
| 18 | 2 | ▶ | if ( $ENV{'WWW-SHORTEN-TESTING'} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 19 | 2 | ▶ | return 'http://tinyurl.com/abc12345' | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 20 | if ( $url eq 'https://metacpan.org/release/WWW-Shorten' ); | ||||||||||||||
| 21 | 1 | $_error_message = 'Incorrect URL for testing purposes'; | |||||||||||||
| 22 | 1 | return undef; | |||||||||||||
| 23 | } | ||||||||||||||
| 24 | |||||||||||||||
| 25 | # back to normality. | ||||||||||||||
| 26 | 0 | my $ua = __PACKAGE__->ua(); | |||||||||||||
| 27 | 0 | my $tinyurl = 'http://tinyurl.com/api-create.php'; | |||||||||||||
| 28 | 0 | my $resp | |||||||||||||
| 29 | = $ua->post($tinyurl, [url => $url, source => "PerlAPI-$VERSION",]); | ||||||||||||||
| 30 | 0 | ▶ | return undef unless $resp->is_success; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 31 | 0 | my $content = $resp->content; | |||||||||||||
| 32 | 0 | ▶ | if ($content =~ /Error/) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 33 | |||||||||||||||
| 34 | 0 | ▶ | if ($content =~ /<html/) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 35 | 0 | $_error_message = 'Error is a html page'; | |||||||||||||
| 36 | } | ||||||||||||||
| 37 | elsif (length($content) > 100) { | ||||||||||||||
| 38 | 0 | $_error_message = substr($content, 0, 100); | |||||||||||||
| 39 | } | ||||||||||||||
| 40 | else { | ||||||||||||||
| 41 | 0 | $_error_message = $content; | |||||||||||||
| 42 | } | ||||||||||||||
| 43 | 0 | return undef; | |||||||||||||
| 44 | } | ||||||||||||||
| 45 | 0 | ▶ | if ($resp->content =~ m!(\Qhttp://tinyurl.com/\E\w+)!x) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 46 | 0 | return $1; | |||||||||||||
| 47 | } | ||||||||||||||
| 48 | 0 | return; | |||||||||||||
| 49 | } | ||||||||||||||
| 50 | |||||||||||||||
| 51 | sub makealongerlink { | ||||||||||||||
| 52 | 4 | ▶ | my $url = shift | ||||||||||||
|
Subroutine
makealongerlink: called CC=9 SCAR=33.1
Pod
documented
Branch: 1 branch
| |||||||||||||||
| 53 | or Carp::croak('No TinyURL key / URL passed to makealongerlink'); | ||||||||||||||
| 54 | 3 | $_error_message = ''; | |||||||||||||
| 55 | 3 | ▶ | $url = "http://tinyurl.com/$url" | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 56 | unless $url =~ m!^http://!i; | ||||||||||||||
| 57 | |||||||||||||||
| 58 | # terrible, bad! skip live testing for now. | ||||||||||||||
| 59 | 3 | ▶ | if ( $ENV{'WWW-SHORTEN-TESTING'} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 60 | 3 | ▶ | return 'https://metacpan.org/release/WWW-Shorten' | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 61 | if ( $url eq 'http://tinyurl.com/abc12345' ); | ||||||||||||||
| 62 | 1 | $_error_message = 'Incorrect URL for testing purposes'; | |||||||||||||
| 63 | 1 | return undef; | |||||||||||||
| 64 | } | ||||||||||||||
| 65 | |||||||||||||||
| 66 | # back to normality | ||||||||||||||
| 67 | 0 | my $ua = __PACKAGE__->ua(); | |||||||||||||
| 68 | |||||||||||||||
| 69 | 0 | my $resp = $ua->get($url); | |||||||||||||
| 70 | |||||||||||||||
| 71 | 0 | ▶ | unless ($resp->is_redirect) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 72 | 0 | my $content = $resp->content; | |||||||||||||
| 73 | 0 | ▶ | if ($content =~ /Error/) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 74 | 0 | ▶ | if ($content =~ /<html/) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 75 | 0 | $_error_message = 'Error is a html page'; | |||||||||||||
| 76 | } | ||||||||||||||
| 77 | elsif (length($content) > 100) { | ||||||||||||||
| 78 | 0 | $_error_message = substr($content, 0, 100); | |||||||||||||
| 79 | } | ||||||||||||||
| 80 | else { | ||||||||||||||
| 81 | 0 | $_error_message = $content; | |||||||||||||
| 82 | } | ||||||||||||||
| 83 | } | ||||||||||||||
| 84 | else { | ||||||||||||||
| 85 | 0 | $_error_message = 'Unknown error'; | |||||||||||||
| 86 | } | ||||||||||||||
| 87 | |||||||||||||||
| 88 | 0 | return undef; | |||||||||||||
| 89 | } | ||||||||||||||
| 90 | 0 | my $long = $resp->header('Location'); | |||||||||||||
| 91 | 0 | return $long; | |||||||||||||
| 92 | } | ||||||||||||||
| 93 | |||||||||||||||
| 94 | 7 | 1; | |||||||||||||
| 95 | |||||||||||||||
| 96 | =head1 NAME | ||||||||||||||
| 97 | |||||||||||||||
| 98 | WWW::Shorten::TinyURL - Perl interface to L<http://tinyurl.com> | ||||||||||||||
| 99 | |||||||||||||||
| 100 | =head1 SYNOPSIS | ||||||||||||||
| 101 | |||||||||||||||
| 102 | use strict; | ||||||||||||||
| 103 | use warnings; | ||||||||||||||
| 104 | |||||||||||||||
| 105 | use WWW::Shorten::TinyURL; | ||||||||||||||
| 106 | use WWW::Shorten 'TinyURL'; | ||||||||||||||
| 107 | |||||||||||||||
| 108 | my $short_url = makeashorterlink('http://www.foo.com/some/long/url'); | ||||||||||||||
| 109 | my $long_url = makealongerlink($short_url); | ||||||||||||||
| 110 | |||||||||||||||
| 111 | =head1 DESCRIPTION | ||||||||||||||
| 112 | |||||||||||||||
| 113 | A Perl interface to the web site L<http://tinyurl.com>. The service simply maintains | ||||||||||||||
| 114 | a database of long URLs, each of which has a unique identifier. | ||||||||||||||
| 115 | |||||||||||||||
| 116 | =head1 Functions | ||||||||||||||
| 117 | |||||||||||||||
| 118 | =head2 makeashorterlink | ||||||||||||||
| 119 | |||||||||||||||
| 120 | The function C<makeashorterlink> will call the L<http://TinyURL.com> web site passing | ||||||||||||||
| 121 | it your long URL and will return the shorter version. | ||||||||||||||
| 122 | |||||||||||||||
| 123 | =head2 makealongerlink | ||||||||||||||
| 124 | |||||||||||||||
| 125 | The function C<makealongerlink> does the reverse. C<makealongerlink> | ||||||||||||||
| 126 | will accept as an argument either the full URL or just the identifier. | ||||||||||||||
| 127 | |||||||||||||||
| 128 | If anything goes wrong, then either function will return C<undef>. | ||||||||||||||
| 129 | |||||||||||||||
| 130 | =head2 EXPORT | ||||||||||||||
| 131 | |||||||||||||||
| 132 | makeashorterlink, makealongerlink | ||||||||||||||
| 133 | |||||||||||||||
| 134 | =head1 SUPPORT, LICENSE, THANKS and SUCH | ||||||||||||||
| 135 | |||||||||||||||
| 136 | See the main L<WWW::Shorten> docs. | ||||||||||||||
| 137 | |||||||||||||||
| 138 | =head1 AUTHOR | ||||||||||||||
| 139 | |||||||||||||||
| 140 | Iain Truskett <spoon@cpan.org> | ||||||||||||||
| 141 | |||||||||||||||
| 142 | =head1 SEE ALSO | ||||||||||||||
| 143 | |||||||||||||||
| 144 | L<WWW::Shorten>, L<http://tinyurl.com/> | ||||||||||||||
| 145 | |||||||||||||||
| 146 | =cut | ||||||||||||||