File Coverage

blib/lib/Catmandu/Fix/rdf_ldf_statements.pm
Criterion Covered Total %
statement 12 23 52.1
branch n/a
condition 0 2 0.0
subroutine 4 6 66.6
pod 0 1 0.0
total 16 32 50.0


line stmt bran cond sub pod time code
1             package Catmandu::Fix::rdf_ldf_statements;
2              
3 1     1   73313 use Catmandu::Sane;
  1         129841  
  1         6  
4 1     1   227 use Moo;
  1         3  
  1         5  
5 1     1   606 use RDF::LDF;
  1         1723705  
  1         31  
6 1     1   275 use Catmandu::Fix::Has;
  1         726  
  1         5  
7              
8             has path => (fix_arg => 1);
9             has subject => (fix_opt => 1, default => sub { undef });
10             has predicate => (fix_opt => 1, default => sub { undef });
11             has url => (fix_opt => 1, default => sub { undef });
12              
13             has client => (is => 'lazy');
14              
15             with 'Catmandu::Fix::SimpleGetValue';
16              
17             sub _build_client {
18 0     0     my ($self) = @_;
19 0   0       my $url = $self->url // 'http://data.linkeddatafragments.org/viaf';
20 0           RDF::LDF->new(url => $url);
21             }
22              
23             sub emit_value {
24 0     0 0   my ($self,$var,$fixer) = @_;
25 0           my $subject_var = $fixer->capture($self->subject);
26 0           my $predicate_var = $fixer->capture($self->predicate);
27 0           my $client_var = $fixer->capture($self->client);
28 0           my $it = $fixer->generate_var;
29 0           my $st = $fixer->generate_var;
30              
31 0           my $perl = <<EOF;
32             if (is_value(${var})) {
33             my ${it} = ${client_var}->get_statements(${subject_var},${predicate_var},${var});
34             if (${it}) {
35             ${var} = [];
36             while (my ${st} = ${it}->()) {
37             push \@{${var}} , ${st}->subject->uri;
38             }
39             }
40             }
41             EOF
42 0           $perl;
43             }
44              
45             =head1 NAME
46              
47             Catmandu::Fix::rdf_ldf_statements - lookup an object into a LDF endpoint
48              
49             =head1 SYNOPSIS
50              
51             # Replace a name with an array of matching VIAF records
52             # name: "\"Einstein, Albert, 1879-1955\""
53             rdf_ldf_statements(name,url:"http://data.linkedatafragments.org/viaf",predicate:"http://schema.org/alternateName")
54              
55             # name:
56             # - http://viaf.org/viaf/75121530
57              
58             =head1 DESCRIPTION
59              
60             This L<Catmandu::Fix> can be used to find at a Linked Data Fragments endpoint
61             all subject URIs for which the object has a specific value found at a path. E.g.
62              
63             rdf_ldf_statements(name,url:"http://data.linkedatafragments.org/viaf")
64              
65             means, search at the endpoint http://data.linkedatafragments.org/viaf all the
66             subjects for which the object is the value found in 'name', and replace the name value
67             with all the found subjects.
68              
69             =head1 ARGUMENTS
70              
71             =over
72              
73             =item subject
74              
75             Optional subject URI to be used in the LDF query
76              
77             =item predicate
78              
79             Optional predicate URI to be used in the LDF query
80              
81             =item url
82              
83             Required URL to the Linked Data Fragments endpoint
84              
85             =back
86              
87             =head1 SEE ALSO
88              
89             L<Catmandu::Fix>
90              
91             =cut
92              
93             1;