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   30345 use strict;
  1         2  
  1         21  
4 1     1   3 use warnings;
  1         1  
  1         45  
5 1     1   556 use LWP::UserAgent;
  1         28422  
  1         735  
6              
7             our $VERSION = '0.09';
8              
9             our %KnownServices = (
10             '0rz' => qr{^https?://0rz\.tw/.+},
11             Metamark => qr{^https?://xrl\.us/.+},
12             SnipURL => qr{^https?://snipurl\.com/.+},
13             TinyURL => qr{^https?://tinyurl\.com/.+},
14             snurl => qr{^https?://snurl\.com/.+},
15             bitly => qr{^https?://bit\.ly/.+},
16             htly => qr{^https?://ht\.ly/.+},
17             isgd => qr{^https?://is\.gd/.+},
18             owly => qr{^https?://ow\.ly/.+},
19             urlchen => qr{^https?://urlchen\.de/.+},
20             google => qr{^https?://goo\.gl/.+},
21             );
22              
23             # Can't test, but widely used
24             our %PartOfOtherServices = (
25             twitterco => qr{^https?://t\.co/.+},
26             hatena => qr{^https?://htn\.to/.+},
27             jmp => qr{^https?://j\.mp/.+},
28             tumblr => qr{^https?://tmblr\.co/.+},
29             facebook => qr{^https?://fb\.me/.+},
30             );
31              
32             our %ExtraServices = (
33             Shorl => [ qr{^https?://shorl\.com/.+}, 'Shorl' ],
34             );
35              
36             # not only dead but also failed anyhow when I tested
37             our %UnsupportedOrDeadServices = (
38             icanhaz => qr{^https?://icanhaz\.com/.+},
39             urlTea => qr{^https?://urltea\.com/.+},
40             BabyURL => qr{^https?://babyurl\.com/.+},
41             Linkz => qr{^https?://lin\.kz/?\?.+},
42             TinyClick => qr{^https?://tinyclick\.com/?\?.+},
43             V3 => qr{^https?://\w+\.v3\.net/},
44             ShortenURL => qr{^https?://www\.shortenurl\.com/.+},
45             URLjr => qr{^https?://urljr\.com/.+},
46             qURL => qr{^https?://qurl\.net/.+},
47             SmLnk => qr{^https?://smlnk\.com/.+},
48             ShortLink => qr{^https?://shortlink\.us/.+},
49             EkDk => qr{^https?://add\.redir\.ek\.dk/.+},
50             MakeAShorterLink => qr{^https?://tinyurl\.com/.+},
51             LinkToolbot => qr{^https?://link\.toolbot\.com/.+},
52             haojp => qr{^https?://hao\.jp/.+},
53             Smallr => qr{^https?://smallr\.com/.+},
54             unu => qr{^https?://u\.nu/.+},
55             Tinylink => [ qr{^https?://tinylink\.com/.+}, 'Tinylink' ],
56             durlme => qr{^https?://durl\.me/.+},
57             NotLong => qr{^https?://[\w\-]+\.notlong\.com/?$},
58             shadyurl => qr{^https?://5z8\.info/.+},
59             miudin => qr{^https?://miud\.in/.+},
60             OneShortLink => [ qr{^https?://1sl\.net/.+}, 'OneShortLink' ],
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             $services{$name} = $PartOfOtherServices{$name}
70 0   0       || $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__