File Coverage

blib/lib/WWW/Shorten/SapoPuny.pm
Criterion Covered Total %
statement 33 49 67.3
branch 9 24 37.5
condition n/a
subroutine 7 7 100.0
pod 2 2 100.0
total 51 82 62.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 xsl.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 xsl.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.03';
26 1     1   12883 use 5.006;
  1         2  
27 1     1   3 use strict;
  1         1  
  1         15  
28 1     1   3 use warnings;
  1         1  
  1         21  
29              
30 1     1   3 use base qw( WWW::Shorten::generic Exporter );
  1         1  
  1         431  
31             our @EXPORT = qw( makeashorterlink makealongerlink );
32             our $_error_message = '';
33              
34 1     1   32495 use Carp;
  1         2  
  1         341  
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://xsl.pt/punify?url='+encodeURIComponent(location.href))
46              
47             sub makeashorterlink {
48 2 100   2 1 146 my $url = shift or croak 'No URL passed to makeashorterlink';
49 1         1 $_error_message = '';
50 1         8 my $ua = __PACKAGE__->ua();
51 1         11603 my $tinyurl = 'http://xsl.pt/punify?url=';
52 1         28 print STDERR $tinyurl . $url;
53 1         7 my $resp = $ua->get( $tinyurl . $url );
54              
55 1 50       981735 unless ( $resp->is_success ) {
56 0         0 $_error_message = $resp->status_line;
57 0         0 return undef;
58             }
59              
60 1         16 my $content = $resp->content;
61 1 50       19 if ( $content !~ /id="ascii"/ ) {
62 0 0       0 if ( $content =~ /
    0          
63 0         0 $_error_message = 'Error is a html page';
64             }
65             elsif ( length($content) > 100 ) {
66 0         0 $_error_message = substr( $content, 0, 100 );
67             }
68             else {
69 0         0 $_error_message = $content;
70             }
71 0         0 return undef;
72             }
73 1 50       2 if ( $resp->content =~ m!(http://[a-z0-9]+\.[a-z0-9]+\.xsl\.pt)!x ) {
74 1         43 return $1;
75             }
76 0         0 return;
77             }
78              
79             =head2 makealongerlink
80              
81             The function C does the reverse. C
82             will accept as an B the full SapoPuny URL.
83              
84             If anything goes wrong, then either function will return C.
85              
86             =cut
87              
88             sub makealongerlink {
89 2 100   2 1 1349 my $tinyurl_url = shift
90             or croak 'No SapoPuny key / URL passed to makealongerlink';
91 1         3 $_error_message = '';
92 1         7 my $ua = __PACKAGE__->ua();
93              
94 1 50       9 return undef unless $tinyurl_url =~ m!http://[a-z0-9]+\.[a-z0-9]+\.xsl\.pt!;
95              
96 1         4 my $resp = $ua->get($tinyurl_url);
97              
98 1 50       830410 unless ( $resp->is_redirect ) {
99 0         0 my $content = $resp->content;
100 0 0       0 if ( $content =~ /Error/ ) {
101 0 0       0 if ( $content =~ /
    0          
102 0         0 $_error_message = 'Error is a html page';
103             }
104             elsif ( length($content) > 100 ) {
105 0         0 $_error_message = substr( $content, 0, 100 );
106             }
107             else {
108 0         0 $_error_message = $content;
109             }
110             }
111             else {
112 0         0 $_error_message = 'Unknown error';
113             }
114              
115 0         0 return undef;
116             }
117 1         12 my $url = $resp->header('Location');
118 1         36 return $url;
119              
120             }
121              
122             1;
123              
124             __END__