File Coverage

blib/lib/WWW/Shorten/TinyURL.pm
Criterion Covered Total %
statement 32 46 69.5
branch 10 24 41.6
condition n/a
subroutine 7 7 100.0
pod 2 2 100.0
total 51 79 64.5


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