File Coverage

lib/WWW/Lengthen.pm
Criterion Covered Total %
statement 9 50 18.0
branch 0 24 0.0
condition 0 6 0.0
subroutine 3 8 37.5
pod 4 4 100.0
total 16 92 17.3


line stmt bran cond sub pod time code
1             package WWW::Lengthen;
2              
3 1     1   41014 use strict;
  1         3  
  1         40  
4 1     1   5 use warnings;
  1         2  
  1         29  
5 1     1   2790 use LWP::UserAgent;
  1         59400  
  1         1405  
6              
7             our $VERSION = '0.08';
8              
9             our %KnownServices = (
10             '0rz' => qr{^http://0rz\.tw/.+},
11             Metamark => qr{^http://xrl\.us/.+},
12             SnipURL => qr{^http://snipurl\.com/.+},
13             TinyURL => qr{^http://tinyurl\.com/.+},
14             snurl => qr{^http://snurl\.com/.+},
15             bitly => qr{^http://bit\.ly/.+},
16             htly => qr{^http://ht\.ly/.+},
17             isgd => qr{^http://is\.gd/.+},
18             owly => qr{^http://ow\.ly/.+},
19             urlchen => qr{^http://urlchen\.de/.+},
20             google => qr{^http://goo\.gl/.+},
21             );
22              
23             # Can't test, but widely used
24             our %PartOfOtherServices = (
25             twitterco => qr{^http://t\.co/.+},
26             hatena => qr{^http://htn\.to/.+},
27             jmp => qr{^http://j\.mp/.+},
28             tumblr => qr{^http://tmblr\.co/.+},
29             facebook => qr{^http://fb\.me/.+},
30             );
31              
32             our %ExtraServices = (
33             OneShortLink => [ qr{^http://1sl\.net/.+}, 'OneShortLink' ],
34             Shorl => [ qr{^http://shorl\.com/.+}, 'Shorl' ],
35             );
36              
37             # not only dead but also failed anyhow when I tested
38             our %UnsupportedOrDeadServices = (
39             icanhaz => qr{^http://icanhaz\.com/.+},
40             urlTea => qr{^http://urltea\.com/.+},
41             BabyURL => qr{^http://babyurl\.com/.+},
42             Linkz => qr{^http://lin\.kz/?\?.+},
43             TinyClick => qr{^http://tinyclick\.com/?\?.+},
44             V3 => qr{^http://\w+\.v3\.net/},
45             ShortenURL => qr{^http://www\.shortenurl\.com/.+},
46             URLjr => qr{^http://urljr\.com/.+},
47             qURL => qr{^http://qurl\.net/.+},
48             SmLnk => qr{^http://smlnk\.com/.+},
49             ShortLink => qr{^http://shortlink\.us/.+},
50             EkDk => qr{^http://add\.redir\.ek\.dk/.+},
51             MakeAShorterLink => qr{^http://tinyurl\.com/.+},
52             LinkToolbot => qr{^http://link\.toolbot\.com/.+},
53             haojp => qr{^http://hao\.jp/.+},
54             Smallr => qr{^http://smallr\.com/.+},
55             unu => qr{^http://u\.nu/.+},
56             Tinylink => [ qr{^http://tinylink\.com/.+}, 'Tinylink' ],
57             durlme => qr{^http://durl\.me/.+},
58             NotLong => qr{^http://[\w\-]+\.notlong\.com/?$},
59             shadyurl => qr{^http://5z8\.info/.+},
60             miudin => qr{^http://miud\.in/.+},
61             );
62              
63             sub new {
64 0     0 1   my $class = shift;
65              
66 0           my %services;
67 0 0         if ( @_ ) {
68 0           foreach my $name ( @_ ) {
69 0   0       $services{$name} = $PartOfOtherServices{$name}
70             || $KnownServices{$name};
71             }
72             }
73             else {
74 0           %services = (%KnownServices, %PartOfOtherServices);
75             }
76              
77 0           my $ua = LWP::UserAgent->new(
78             env_proxy => 1,
79             timeout => 30,
80             agent => "$class/$VERSION",
81             requests_redirectable => [],
82             );
83              
84 0           bless { ua => $ua, services => \%services }, $class;
85             }
86              
87 0     0 1   sub ua { shift->{ua} }
88              
89             sub try {
90 0     0 1   my ($self, $url) = @_;
91              
92 0           my $new_url;
93             my %seen;
94 0           my $max_try = 5;
95 0           while ($max_try--) {
96 0           $new_url = $self->_try($url);
97 0 0 0       return $new_url if $new_url eq $url or $seen{$new_url}++;
98 0           $url = $new_url;
99             }
100 0           return $url;
101             }
102              
103             sub _try {
104 0     0     my ($self, $url) = @_;
105              
106 0           foreach my $name ( keys %{ $self->{services} } ) {
  0            
107 0           my $service = $self->{services}->{$name};
108 0 0         next unless $service;
109 0 0         if ( ref $service eq 'Regexp' ) {
    0          
110 0 0         next unless $url =~ /$service/;
111 0           my $res = $self->ua->get($url);
112 0 0         next unless $res->is_redirect;
113 0           my $location = $res->header('Location');
114 0 0         return $location if defined $location;
115             }
116             elsif ( ref $service eq 'ARRAY' ) {
117 0           my ($regex, $package) = @{ $service };
  0            
118 0 0         next unless $url =~ /$regex/;
119 0 0         $package = 'WWW::Shorten::'.$package
120             unless $package =~ /^WWW::Shorten::/;
121 0           eval "require $package";
122 0 0         unless ($@) {
123 0           my $longer_url = eval "$package\::makealongerlink('$url')";
124 0 0         return $longer_url if defined $longer_url;
125             }
126             }
127             }
128 0           return $url;
129             }
130              
131             sub add {
132 0     0 1   my ($self, %hash) = @_;
133              
134 0           %{ $self->{services} } = ( %{ $self->{services} }, %hash );
  0            
  0            
135             }
136              
137             1;
138              
139             __END__