blib/lib/RDF/Query/Client.pm

filter: Statementstmt 21.8% 36 / 165 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 28.5% 12 / 42 Podpod 28.5% 8 / 28 totaltotal 18.0% 56 / 311 CC 37 SCAR 43.5
CC
37
Cov
15%
_prepare_request
306.0
get
42.0
_create_iterator
42.0
CRAP 77.5
↑ Summary
1 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package RDF::Query::Client;
6  
7 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 $RDF::Query::Client::AUTHORITY = 'cpan:TOBYINK';
9 1 $RDF::Query::Client::VERSION = '0.114';
10 }
11  
12 1 ▶use Carp 0 qw/carp/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use LWP::UserAgent 0 qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use RDF::Trine 0.133 qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Scalar::Util 0 qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use URI::Escape 0 qw/uri_escape/;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 sub new
21 0 ▶{
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
22 0 my $class = shift;
23 0 my ($query, $opts) = @_;
24
25 bless {
26 query => $query ,
27 0 ▶ useragent => ($opts->{UserAgent} // undef) ,
Condition0%0/2
$opts->{"UserAgent"} // undef
l!l
00
MC/DC0%0/1
$opts->{"UserAgent"} // undef
$opts->{"UserAgent"}
Truth table0%0/2
A
A= $opts->{"UserAgent"}
Aresult
11
00
28 results => [] ,
29 error => undef ,
30 }, $class;
31 }
32  
33 sub execute
34 0 ▶{
Subroutine execute: not called CC=4 SCAR=30.0
Pod documented
35 0 my $self = shift;
36 0 my ($endpoint, $opts) = @_;
37
38 0 ▶ my $ua = $opts->{UserAgent} // $self->useragent;
Condition0%0/3
$opts->{"UserAgent"} // $self->useragent
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts->{"UserAgent"} // $self->useragent
$opts->{"UserAgent"} $self->useragent
Truth table0%0/3
A // B
A= $opts->{"UserAgent"}B= $self->useragent
ABresult
1X1
011
000
39 0 my $request = $self->_prepare_request($endpoint, $opts);
40 0 my $response = $ua->request($request);
41
42 0 push @{ $self->{results} }, { response => $response };
43
44 0 my $iterator = $self->_create_iterator($response);
45 0 ▶ return unless defined $iterator;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $iterator 0 0 0
46 0 $self->{results}[-1]{iterator} = $iterator;
47
48 0 ▶ wantarray ? $iterator->get_all : $iterator;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
49 }
50  
51 1 our $LRDD;
52 sub discover_execute
53 0 ▶{
Subroutine discover_execute: not called CC=6 SCAR=34.2
Pod documented
54 0 my $self = shift;
55 0 my ($resource_uri, $opts) = @_;
56
57 0 ▶ unless ($LRDD)
Branch: 1 branch
BranchTrueFalseTotal
unless ($RDF::Query::Client::LRDD) 0 0 0
58 {
59 1 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 0 eval 'use HTTP::LRDD;';
61 eval
62 0 {
63 0 ▶ $LRDD = HTTP::LRDD->new('http://ontologi.es/sparql#endpoint', 'http://ontologi.es/sparql#fingerpoint')
Branch: 1 branch
BranchTrueFalseTotal
if "HTTP::LRDD"->can("new") 0 0 0
64 if HTTP::LRDD->can('new');
65 };
66 }
67
68 0 ▶ unless (blessed($LRDD) and $LRDD->isa('HTTP::LRDD'))
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($RDF::Query::Client::LRDD) and $RDF::Query::Client::LRDD->isa("HTTP::LRDD")) 0 0 0
Condition0%0/3
builtin::blessed($RDF::Query::Client::LRDD) and $RDF::Query::Client::LRDD->isa("HTTP::LRDD")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($RDF::Query::Client::LRDD) and $RDF::Query::Client::LRDD->isa("HTTP::LRDD")
builtin::blessed($RDF::Query::Client::LRDD) $RDF::Query::Client::LRDD->isa("HTTP::LRDD")
Truth table0%0/3
A and B
A= builtin::blessed($RDF::Query::Client::LRDD)B= $RDF::Query::Client::LRDD->isa("HTTP::LRDD")
ABresult
0X0
100
111
69 {
70 0 $self->{error} = "Need HTTP::LRDD to use the discover_execute feature.";
71 0 return;
72 }
73
74 0 ▶ my $endpoint = $LRDD->discover($resource_uri)
Branch: 1 branch
BranchTrueFalseTotal
my $endpoint = $RDF::Query::Client::LRDD->discover($resource_uri) or return 0 0 0
75 or return;
76
77 0 return $self->execute($endpoint, $opts);
78 }
79  
80 sub get
81 0 ▶{
Subroutine get: not called CC=6 SCAR=37.4
Pod documented
82 0 my $self = shift;
83 0 my $stream = $self->execute(@_);
84
85 0 ▶ if (ref $stream)
Branch: 1 branch
BranchTrueFalseTotal
if (ref $stream) 0 0 0
86 {
87 0 ▶ if ($stream->is_bindings)
Branch: 1 branch
BranchTrueFalseTotal
if ($stream->is_bindings) 0 0 0
88 {
89 0 my $row = $stream->next;
90 0 return $stream->binding_values;
91 }
92 0 ▶ if ($stream->is_graph)
Branch: 1 branch
BranchTrueFalseTotal
if ($stream->is_graph) 0 0 0
93 {
94 0 my $st = $stream->next;
95 0 return ($st->subject, $st->predicate, $st->object);
96 }
97 0 ▶ if ($stream->is_boolean)
Branch: 1 branch
BranchTrueFalseTotal
if ($stream->is_boolean) 0 0 0
98 {
99 0 my @rv;
100 0 ▶ push @rv, 1 if $stream->get_boolean;
Branch: 1 branch
BranchTrueFalseTotal
if $stream->get_boolean 0 0 0
101 0 return @rv;
102 }
103 }
104
105 0 return;
106 }
107  
108 sub as_sparql
109 0 ▶{
Subroutine as_sparql: not called CC=1 SCAR=6.9
Pod documented
110 0 return (shift)->{query};
111 }
112  
113 sub http_response
114 0 ▶{
Subroutine http_response: not called CC=1 SCAR=6.9
Pod documented
115 0 return (shift)->{results}[-1]{response};
116 }
117  
118 sub error
119 0 ▶{
Subroutine error: not called CC=1 SCAR=6.9
Pod documented
120 0 return (shift)->{error};
121 }
122  
123 0 ▶sub prepare { carp "Method not implemented"; }
Subroutine prepare: not called CC=1 SCAR=6.9
Pod undocumented
124 0 ▶sub execute_plan { carp "Method not implemented"; }
Subroutine execute_plan: not called CC=1 SCAR=6.9
Pod undocumented
125 0 ▶sub execute_with_named_graphs { carp "Method not implemented"; }
Subroutine execute_with_named_graphs: not called CC=1 SCAR=6.9
Pod undocumented
126 0 ▶sub aggregate { carp "Method not implemented"; }
Subroutine aggregate: not called CC=1 SCAR=6.9
Pod undocumented
127 0 ▶sub pattern { carp "Method not implemented"; }
Subroutine pattern: not called CC=1 SCAR=6.9
Pod undocumented
128 0 ▶sub sse { carp "Method not implemented"; }
Subroutine sse: not called CC=1 SCAR=6.9
Pod undocumented
129 0 ▶sub algebra_fixup { carp "Method not implemented"; }
Subroutine algebra_fixup: not called CC=1 SCAR=6.9
Pod undocumented
130 0 ▶sub add_function { carp "Method not implemented"; }
Subroutine add_function: not called CC=1 SCAR=6.9
Pod undocumented
131 0 ▶sub supported_extensions { carp "Method not implemented"; }
Subroutine supported_extensions: not called CC=1 SCAR=6.9
Pod undocumented
132 0 ▶sub supported_functions { carp "Method not implemented"; }
Subroutine supported_functions: not called CC=1 SCAR=6.9
Pod undocumented
133 0 ▶sub add_computed_statement_generator { carp "Method not implemented"; }
Subroutine add_computed_statement_generator: not called CC=1 SCAR=6.9
Pod undocumented
134 0 ▶sub get_computed_statement_generators { carp "Method not implemented"; }
Subroutine get_computed_statement_generators: not called CC=1 SCAR=6.9
Pod undocumented
135 0 ▶sub net_filter_function { carp "Method not implemented"; }
Subroutine net_filter_function: not called CC=1 SCAR=6.9
Pod undocumented
136 0 ▶sub add_hook_once { carp "Method not implemented"; }
Subroutine add_hook_once: not called CC=1 SCAR=6.9
Pod undocumented
137 0 ▶sub add_hook { carp "Method not implemented"; }
Subroutine add_hook: not called CC=1 SCAR=6.9
Pod undocumented
138 0 ▶sub parsed { carp "Method not implemented"; }
Subroutine parsed: not called CC=1 SCAR=6.9
Pod undocumented
139 0 ▶sub bridge { carp "Method not implemented"; }
Subroutine bridge: not called CC=1 SCAR=6.9
Pod undocumented
140 0 ▶sub log { carp "Method not implemented"; }
Subroutine log: not called CC=1 SCAR=6.9
Pod undocumented
141 0 ▶sub logger { carp "Method not implemented"; }
Subroutine logger: not called CC=1 SCAR=6.9
Pod undocumented
142 0 ▶sub costmodel { carp "Method not implemented"; }
Subroutine costmodel: not called CC=1 SCAR=6.9
Pod undocumented
143  
144 sub useragent
145 0 ▶{
Subroutine useragent: not called CC=2 SCAR=13.7
Pod documented
146 0 my $self = shift;
147
148 0 ▶ unless (defined $self->{useragent})
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"useragent"}) 0 0 0
149 {
150 0 my $accept = join q{, } => (
151 'application/sparql-results+xml',
152 'application/sparql-results+json;q=0.9',
153 'application/rdf+xml',
154 'application/x-turtle',
155 'text/turtle',
156 );
157 my $agent = sprintf(
158 '%s/%s (%s) ',
159 __PACKAGE__,
160 __PACKAGE__->VERSION,
161 0 ▶ do { no strict "refs"; ${ref($self)."::AUTHORITY"} },
Subroutine BEGIN: called CC=1 SCAR=1.2
162 );
163 0 $self->{useragent} = LWP::UserAgent->new(
164 agent => $agent,
165 max_redirect => 2,
166 parse_head => 0,
167 protocols_allowed => [qw/http https/],
168 );
169 0 $self->{useragent}->default_header(Accept => $accept);
170 }
171
172 0 $self->{useragent};
173 }
174  
175 sub _prepare_request
176 0 ▶{
Subroutine _prepare_request: not called CC=17 SCAR=57.2
177 0 my $self = shift;
178 0 my ($endpoint, $opts) = @_;
179
180 0 ▶ my $method = uc($opts->{QueryMethod} // '');
Condition0%0/2
$opts->{"QueryMethod"} // ""
l!l
00
MC/DC0%0/1
$opts->{"QueryMethod"} // ""
$opts->{"QueryMethod"}
Truth table0%0/2
A
A= $opts->{"QueryMethod"}
Aresult
11
00
181 0 ▶ if ($method !~ /^(get|post|patch)$/i)
Branch: 1 branch
BranchTrueFalseTotal
unless ($method =~ /^(get|post|patch)$/i) 0 0 0
182 {
183 0 ▶ $method = (length $self->{'query'} > 511) ? 'POST' : 'GET';
Branch: 1 branch
BranchTrueFalseTotal
length $self->{'query'} > 511 ? : 0 0 0
184 }
185
186 0 ▶ my $param = $opts->{QueryParameter} // 'query';
Condition0%0/2
$opts->{"QueryParameter"} // "query"
l!l
00
MC/DC0%0/1
$opts->{"QueryParameter"} // "query"
$opts->{"QueryParameter"}
Truth table0%0/2
A
A= $opts->{"QueryParameter"}
Aresult
11
00
187
188 0 my $uri = '';
189 0 my $cnt = '';
190 0 ▶ if ($method eq 'GET')
Branch: 2 branches
BranchTrueFalseTotal
if ($method eq 'GET') { } 0 0 0
elsif ($method eq 'POST') { } 0 0 0
191 {
192 0 ▶ $uri = $endpoint . ($endpoint =~ /\?/ ? '&' : '?');
Branch: 1 branch
BranchTrueFalseTotal
$endpoint =~ /\?/ ? : 0 0 0
193 $uri .= sprintf(
194 "%s=%s",
195 uri_escape($param),
196 uri_escape($self->{query})
197 0 );
198 0 ▶ if ($opts->{Parameters})
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"Parameters"}) 0 0 0
199 {
200 0 foreach my $field (keys %{$opts->{Parameters}})
201 {
202 $uri .= sprintf(
203 "&%s=%s",
204 uri_escape($field),
205 0 uri_escape($opts->{Parameters}->{$field}),
206 );
207 }
208 }
209 }
210 elsif ($method eq 'POST')
211 {
212 0 $uri = $endpoint;
213 $cnt = sprintf(
214 "%s=%s",
215 uri_escape($param),
216 uri_escape($self->{query})
217 0 );
218 0 ▶ if ($opts->{Parameters})
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"Parameters"}) 0 0 0
219 {
220 0 foreach my $field (keys %{$opts->{Parameters}})
221 {
222 $cnt .= sprintf(
223 "&%s=%s",
224 uri_escape($field),
225 0 uri_escape($opts->{Parameters}{$field}),
226 );
227 }
228 }
229 }
230
231 0 my $req = HTTP::Request->new($method => $uri);
232
233 0 ▶ my $type = $opts->{ContentType} // '';
Condition0%0/2
$opts->{"ContentType"} // ""
l!l
00
MC/DC0%0/1
$opts->{"ContentType"} // ""
$opts->{"ContentType"}
Truth table0%0/2
A
A= $opts->{"ContentType"}
Aresult
11
00
234 0 ▶ if ($type =~ m{^application/sparql-query}i)
Branch: 2 branches
BranchTrueFalseTotal
if ($type =~ m[^application/sparql-query]i) { } 0 0 0
elsif ($type =~ m[^application/sparql-update]i) { } 0 0 0
235 {
236 0 $req->content_type('application/sparql-query');
237 0 $req->content($self->{query});
238 }
239 elsif ($type =~ m{^application/sparql-update}i)
240 {
241 0 $req->content_type('application/sparql-update');
242 0 $req->content($self->{query});
243 }
244 else
245 {
246 0 $req->content_type('application/x-www-form-urlencoded');
247 0 $req->content($cnt);
248 }
249
250 $req->authorization_basic($opts->{AuthUsername}, $opts->{AuthPassword})
251 0 ▶ if defined $opts->{AuthUsername};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"AuthUsername"} 0 0 0
252
253 0 foreach my $k (keys %{$opts->{Headers}})
254 {
255 0 $req->header($k => $opts->{Headers}{$k});
256 }
257
258 0 $req;
259 }
260  
261 sub _create_iterator
262 0 ▶{
Subroutine _create_iterator: not called CC=6 SCAR=37.4
263 0 my $self = shift;
264 0 my ($response) = @_;
265
266 0 ▶ unless ($response->is_success)
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
267 {
268 0 $self->{error} = $response->message;
269 0 return;
270 }
271
272 0 ▶ if ($response->content_type =~ /sparql.results/)
Branch: 1 branch
BranchTrueFalseTotal
if ($response->content_type =~ /sparql.results/) { } 0 0 0
273 {
274 0 local $@ = undef;
275 my $iterator = eval
276 0 {
277 0 ▶ if ($response->content_type =~ /json/)
Branch: 1 branch
BranchTrueFalseTotal
if ($response->content_type =~ /json/) { } 0 0 0
278 0 { RDF::Trine::Iterator->from_json($response->decoded_content); }
279 else
280 0 { RDF::Trine::Iterator->from_string($response->decoded_content); }
281 };
282 0 ▶ return $iterator
Branch: 1 branch
BranchTrueFalseTotal
if $iterator 0 0 0
283 if $iterator;
284
285 0 $self->{error} = $@;
286 0 return;
287 }
288 else
289 {
290 0 my $model;
291 eval
292 0 {
293 0 my $parser = RDF::Trine::Parser->parser_by_media_type($response->content_type);
294 0 my $tmp = RDF::Trine::Model->temporary_model;
295 0 $parser->parse_into_model($response->base, $response->decoded_content, $tmp);
296 0 $model = $tmp;
297 };
298
299 0 ▶ return $model->as_stream if defined $model;
Branch: 1 branch
BranchTrueFalseTotal
if defined $model 0 0 0
300
301 0 $self->{error} = sprintf("Response of type '%s' could not be parsed.", $response->content_type);
302 0 return;
303 }
304 }
305  
306 1 1;
307  
308 __END__
↑ Summary