File Coverage

blib/lib/WWW/Shorten/TinyURL.pm
Criterion Covered Total %
statement 25 52 48.0
branch 12 32 37.5
condition n/a
subroutine 6 6 100.0
pod 2 2 100.0
total 45 92 48.9


line stmt bran cond sub pod time code
1             package WWW::Shorten::TinyURL;
2              
3 7     7   34914 use strict;
  7         11  
  7         181  
4 7     7   47 use warnings;
  7         7  
  7         175  
5 7     7   22 use Carp ();
  7         6  
  7         113  
6              
7 7     7   25 use base qw( WWW::Shorten::generic Exporter );
  7         10  
  7         3300  
8             our $_error_message = '';
9             our @EXPORT = qw( makeashorterlink makealongerlink );
10             our $VERSION = '3.093';
11             $VERSION = eval $VERSION;
12              
13             sub makeashorterlink {
14 3 100   3 1 1140 my $url = shift or Carp::croak('No URL passed to makeashorterlink');
15 2         4 $_error_message = '';
16              
17             # terrible, bad! skip live testing for now.
18 2 50       6 if ( $ENV{'WWW-SHORTEN-TESTING'} ) {
19 2 100       5 return 'http://tinyurl.com/abc12345'
20             if ( $url eq 'https://metacpan.org/release/WWW-Shorten' );
21 1         2 $_error_message = 'Incorrect URL for testing purposes';
22 1         2 return undef;
23             }
24              
25             # back to normality.
26 0         0 my $ua = __PACKAGE__->ua();
27 0         0 my $tinyurl = 'http://tinyurl.com/api-create.php';
28 0         0 my $resp
29             = $ua->post($tinyurl, [url => $url, source => "PerlAPI-$VERSION",]);
30 0 0       0 return undef unless $resp->is_success;
31 0         0 my $content = $resp->content;
32 0 0       0 if ($content =~ /Error/) {
33              
34 0 0       0 if ($content =~ /
    0          
35 0         0 $_error_message = 'Error is a html page';
36             }
37             elsif (length($content) > 100) {
38 0         0 $_error_message = substr($content, 0, 100);
39             }
40             else {
41 0         0 $_error_message = $content;
42             }
43 0         0 return undef;
44             }
45 0 0       0 if ($resp->content =~ m!(\Qhttp://tinyurl.com/\E\w+)!x) {
46 0         0 return $1;
47             }
48 0         0 return;
49             }
50              
51             sub makealongerlink {
52 4 100   4 1 3380 my $url = shift
53             or Carp::croak('No TinyURL key / URL passed to makealongerlink');
54 3         5 $_error_message = '';
55 3 100       13 $url = "http://tinyurl.com/$url"
56             unless $url =~ m!^http://!i;
57              
58             # terrible, bad! skip live testing for now.
59 3 50       7 if ( $ENV{'WWW-SHORTEN-TESTING'} ) {
60 3 100       9 return 'https://metacpan.org/release/WWW-Shorten'
61             if ( $url eq 'http://tinyurl.com/abc12345' );
62 1         2 $_error_message = 'Incorrect URL for testing purposes';
63 1         1 return undef;
64             }
65              
66             # back to normality
67 0           my $ua = __PACKAGE__->ua();
68              
69 0           my $resp = $ua->get($url);
70              
71 0 0         unless ($resp->is_redirect) {
72 0           my $content = $resp->content;
73 0 0         if ($content =~ /Error/) {
74 0 0         if ($content =~ /
    0          
75 0           $_error_message = 'Error is a html page';
76             }
77             elsif (length($content) > 100) {
78 0           $_error_message = substr($content, 0, 100);
79             }
80             else {
81 0           $_error_message = $content;
82             }
83             }
84             else {
85 0           $_error_message = 'Unknown error';
86             }
87              
88 0           return undef;
89             }
90 0           my $long = $resp->header('Location');
91 0           return $long;
92             }
93              
94             1;
95              
96             =head1 NAME
97              
98             WWW::Shorten::TinyURL - Perl interface to L
99              
100             =head1 SYNOPSIS
101              
102             use strict;
103             use warnings;
104              
105             use WWW::Shorten::TinyURL;
106             use WWW::Shorten 'TinyURL';
107              
108             my $short_url = makeashorterlink('http://www.foo.com/some/long/url');
109             my $long_url = makealongerlink($short_url);
110              
111             =head1 DESCRIPTION
112              
113             A Perl interface to the web site L. The service simply maintains
114             a database of long URLs, each of which has a unique identifier.
115              
116             =head1 Functions
117              
118             =head2 makeashorterlink
119              
120             The function C will call the L web site passing
121             it your long URL and will return the shorter version.
122              
123             =head2 makealongerlink
124              
125             The function C does the reverse. C
126             will accept as an argument either the full URL or just the identifier.
127              
128             If anything goes wrong, then either function will return C.
129              
130             =head2 EXPORT
131              
132             makeashorterlink, makealongerlink
133              
134             =head1 SUPPORT, LICENSE, THANKS and SUCH
135              
136             See the main L docs.
137              
138             =head1 AUTHOR
139              
140             Iain Truskett
141              
142             =head1 SEE ALSO
143              
144             L, L
145              
146             =cut