line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Dallycot::Resolver; |
2
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:JSMITH'; |
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
# ABSTRACT: Resolve URLs into data objects |
5
|
|
|
|
|
|
|
|
6
|
10
|
|
|
10
|
|
40180
|
use strict; |
|
10
|
|
|
|
|
40
|
|
|
10
|
|
|
|
|
386
|
|
7
|
10
|
|
|
10
|
|
44
|
use warnings; |
|
10
|
|
|
|
|
16
|
|
|
10
|
|
|
|
|
296
|
|
8
|
|
|
|
|
|
|
|
9
|
10
|
|
|
10
|
|
537
|
use utf8; |
|
10
|
|
|
|
|
21
|
|
|
10
|
|
|
|
|
64
|
|
10
|
10
|
|
|
10
|
|
587
|
use MooseX::Singleton; |
|
10
|
|
|
|
|
401376
|
|
|
10
|
|
|
|
|
73
|
|
11
|
|
|
|
|
|
|
|
12
|
10
|
|
|
10
|
|
108200
|
use namespace::autoclean; |
|
10
|
|
|
|
|
1271
|
|
|
10
|
|
|
|
|
78
|
|
13
|
|
|
|
|
|
|
|
14
|
10
|
|
|
10
|
|
2828
|
use CHI; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
use Promises qw(deferred); |
16
|
|
|
|
|
|
|
use RDF::Trine::Parser; |
17
|
|
|
|
|
|
|
use Mojo::UserAgent; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
use Dallycot::Resolver::Request; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
has cache => ( |
22
|
|
|
|
|
|
|
is => 'ro', |
23
|
|
|
|
|
|
|
default => sub { |
24
|
|
|
|
|
|
|
CHI->new( driver => 'Memory', cache_size => '32M', datastore => {} ); |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
); |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
has ua => ( |
29
|
|
|
|
|
|
|
is => 'ro', |
30
|
|
|
|
|
|
|
default => sub { |
31
|
|
|
|
|
|
|
my $ua = Mojo::UserAgent->new; |
32
|
|
|
|
|
|
|
$ua->connect_timeout(30); |
33
|
|
|
|
|
|
|
return $ua; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
); |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub get { |
38
|
|
|
|
|
|
|
my ( $self, $url ) = @_; |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
my $data = $self->cache->get($url); |
42
|
|
|
|
|
|
|
if ( defined($data) ) { |
43
|
|
|
|
|
|
|
my $deferred = deferred; |
44
|
|
|
|
|
|
|
$deferred->resolve($data); |
45
|
|
|
|
|
|
|
return $deferred->promise; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
else { |
48
|
|
|
|
|
|
|
print STDERR "Getting $url\n"; |
49
|
|
|
|
|
|
|
my $request = Dallycot::Resolver::Request->new( |
50
|
|
|
|
|
|
|
ua => $self->ua, |
51
|
|
|
|
|
|
|
url => $url, |
52
|
|
|
|
|
|
|
canonical_url => $url, |
53
|
|
|
|
|
|
|
); |
54
|
|
|
|
|
|
|
return $request->run->then( |
55
|
|
|
|
|
|
|
sub { |
56
|
|
|
|
|
|
|
($data) = @_; |
57
|
|
|
|
|
|
|
$self->cache->set( $url, $data ); |
58
|
|
|
|
|
|
|
$data; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
); |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
1; |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
__END__ |
69
|
|
|
|
|
|
|
=encoding utf8 |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=head1 SYNOPSIS |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
my $resolver = Dallycot::Resolver->new( |
74
|
|
|
|
|
|
|
cache => CHI->new(...) |
75
|
|
|
|
|
|
|
); |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
my $promise = $resolver->get($uri); |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=head1 DESCRIPTION |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
The resolver will fetch the linked data at the provided URL and |
82
|
|
|
|
|
|
|
return a promise that will be fulfilled with the parsed/extracted |
83
|
|
|
|
|
|
|
RDF as a simple graph. Different backends will parse data from |
84
|
|
|
|
|
|
|
different sources. |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
In order of preference: |
87
|
|
|
|
|
|
|
- JSON-LD |
88
|
|
|
|
|
|
|
- RDF/XML |
89
|
|
|
|
|
|
|
- RDFa |