File Coverage

blib/lib/WWW/Shorten/IsGd.pm
Criterion Covered Total %
statement 32 33 96.9
branch 11 16 68.7
condition n/a
subroutine 8 8 100.0
pod 2 2 100.0
total 53 59 89.8


line stmt bran cond sub pod time code
1             package WWW::Shorten::IsGd;
2             # ABSTRACT: shorten (or lengthen) URLs with http://is.gd
3 1     1   45052 use strict;
  1         2  
  1         40  
4 1     1   6 use warnings;
  1         2  
  1         35  
5 1     1   31 use 5.006;
  1         4  
  1         68  
6             our $VERSION = '0.003'; # VERSION
7              
8              
9 1     1   10 use base qw( WWW::Shorten::generic Exporter );
  1         2  
  1         2938  
10             our @EXPORT = qw( makeashorterlink makealongerlink );
11 1     1   81288 use Carp;
  1         2  
  1         52  
12 1     1   52 use URI;
  1         5  
  1         372  
13              
14              
15             sub makeashorterlink {
16 3 100   3 1 1830 my $url = shift or croak 'No URL passed to makeashorterlink';
17 2         25 my $ua = __PACKAGE__->ua();
18 2 50       263162 my $response = $ua->post('http://is.gd/create.php', [
19             url => $url,
20             source => 'PerlAPI-' . (defined __PACKAGE__->VERSION ? __PACKAGE__->VERSION : 'dev'),
21             format => 'simple',
22             ]);
23 2 50       291517 return unless $response->is_success;
24 2         29 my $shorturl = $response->{_content};
25 2 50       14 return if $shorturl =~ m/Error/;
26 2 50       13 if ($response->content =~ m{(\Qhttp://is.gd/\E[\w_]+)}) {
27 2         84 return $1;
28             }
29 0         0 return;
30             }
31              
32              
33             sub makealongerlink {
34 3 100   3 1 1490 my $url = shift or croak 'No is.gd key/URL passed to makealongerlink';
35 2         23 my $ua = __PACKAGE__->ua();
36              
37 2 100       27 $url = "http://is.gd/$url" unless $url =~ m{^https?://}i;
38 2         14 my $response = $ua->get($url);
39              
40 2 50       50836 return unless $response->is_redirect;
41 2         30 return $response->header('Location');
42             }
43              
44             1;
45              
46              
47              
48             =pod
49              
50             =encoding utf-8
51              
52             =head1 NAME
53              
54             WWW::Shorten::IsGd - shorten (or lengthen) URLs with http://is.gd
55              
56             =head1 VERSION
57              
58             version 0.003
59              
60             =head1 SYNOPSIS
61              
62             use WWW::Shorten::IsGd;
63             use WWW::Shorten 'IsGd';
64              
65             my $url = q{http://averylong.link/wow?thats=really&really=long};
66             my $short_url = makeashorterlink($url);
67             my $long_url = makealongerlink($short_url); # eq $url
68              
69             =head1 DESCRIPTION
70              
71             A Perl interface to the web site L. is.gd simply maintains
72             a database of long URLs, each of which has a unique identifier.
73              
74             =head1 FUNCTIONS
75              
76             =head2 makeashorterlink
77              
78             The function C will call the is.gd web site passing
79             it your long URL and will return the shortened link.
80              
81             =head2 makealongerlink
82              
83             The function C does the reverse. C
84             will accept as an argument either the full TinyURL URL or just the
85             TinyURL identifier.
86              
87             If anything goes wrong, then either function will return C.
88              
89             =head1 AVAILABILITY
90              
91             The project homepage is L.
92              
93             The latest version of this module is available from the Comprehensive Perl
94             Archive Network (CPAN). Visit L to find a CPAN
95             site near you, or see L.
96              
97             The development version lives at L
98             and may be cloned from L.
99             Instead of sending patches, please fork this project using the standard
100             git and github infrastructure.
101              
102             =head1 SOURCE
103              
104             The development version is on github at L
105             and may be cloned from L
106              
107             =head1 BUGS AND LIMITATIONS
108              
109             No bugs have been reported.
110              
111             Please report any bugs or feature requests through the web interface at
112             L.
113              
114             =head1 AUTHOR
115              
116             Mike Doherty
117              
118             =head1 COPYRIGHT AND LICENSE
119              
120             This software is copyright (c) 2011 by Mike Doherty.
121              
122             This is free software; you can redistribute it and/or modify it under
123             the same terms as the Perl 5 programming language system itself.
124              
125             =cut
126              
127              
128             __END__