File Coverage

blib/lib/WWW/Shorten/Digg.pm
Criterion Covered Total %
statement 25 41 60.9
branch 1 14 7.1
condition n/a
subroutine 8 9 88.8
pod 2 2 100.0
total 36 66 54.5


line stmt bran cond sub pod time code
1             # $Id$
2            
3             =head1 NAME
4            
5             WWW::Shorten::Digg - Perl interface to digg.com
6            
7             =head1 SYNOPSIS
8            
9             use WWW::Shorten::Digg;
10             use WWW::Shorten 'Digg';
11            
12             $short_url = makeashorterlink($long_url);
13            
14             $long_url = makealongerlink($short_url);
15            
16             =head1 DESCRIPTION
17            
18             A Perl interface to the web site digg.com. Digg maintains
19             a database of long URLs, each of which has a unique identifier.
20            
21             =cut
22            
23             package WWW::Shorten::Digg;
24            
25 1     1   33273 use 5.006;
  1         3  
  1         36  
26 1     1   5 use strict;
  1         2  
  1         34  
27 1     1   5 use warnings;
  1         6  
  1         34  
28 1     1   916 use URI::Escape;
  1         6997  
  1         636  
29 1     1   2636 use HTML::Entities;
  1         17915  
  1         117  
30            
31 1     1   8 use base qw( WWW::Shorten::generic Exporter );
  1         2  
  1         1299  
32             our @EXPORT = qw( makeashorterlink makealongerlink );
33             our $VERSION = '0.01';
34            
35 1     1   60750 use Carp;
  1         3  
  1         394  
36            
37             =head1 Functions
38            
39             =head2 makeashorterlink
40            
41             The function C will call the Digg web site passing
42             it your long URL and will return the shorter Digg version.
43            
44             =cut
45            
46             sub makeashorterlink ($)
47             {
48 1 50   1 1 15 my $url = uri_escape(shift) or croak 'No URL passed to makeashorterlink';
49 1         80 my $ua = __PACKAGE__->ua();
50 1         22521 my $diggurl = "http://services.digg.com/url/short/create?url=$url&appkey=http%3A%2F%2Fsearch.cpan.org%2Fsearch%3Fquery%3DWWW-Shorten-Digg&type=xml";
51 1         7 my $resp = $ua->get($diggurl);
52 0 0         return undef unless $resp->is_success;
53 0           my $content = $resp->content;
54 0 0         if ($resp->content =~ m!short_url="(\Qhttp://digg.com/\E\w+)"!x) {
55 0           return $1;
56             }
57 0           return;
58             }
59            
60             =head2 makealongerlink
61            
62             The function C does the reverse. C
63             will accept as an argument either the full Digg URL or just the
64             Digg identifier.
65            
66             If anything goes wrong, then either function will return C.
67            
68             =cut
69            
70             sub makealongerlink ($)
71             {
72 0 0   0 1   my $short = uri_escape(shift) or croak 'No Digg key / URL passed to makealongerlink';
73 0 0         if ($short =~ m!\Qhttp%3A%2F%2Fdigg.com%2F\E(\w+)!x) {
74 0           $short = $1;
75             }
76 0           my $ua = __PACKAGE__->ua();
77 0           my $diggurl = "http://services.digg.com/url/short/$short?appkey=http%3A%2F%2Fsearch.cpan.org%2Fsearch%3Fquery%3DWWW-Shorten-Digg&type=xml";
78 0           my $resp = $ua->get($diggurl);
79 0 0         return undef unless $resp->is_success;
80 0           my $content = $resp->content;
81 0 0         if ($resp->content =~ m!link="(.*?)"!x) {
82 0           return decode_entities($1, '<&>"');
83             }
84 0           return;
85             }
86            
87             1;
88            
89             __END__