File Coverage

blib/lib/WWW/Shorten/NotLong.pm
Criterion Covered Total %
statement 12 12 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 16 16 100.0


line stmt bran cond sub pod time code
1             # $Id$
2              
3             =head1 NAME
4              
5             WWW::Shorten::NotLong - Perl interface to notlong.com
6              
7             =head1 SYNOPSIS
8              
9             use WWW::Shorten 'NotLong';
10              
11             $short_url = makeashorterlink($long_url);
12             $short_url = makeashorterlink($long_url, nickname => $nickname);
13             ($short_url,$password) = makeashorterlink($long_url);
14             ($short_url,$password) = makeashorterlink($long_url, nickname => $nickname);
15              
16             $long_url = makealongerlink($short_url);
17             $long_url = makealongerlink($nickname);
18              
19             =head1 DESCRIPTION
20              
21             A Perl interface to the web site notlong.com. Notlong.com simply maintains
22             a database of long URLs, each of which has a unique identifier.
23              
24             =cut
25              
26             package WWW::Shorten::NotLong;
27              
28 3     3   117250 use 5.006;
  3         13  
  3         169  
29 3     3   21 use strict;
  3         6  
  3         110  
30 3     3   23 use warnings;
  3         11  
  3         136  
31              
32 3     3   48 use base qw( WWW::Shorten::generic Exporter );
  3         9  
  3         4831  
33             our @EXPORT = qw(makeashorterlink makealongerlink);
34             our $VERSION = '2.00';
35              
36             use Carp;
37              
38             =head1 Functions
39              
40             =head2 makeashorterlink
41              
42             The function C will call the notlong.com web site passing it
43             your long URL and will return the shorter (notlong) version. If used in a
44             list context, then it will return both the notlong URL and the password.
45              
46             If you pass a nickname, the notlong service will use your provided
47             (alpha-numeric) string as the unique identifier, provided that it has
48             not already been assigned previously.
49              
50             Note that notlong.com, unlike TinyURL and MakeAShorterLink, returns a
51             unique code for every submission.
52              
53             =cut
54              
55             sub makeashorterlink ($;%)
56             {
57             my $url = shift or croak 'No URL passed to makeashorterlink';
58             my $ua = __PACKAGE__->ua();
59             my %args = @_;
60             my $nickname = delete $args{'nickname'} || 'ws-' . $$ . int rand 100;
61             my $notlong = 'http://notlong.com/';
62             my $resp = $ua->post($notlong, [
63             url => $url,
64             nickname => $nickname,
65             ]);
66             return unless $resp->is_success;
67             if ($resp->content =~ m!
68             notlong \s+ URL:
69             .*?
70            
71             (http://[^.]+\.notlong\.com)
72            
73             .*?
74             Password:
75             \s+
76             ([-\w]+)
77             !xs) {
78             return wantarray ? ($1, $2) : $1;
79             }
80             return;
81             }
82              
83             =head2 makealongerlink
84              
85             The function C does the reverse. C
86             will accept as an argument either the full notlong URL or just the
87             notlong identifier/nickname.
88              
89             If anything goes wrong, then either function will return C.
90              
91             =cut
92              
93             sub makealongerlink ($)
94             {
95             my $notlong_url = shift
96             or croak 'No notlong nickname/URL passed to makealongerlink';
97             my $ua = __PACKAGE__->ua();
98              
99             $notlong_url = "http://$notlong_url.notlong.com/"
100             unless $notlong_url =~ m!^http://!i;
101              
102             my $resp = $ua->get($notlong_url);
103              
104             return undef unless $resp->is_redirect;
105             my $url = $resp->header('Location');
106             return $url;
107             }
108              
109             1;
110              
111             __END__