| 1 | package Web::oEmbed; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 3 | ▶ | use 5.8.1; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 3 | our $VERSION = '0.04'; | |||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 3 | ▶ | use Any::Moose; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 3 | has 'format' => (is => 'rw', default => 'json'); | |||||||||||||||||||
| 9 | 3 | has 'discovery' => (is => 'rw'); | |||||||||||||||||||
| 10 | 1 | ▶ | has 'providers' => (is => 'rw', isa => 'ArrayRef', default => sub { [] }); | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 1 | ▶ | has 'agent' => (is => 'rw', isa => 'LWP::UserAgent', default => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 1 | require LWP::UserAgent; | |||||||||||||||||||
| 13 | 1 | LWP::UserAgent->new( agent => __PACKAGE__ . "/" . $VERSION ); | |||||||||||||||||||
| 14 | 3 | }); | |||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 3 | ▶ | use URI; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | 3 | ▶ | use Web::oEmbed::Response; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | 7 | ▶ | sub register_provider { | ||||||||||||||||||
|
Subroutine
register_provider: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 20 | 7 | my($self, $provider) = @_; | |||||||||||||||||||
| 21 | 7 | $provider->{regexp} = $self->_compile_url($provider->{url}); | |||||||||||||||||||
| 22 | 7 | push @{$self->providers}, $provider; | |||||||||||||||||||
| 23 | } | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | 7 | ▶ | sub _compile_url { | ||||||||||||||||||
|
Subroutine
_compile_url: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 26 | 7 | my($self, $url) = @_; | |||||||||||||||||||
| 27 | 7 | my $res; | |||||||||||||||||||
| 28 | 7 | my $uri = URI->new($url); | |||||||||||||||||||
| 29 | 7 | $res = $uri->scheme . "://"; | |||||||||||||||||||
| 30 | 7 | $res .= _run_regexp($uri->host, '[0-9a-zA-Z\-]+'); | |||||||||||||||||||
| 31 | 7 | $res .= _run_regexp($uri->path, "[$URI::uric]+" ); | |||||||||||||||||||
| 32 | 7 | $res; | |||||||||||||||||||
| 33 | } | ||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | 14 | ▶ | sub _run_regexp { | ||||||||||||||||||
|
Subroutine
_run_regexp: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 36 | 14 | my($str, $replacement) = @_; | |||||||||||||||||||
| 37 | 14 | ▶ | $str =~ s/(?:(\*)|([^\*]+))/$1 ? $replacement : quotemeta($2)/eg; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 38 | 14 | $str; | |||||||||||||||||||
| 39 | } | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | 2 | ▶ | sub provider_for { | ||||||||||||||||||
|
Subroutine
provider_for: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||
| 42 | 2 | my($self, $uri) = @_; | |||||||||||||||||||
| 43 | 2 | for my $provider (@{$self->providers}) { | |||||||||||||||||||
| 44 | 7 | ▶ | if ($uri =~ m!^$provider->{regexp}!) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 45 | 2 | return $provider; | |||||||||||||||||||
| 46 | } | ||||||||||||||||||||
| 47 | } | ||||||||||||||||||||
| 48 | 0 | return; | |||||||||||||||||||
| 49 | } | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | 2 | ▶ | sub request_url { | ||||||||||||||||||
|
Subroutine
request_url: called CC=6 SCAR=18.4
Pod
undocumented
| |||||||||||||||||||||
| 52 | 2 | my($self, $uri, $opt) = @_; | |||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | my $params = { | ||||||||||||||||||||
| 55 | url => $uri, | ||||||||||||||||||||
| 56 | 2 | ▶ | format => $opt->{format} || $self->format, | ||||||||||||||||||
|
Condition33%1/3
$opt->{'format'} || $self->format
MC/DC0%0/2
$opt->{'format'} || $self->format
$opt->{'format'}
$self->format
Truth table33%1/3
A || B
A= $opt->{'format'}B= $self->format
| |||||||||||||||||||||
| 57 | }; | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | 2 | ▶ | $params->{maxwidth} = $opt->{maxwidth} if exists $opt->{maxwidth}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 60 | 2 | ▶ | $params->{maxheight} = $opt->{maxheight} if exists $opt->{maxheight}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | 2 | ▶ | my $provider = $self->provider_for($uri) or return; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 63 | 2 | my $req_uri = URI->new( $provider->{api} ); | |||||||||||||||||||
| 64 | 2 | ▶ | if ($req_uri->path =~ /%7Bformat%7D/) { # yuck | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 65 | 1 | my $path = $req_uri->path; | |||||||||||||||||||
| 66 | 1 | $path =~ s/%7Bformat%7D/$params->{format}/; | |||||||||||||||||||
| 67 | 1 | $req_uri->path($path); | |||||||||||||||||||
| 68 | 1 | delete $params->{format}; | |||||||||||||||||||
| 69 | } | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | 2 | $req_uri->query_form($params); | |||||||||||||||||||
| 72 | 2 | $req_uri; | |||||||||||||||||||
| 73 | } | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | 0 | ▶ | sub embed { | ||||||||||||||||||
|
Subroutine
embed: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 76 | 0 | my($self, $uri, $opt) = @_; | |||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | 0 | ▶ | my $url = $self->request_url($uri, $opt) or return; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 79 | 0 | my $res = $self->agent->get($url); | |||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | 0 | Web::oEmbed::Response->new_from_response($res, $uri); | |||||||||||||||||||
| 82 | } | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | 3 | 1; | |||||||||||||||||||
| 85 | __END__ | ||||||||||||||||||||