File Coverage

blib/lib/WWW/Shorten/SapoPuny.pm
Criterion Covered Total %
statement 32 46 69.5
branch 9 24 37.5
condition n/a
subroutine 7 7 100.0
pod 2 2 100.0
total 50 79 63.2


line stmt bran cond sub pod time code
1              
2             =encoding utf8
3              
4             =head1 NAME
5              
6             WWW::Shorten::SapoPuny - Perl interface to sl.pt
7              
8             =head1 SYNOPSIS
9              
10             use WWW::Shorten::SapoPuny;
11             use WWW::Shorten 'SapoPuny';
12              
13             $short_url = makeashorterlink($long_url);
14              
15             $long_url = makealongerlink($short_url);
16              
17             =head1 DESCRIPTION
18              
19             A Perl interface to the web site sl.pt. SapoPuny simply maintains
20             a database of long URLs, each of which has a unique identifier.
21              
22             =cut
23              
24             package WWW::Shorten::SapoPuny;
25             $WWW::Shorten::SapoPuny::VERSION = '0.02';
26 1     1   19234 use 5.006;
  1         3  
27 1     1   6 use strict;
  1         3  
  1         31  
28 1     1   5 use warnings;
  1         2  
  1         47  
29              
30 1     1   6 use base qw( WWW::Shorten::generic Exporter );
  1         1  
  1         724  
31             our @EXPORT = qw( makeashorterlink makealongerlink );
32             our $_error_message = '';
33              
34 1     1   48541 use Carp;
  1         1  
  1         329  
35              
36             =head1 Functions
37              
38             =head2 makeashorterlink
39              
40             The function C will call the SapoPuny web site passing
41             it your long URL and will return the shorter SapoPuny version.
42              
43             =cut
44              
45             #javascript:void(location.href='http://sl.pt/punify?url='+encodeURIComponent(location.href))
46              
47             sub makeashorterlink {
48 2 100   2 1 172 my $url = shift or croak 'No URL passed to makeashorterlink';
49 1         2 $_error_message = '';
50 1         10 my $ua = __PACKAGE__->ua();
51 1         12678 my $tinyurl = 'http://sl.pt/punify?url=';
52 1         6 my $resp = $ua->get( $tinyurl . $url );
53              
54 1 50       345374 return undef unless $resp->is_success;
55              
56 1         13 my $content = $resp->content;
57 1 50       21 if ( $content !~ /id="ascii"/ ) {
58 0 0       0 if ( $content =~ /
    0          
59 0         0 $_error_message = 'Error is a html page';
60             }
61             elsif ( length($content) > 100 ) {
62 0         0 $_error_message = substr( $content, 0, 100 );
63             }
64             else {
65 0         0 $_error_message = $content;
66             }
67 0         0 return undef;
68             }
69 1 50       3 if ( $resp->content =~ m!(http://[a-z0-9]+\.[a-z0-9]+\.sl\.pt)!x ) {
70 1         45 return $1;
71             }
72 0         0 return;
73             }
74              
75             =head2 makealongerlink
76              
77             The function C does the reverse. C
78             will accept as an B the full SapoPuny URL.
79              
80             If anything goes wrong, then either function will return C.
81              
82             =cut
83              
84             sub makealongerlink {
85 2 100   2 1 1009 my $tinyurl_url = shift
86             or croak 'No SapoPuny key / URL passed to makealongerlink';
87 1         3 $_error_message = '';
88 1         10 my $ua = __PACKAGE__->ua();
89              
90 1 50       12 return undef unless $tinyurl_url =~ m!http://[a-z0-9]+\.[a-z0-9]+\.sl\.pt!;
91              
92 1         7 my $resp = $ua->get($tinyurl_url);
93              
94 1 50       778368 unless ( $resp->is_redirect ) {
95 0         0 my $content = $resp->content;
96 0 0       0 if ( $content =~ /Error/ ) {
97 0 0       0 if ( $content =~ /
    0          
98 0         0 $_error_message = 'Error is a html page';
99             }
100             elsif ( length($content) > 100 ) {
101 0         0 $_error_message = substr( $content, 0, 100 );
102             }
103             else {
104 0         0 $_error_message = $content;
105             }
106             }
107             else {
108 0         0 $_error_message = 'Unknown error';
109             }
110              
111 0         0 return undef;
112             }
113 1         12 my $url = $resp->header('Location');
114 1         39 return $url;
115              
116             }
117              
118             1;
119              
120             __END__