File Coverage

blib/lib/WWW/Shorten/NotLong.pm
Criterion Covered Total %
statement 15 32 46.9
branch 0 14 0.0
condition 0 3 0.0
subroutine 5 7 71.4
pod 2 2 100.0
total 22 58 37.9


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 1     1   1522 use 5.006;
  1         2  
  1         22  
29 1     1   2 use strict;
  1         1  
  1         14  
30 1     1   2 use warnings;
  1         0  
  1         18  
31              
32 1     1   2 use base qw( WWW::Shorten::generic Exporter );
  1         1  
  1         190  
33             our @EXPORT = qw(makeashorterlink makealongerlink);
34             our $VERSION = '1.90';
35              
36 1     1   4 use Carp;
  1         0  
  1         211  
37              
38             =head1 Functions
39            
40             =head2 makeashorterlink
41            
42             The function C<makeashorterlink> 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 0 0   0 1       my $url = shift or croak 'No URL passed to makeashorterlink';
58 0               my $ua = __PACKAGE__->ua();
59 0               my %args = @_;
60 0   0           my $nickname = delete $args{'nickname'} || 'ws-' . $$ . int rand 100;
61 0               my $notlong = 'http://notlong.com/';
62 0               my $resp = $ua->post($notlong, [
63             url => $url,
64             nickname => $nickname,
65             ]);
66 0 0             return unless $resp->is_success;
67 0 0             if ($resp->content =~ m!
68             notlong \s+ URL:
69             .*?
70             <a \s+ href="[^"]+">
71             (http://[^.]+\.notlong\.com)
72             </a>
73             .*?
74             Password:
75             \s+
76             ([-\w]+)
77             !xs) {
78 0 0         return wantarray ? ($1, $2) : $1;
79                 }
80 0               return;
81             }
82              
83             =head2 makealongerlink
84            
85             The function C<makealongerlink> does the reverse. C<makealongerlink>
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<undef>.
90            
91             =cut
92              
93             sub makealongerlink ($)
94             {
95 0 0   0 1       my $notlong_url = shift
96             or croak 'No notlong nickname/URL passed to makealongerlink';
97 0               my $ua = __PACKAGE__->ua();
98              
99 0 0             $notlong_url = "http://$notlong_url.notlong.com/"
100                 unless $notlong_url =~ m!^http://!i;
101              
102 0               my $resp = $ua->get($notlong_url);
103              
104 0 0             return undef unless $resp->is_redirect;
105 0               my $url = $resp->header('Location');
106 0               return $url;
107             }
108              
109             1;
110              
111             __END__
112            
113             =head2 EXPORT
114            
115             makeashorterlink, makealongerlink
116            
117             =head1 SUPPORT, LICENCE, THANKS and SUCH
118            
119             See the main L<WWW::Shorten> docs.
120            
121             =head1 AUTHOR
122            
123             Eric Hammond <ehammond@thinksome.com>
124            
125             Based almost entirely on WWW::Shorten::Shorl by Iain Truskett <spoon@cpan.org>
126             which was based on WWW::MakeAShorterLink by Dave Cross <dave@dave.org.uk>
127            
128             =head1 SEE ALSO
129            
130             L<WWW::Shorten>, L<perl>, L<http://notlong.com/>
131            
132             =cut
133