| 1 | # $Id$ | ||||||||||
| 2 | |||||||||||
| 3 | =head1 NAME | ||||||||||
| 4 | |||||||||||
| 5 | WWW::Shorten::Miudin - Perl interface to miud.in | ||||||||||
| 6 | |||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||
| 8 | |||||||||||
| 9 | use WWW::Shorten::Miudin; | ||||||||||
| 10 | use WWW::Shorten 'Miudin'; | ||||||||||
| 11 | |||||||||||
| 12 | $short_url = makeashorterlink($long_url); | ||||||||||
| 13 | |||||||||||
| 14 | $long_url = makealongerlink($short_url); | ||||||||||
| 15 | |||||||||||
| 16 | =head1 DESCRIPTION | ||||||||||
| 17 | |||||||||||
| 18 | A Perl interface to the web site miud.in. Miudin simply maintains | ||||||||||
| 19 | a database of long URLs, each of which has a unique identifier. | ||||||||||
| 20 | |||||||||||
| 21 | =cut | ||||||||||
| 22 | |||||||||||
| 23 | package WWW::Shorten::Miudin; | ||||||||||
| 24 | |||||||||||
| 25 | 2 | ▶ | use 5.006; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 26 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 27 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 28 | |||||||||||
| 29 | 2 | ▶ | use base qw( WWW::Shorten::generic Exporter ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 30 | 2 | our @EXPORT = qw( makeashorterlink makealongerlink ); | |||||||||
| 31 | 2 | our $VERSION = '1.0'; | |||||||||
| 32 | |||||||||||
| 33 | 2 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 34 | |||||||||||
| 35 | =head1 Functions | ||||||||||
| 36 | |||||||||||
| 37 | =head2 makeashorterlink | ||||||||||
| 38 | |||||||||||
| 39 | The function C<makeashorterlink> will call the Miudin web site passing | ||||||||||
| 40 | it your long URL and will return the shorter Miudin version. | ||||||||||
| 41 | |||||||||||
| 42 | =cut | ||||||||||
| 43 | |||||||||||
| 44 | sub makeashorterlink ($) | ||||||||||
| 45 | 2 | ▶ | { | ||||||||
|
Subroutine
makeashorterlink: called CC=5 SCAR=23.7
Pod
documented
| |||||||||||
| 46 | 2 | ▶ | my $url = shift or croak 'No URL passed to makeashorterlink'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | 2 | my $ua = __PACKAGE__->ua(); | |||||||||
| 48 | 2 | my $tinyurl = 'http://miud.in/api-create.php'; | |||||||||
| 49 | 2 | my $resp = $ua->post($tinyurl, [ | |||||||||
| 50 | url => $url, | ||||||||||
| 51 | source => "PerlAPI-$VERSION", | ||||||||||
| 52 | ]); | ||||||||||
| 53 | 2 | ▶ | return undef unless $resp->is_success; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | 0 | my $content = $resp->content; | |||||||||
| 55 | 0 | ▶ | return undef if $content =~ /Error/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | 0 | ▶ | if ($resp->content =~ m!(\Qhttp://miud.in/\E\w+)!x) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 57 | 0 | return $1; | |||||||||
| 58 | } | ||||||||||
| 59 | 0 | return; | |||||||||
| 60 | } | ||||||||||
| 61 | |||||||||||
| 62 | =head2 makealongerlink | ||||||||||
| 63 | |||||||||||
| 64 | The function C<makealongerlink> does the reverse. C<makealongerlink> | ||||||||||
| 65 | will accept as an argument either the full Miudin URL or just the | ||||||||||
| 66 | Miudin identifier. | ||||||||||
| 67 | |||||||||||
| 68 | If anything goes wrong, then either function will return C<undef>. | ||||||||||
| 69 | |||||||||||
| 70 | =cut | ||||||||||
| 71 | |||||||||||
| 72 | sub makealongerlink ($) | ||||||||||
| 73 | 2 | ▶ | { | ||||||||
|
Subroutine
makealongerlink: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||
| 74 | 2 | ▶ | my $tinyurl_url = shift | ||||||||
|
Branch: 1 branch
| |||||||||||
| 75 | or croak 'No Miudin key / URL passed to makealongerlink'; | ||||||||||
| 76 | 1 | my $ua = __PACKAGE__->ua(); | |||||||||
| 77 | |||||||||||
| 78 | 1 | ▶ | $tinyurl_url = "http://miud.in/$tinyurl_url" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | unless $tinyurl_url =~ m!^http://!i; | ||||||||||
| 80 | |||||||||||
| 81 | 1 | my $resp = $ua->get($tinyurl_url); | |||||||||
| 82 | |||||||||||
| 83 | 1 | ▶ | return undef unless $resp->is_redirect; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | 1 | my $url = $resp->header('Location'); | |||||||||
| 85 | 1 | return $url; | |||||||||
| 86 | |||||||||||
| 87 | } | ||||||||||
| 88 | |||||||||||
| 89 | 2 | 1; | |||||||||
| 90 | |||||||||||
| 91 | __END__ | ||||||||||