line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Web::oEmbed; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
1490
|
use strict; |
|
3
|
|
|
|
|
9
|
|
|
3
|
|
|
|
|
108
|
|
4
|
3
|
|
|
3
|
|
42
|
use 5.8.1; |
|
3
|
|
|
|
|
14
|
|
|
3
|
|
|
|
|
324
|
|
5
|
|
|
|
|
|
|
our $VERSION = '0.04'; |
6
|
|
|
|
|
|
|
|
7
|
3
|
|
|
3
|
|
3248
|
use Any::Moose; |
|
3
|
|
|
|
|
110615
|
|
|
3
|
|
|
|
|
23
|
|
8
|
|
|
|
|
|
|
has 'format' => (is => 'rw', default => 'json'); |
9
|
|
|
|
|
|
|
has 'discovery' => (is => 'rw'); |
10
|
|
|
|
|
|
|
has 'providers' => (is => 'rw', isa => 'ArrayRef', default => sub { [] }); |
11
|
|
|
|
|
|
|
has 'agent' => (is => 'rw', isa => 'LWP::UserAgent', default => sub { |
12
|
|
|
|
|
|
|
require LWP::UserAgent; |
13
|
|
|
|
|
|
|
LWP::UserAgent->new( agent => __PACKAGE__ . "/" . $VERSION ); |
14
|
|
|
|
|
|
|
}); |
15
|
|
|
|
|
|
|
|
16
|
3
|
|
|
3
|
|
8845
|
use URI; |
|
3
|
|
|
|
|
31543
|
|
|
3
|
|
|
|
|
116
|
|
17
|
3
|
|
|
3
|
|
2046
|
use Web::oEmbed::Response; |
|
3
|
|
|
|
|
11
|
|
|
3
|
|
|
|
|
2050
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub register_provider { |
20
|
7
|
|
|
7
|
1
|
5422
|
my($self, $provider) = @_; |
21
|
7
|
|
|
|
|
25
|
$provider->{regexp} = $self->_compile_url($provider->{url}); |
22
|
7
|
|
|
|
|
10
|
push @{$self->providers}, $provider; |
|
7
|
|
|
|
|
38
|
|
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub _compile_url { |
26
|
7
|
|
|
7
|
|
15
|
my($self, $url) = @_; |
27
|
7
|
|
|
|
|
8
|
my $res; |
28
|
7
|
|
|
|
|
28
|
my $uri = URI->new($url); |
29
|
7
|
|
|
|
|
11999
|
$res = $uri->scheme . "://"; |
30
|
7
|
|
|
|
|
302
|
$res .= _run_regexp($uri->host, '[0-9a-zA-Z\-]+'); |
31
|
7
|
|
|
|
|
27
|
$res .= _run_regexp($uri->path, "[$URI::uric]+" ); |
32
|
7
|
|
|
|
|
37
|
$res; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub _run_regexp { |
36
|
14
|
|
|
14
|
|
259
|
my($str, $replacement) = @_; |
37
|
14
|
100
|
|
|
|
51
|
$str =~ s/(?:(\*)|([^\*]+))/$1 ? $replacement : quotemeta($2)/eg; |
|
25
|
|
|
|
|
99
|
|
38
|
14
|
|
|
|
|
38
|
$str; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub provider_for { |
42
|
2
|
|
|
2
|
0
|
3
|
my($self, $uri) = @_; |
43
|
2
|
|
|
|
|
5
|
for my $provider (@{$self->providers}) { |
|
2
|
|
|
|
|
9
|
|
44
|
7
|
100
|
|
|
|
234
|
if ($uri =~ m!^$provider->{regexp}!) { |
45
|
2
|
|
|
|
|
12
|
return $provider; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
} |
48
|
0
|
|
|
|
|
0
|
return; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
sub request_url { |
52
|
2
|
|
|
2
|
0
|
5
|
my($self, $uri, $opt) = @_; |
53
|
|
|
|
|
|
|
|
54
|
2
|
|
33
|
|
|
26
|
my $params = { |
55
|
|
|
|
|
|
|
url => $uri, |
56
|
|
|
|
|
|
|
format => $opt->{format} || $self->format, |
57
|
|
|
|
|
|
|
}; |
58
|
|
|
|
|
|
|
|
59
|
2
|
50
|
|
|
|
8
|
$params->{maxwidth} = $opt->{maxwidth} if exists $opt->{maxwidth}; |
60
|
2
|
50
|
|
|
|
5
|
$params->{maxheight} = $opt->{maxheight} if exists $opt->{maxheight}; |
61
|
|
|
|
|
|
|
|
62
|
2
|
50
|
|
|
|
8
|
my $provider = $self->provider_for($uri) or return; |
63
|
2
|
|
|
|
|
15
|
my $req_uri = URI->new( $provider->{api} ); |
64
|
2
|
100
|
|
|
|
217
|
if ($req_uri->path =~ /%7Bformat%7D/) { # yuck |
65
|
1
|
|
|
|
|
18
|
my $path = $req_uri->path; |
66
|
1
|
|
|
|
|
12
|
$path =~ s/%7Bformat%7D/$params->{format}/; |
67
|
1
|
|
|
|
|
4
|
$req_uri->path($path); |
68
|
1
|
|
|
|
|
63
|
delete $params->{format}; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
2
|
|
|
|
|
32
|
$req_uri->query_form($params); |
72
|
2
|
|
|
|
|
387
|
$req_uri; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
sub embed { |
76
|
0
|
|
|
0
|
1
|
|
my($self, $uri, $opt) = @_; |
77
|
|
|
|
|
|
|
|
78
|
0
|
0
|
|
|
|
|
my $url = $self->request_url($uri, $opt) or return; |
79
|
0
|
|
|
|
|
|
my $res = $self->agent->get($url); |
80
|
|
|
|
|
|
|
|
81
|
0
|
|
|
|
|
|
Web::oEmbed::Response->new_from_response($res, $uri); |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
1; |
85
|
|
|
|
|
|
|
__END__ |