line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WWW::LongURL; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
25273
|
use JSON::Any; |
|
2
|
|
|
|
|
62048
|
|
|
2
|
|
|
|
|
14
|
|
4
|
2
|
|
|
2
|
|
16107
|
use LWP::UserAgent; |
|
2
|
|
|
|
|
50167
|
|
|
2
|
|
|
|
|
66
|
|
5
|
2
|
|
|
2
|
|
17
|
use URI::Escape; |
|
2
|
|
|
|
|
9
|
|
|
2
|
|
|
|
|
150
|
|
6
|
2
|
|
|
2
|
|
10
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
72
|
|
7
|
2
|
|
|
2
|
|
9
|
use warnings; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
65
|
|
8
|
2
|
|
|
2
|
|
10
|
use base qw(Class::Accessor::Fast); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
2318
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
__PACKAGE__->mk_accessors(qw(apibase useragent format error)); |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $VERSION = '0.05'; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub new { |
15
|
0
|
|
|
0
|
1
|
|
my $class = shift; |
16
|
|
|
|
|
|
|
|
17
|
0
|
|
|
|
|
|
my $self = {}; |
18
|
0
|
|
|
|
|
|
bless $self, $class; |
19
|
0
|
|
|
|
|
|
$self->apibase('http://api.longurl.org/v2/'); |
20
|
0
|
|
|
|
|
|
$self->format('json'); |
21
|
0
|
|
|
|
|
|
$self->useragent('WWW-LongURL/0.05'); |
22
|
0
|
|
|
|
|
|
return $self; |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub expand { |
26
|
0
|
|
|
0
|
1
|
|
my ($self, $url) = @_; |
27
|
|
|
|
|
|
|
|
28
|
0
|
0
|
|
|
|
|
if (! $url) { |
29
|
0
|
|
|
|
|
|
$self->error("No URL found to expand"); |
30
|
0
|
|
|
|
|
|
return; |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
0
|
|
|
|
|
|
my $request = $self->apibase() . 'expand?url=' . uri_escape($url) . '&format=' . $self->format(); |
34
|
0
|
|
|
|
|
|
my $response = $self->_call_api($request); |
35
|
0
|
0
|
|
|
|
|
return if (! $response); |
36
|
0
|
0
|
|
|
|
|
if ($response->{'long-url'}) { |
37
|
0
|
|
|
|
|
|
return $response->{'long-url'}; |
38
|
|
|
|
|
|
|
} else { |
39
|
0
|
|
|
|
|
|
$self->error("Unrecognized response from " . $self->apibase()); |
40
|
0
|
|
|
|
|
|
return; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub get_services { |
45
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
46
|
|
|
|
|
|
|
|
47
|
0
|
|
|
|
|
|
my $request = $self->apibase() . 'services?&format=' . $self->format(); |
48
|
0
|
|
|
|
|
|
my $response = $self->_call_api($request); |
49
|
0
|
0
|
|
|
|
|
return if (! $response); |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
# TODO: handle :utf8 by default |
52
|
0
|
|
|
|
|
|
return keys (%$response); |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
sub _call_api { |
56
|
0
|
|
|
0
|
|
|
my ($self, $what) = @_; |
57
|
|
|
|
|
|
|
|
58
|
0
|
|
|
|
|
|
my $ua = LWP::UserAgent->new(); |
59
|
0
|
|
|
|
|
|
$ua->agent($self->useragent()); |
60
|
|
|
|
|
|
|
|
61
|
0
|
|
|
|
|
|
my $response = $ua->get($what); |
62
|
0
|
0
|
|
|
|
|
if ($response->is_success()) { |
63
|
0
|
|
|
|
|
|
return JSON::Any->jsonToObj($response->decoded_content()); |
64
|
|
|
|
|
|
|
} else { |
65
|
0
|
|
|
|
|
|
$self->error($response->status_line()); |
66
|
0
|
|
|
|
|
|
return; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
1; |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
__END__ |