blib/lib/WWW/Lengthen.pm untested

filter: Statementstmt 0.0% 0 / 60 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 6 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 103 SCAR 52.0
CC
13
Cov
0%
_try
132.0
new
6.0
try
6.0
CRAP 182.0
1 package WWW::Lengthen;
2  
3 use strict;
4 use warnings;
5 use LWP::UserAgent;
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 my $class = shift;
65  
66 my %services;
67 if ( @_ ) {
68 foreach my $name ( @_ ) {
69 $services{$name} = $PartOfOtherServices{$name}
70 || $KnownServices{$name};
71 }
72 }
73 else {
74 %services = (%KnownServices, %PartOfOtherServices);
75 }
76  
77 my $ua = LWP::UserAgent->new(
78 env_proxy => 1,
79 timeout => 30,
80 agent => "$class/$VERSION",
81 requests_redirectable => [],
82 );
83  
84 bless { ua => $ua, services => \%services }, $class;
85 }
86  
87 sub ua { shift->{ua} }
88  
89 sub try {
90 my ($self, $url) = @_;
91  
92 my $new_url;
93 my %seen;
94 my $max_try = 5;
95 while ($max_try--) {
96 $new_url = $self->_try($url);
97 return $new_url if $new_url eq $url or $seen{$new_url}++;
98 $url = $new_url;
99 }
100 return $url;
101 }
102  
103 sub _try {
104 my ($self, $url) = @_;
105  
106 foreach my $name ( keys %{ $self->{services} } ) {
107 my $service = $self->{services}->{$name};
108 next unless $service;
109 if ( ref $service eq 'Regexp' ) {
110 next unless $url =~ /$service/;
111 my $res = $self->ua->get($url);
112 next unless $res->is_redirect;
113 my $location = $res->header('Location');
114 return $location if defined $location;
115 }
116 elsif ( ref $service eq 'ARRAY' ) {
117 my ($regex, $package) = @{ $service };
118 next unless $url =~ /$regex/;
119 $package = 'WWW::Shorten::'.$package
120 unless $package =~ /^WWW::Shorten::/;
121 eval "require $package";
122 unless ($@) {
123 my $longer_url = eval "$package\::makealongerlink('$url')";
124 return $longer_url if defined $longer_url;
125 }
126 }
127 }
128 return $url;
129 }
130  
131 sub add {
132 my ($self, %hash) = @_;
133  
134 %{ $self->{services} } = ( %{ $self->{services} }, %hash );
135 }
136  
137 1;
138  
139 __END__