blib/lib/Dallycot/Resolver.pm

filter: Statementstmt 76.4% 39 / 51 Branchbran 0.0% 0 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 85.7% 12 / 14 Podpod 0.0% 0 / 1 totaltotal 75.0% 51 / 68 CC 2 SCAR 5.5
CC
2
Cov
74%
get
6.0
__ANON__
2.0
BEGIN
1.0
CRAP 1.7
« blib/lib/Dallycot/AST/Compose.pm ↑ Summary blib/lib/Dallycot/AST/Condition.pm »
1 package Dallycot::Resolver;
2 10 our $AUTHORITY = 'cpan:JSMITH';
3  
4 # ABSTRACT: Resolve URLs into data objects
5  
6 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 10 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use MooseX::Singleton;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 10 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 10 ▶use CHI;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 10 ▶use Promises qw(deferred);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 10 ▶use RDF::Trine::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 10 ▶use Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 10 ▶use Dallycot::Resolver::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 has cache => (
22 is => 'ro',
23 default => sub {
24 1 ▶ CHI->new( driver => 'Memory', cache_size => '32M', datastore => {} );
Subroutine __ANON__: called CC=1 SCAR=0.0
25 }
26 10 );
27  
28 has ua => (
29 is => 'ro',
30 default => sub {
31 1 ▶ my $ua = Mojo::UserAgent->new;
Subroutine __ANON__: called CC=1 SCAR=0.0
32 1 $ua->connect_timeout(30);
33 1 return $ua;
34 }
35 10 );
36  
37 sub get {
38 0 ▶ my ( $self, $url ) = @_;
Subroutine get: not called CC=2 SCAR=17.9
Pod undocumented
39  
40  
41 0 my $data = $self->cache->get($url);
42 0 ▶ if ( defined($data) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data) { } 0 0 0
43 0 my $deferred = deferred;
44 0 $deferred->resolve($data);
45 0 return $deferred->promise;
46 }
47 else {
48 0 print STDERR "Getting $url\n";
49 0 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 0 ▶ ($data) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
57 0 $self->cache->set( $url, $data );
58 0 $data;
59 }
60 0 );
61 }
62 }
63  
64 10 __PACKAGE__->meta->make_immutable;
65  
66 10 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
« blib/lib/Dallycot/AST/Compose.pm ↑ Summary blib/lib/Dallycot/AST/Condition.pm »