Attean/SPARQLClient.pm

filter: Statementstmt 84.2% 64 / 76 Branchbran 35.7% 5 / 14 Conditioncond 40.0% 2 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 13 / 13 Podpod 100% 2 / 2 totaltotal 76.1% 86 / 113 CC 10 SCAR 22.4
CC
10
Cov
75%
query
21.3
query_request
2.0
BEGIN
1.0
CRAP 9.4
« Attean/API/Model.pm ↑ Summary AtteanX/Serializer/SPARQLXML.pm »
1 51 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 51 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 =head1 NAME
5  
6 Attean::SPARQLClient - RDF blank nodes
7  
8 =head1 VERSION
9  
10 This document describes Attean::SPARQLClient version 0.041
11  
12 =head1 SYNOPSIS
13  
14 use v5.14;
15 use Attean;
16 my $client = Attean::SPARQLClient->new(endpoint => 'http://example.org/sparql');
17 my $results = $client->query('SELECT * WHERE { ?s ?p ?o }');
18 while (my $r = $results->next) {
19 say $r->as_string;
20 }
21  
22 =head1 DESCRIPTION
23  
24 The Attean::SPARQLClient class provides an API to execute SPARQL queries
25 against a remote SPARQL Protocol endpoint.
26  
27 =head1 ATTRIBUTES
28  
29 The following attributes exist:
30  
31 =over 4
32  
33 =item C<< endpoint >>
34  
35 A URL of the remote service implementing the SPARQL 1.1 Protocol. This value
36 is a L<Attean::API::IRI>, but can be coerced from a string.
37  
38 =item C<< silent >>
39  
40 =item << user_agent >>
41  
42 =item C<< request_signer >>
43  
44 =back
45  
46 =head1 METHODS
47  
48 =over 4
49  
50 =cut
51  
52 51 package Attean::SPARQLClient 0.041 {
53 51 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 51 ▶ use Types::Standard qw(ConsumerOf Bool Str InstanceOf);
Subroutine BEGIN: called CC=1 SCAR=0.0
55 51 ▶ use Encode qw(encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
56 51 ▶ use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
57 51 ▶ use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 51 ▶ use Attean::RDF qw(iri);
Subroutine BEGIN: called CC=1 SCAR=0.0
59 51 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61 4 ▶ has 'endpoint' => (is => 'ro', isa => ConsumerOf['Attean::API::IRI'], coerce => sub { iri(shift) }, required => 1);
Subroutine __ANON__: called CC=1 SCAR=0.0
62 51 has 'silent' => (is => 'ro', isa => Bool, default => 0);
63 2 ▶ has 'user_agent' => (is => 'rw', isa => InstanceOf['LWP::UserAgent'], default => sub { my $ua = LWP::UserAgent->new(); $ua->agent("Attean/$Attean::VERSION " . $ua->_agent); $ua });
Subroutine __ANON__: called CC=1 SCAR=0.0
64 51 has 'request_signer' => (is => 'rw');
65  
66 =item C<< query_request( $sparql ) >>
67  
68 Returns an HTTP::Request object for the given SPARQL query string.
69  
70 =cut
71  
72 4 ▶ sub query_request {
Subroutine query_request: called CC=2 SCAR=6.9
Pod documented
73 4 my $self = shift;
74 4 my $sparql = shift;
75 4 my $endpoint = $self->endpoint->value;
76 4 my $uri = URI->new($endpoint);
77 4 my %params = $uri->query_form;
78 4 $params{'query'} = $sparql;
79 4 $uri->query_form(%params);
80 4 my $url = $uri->as_string;
81 4 my $req = HTTP::Request->new('GET', $url);
82 4 ▶ if (my $signer = $self->request_signer) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $signer = $self->request_signer) 1 3 4
83 1 $signer->sign($req);
84 }
85 4 return $req;
86 }
87  
88 =item C<< query( $sparql ) >>
89  
90 Executes the given SPARQL query string at the remote endpoint. If execution is
91 successful, returns an Attean::API::Iterator object with the results. If
92 execution fails but the client C<< silent >> flag is true, returns an empty
93 iterator. Otherwise raises an error via C<< die >>.
94  
95 =cut
96  
97 2 ▶ sub query {
Subroutine query: called CC=9 SCAR=30.6
Pod documented
98 2 my $self = shift;
99 2 my $sparql = shift;
100 2 my $req = $self->query_request($sparql);
101 2 my $silent = $self->silent;
102 2 my $ua = $self->user_agent;
103
104 2 my $response = $ua->request($req);
105 2 ▶ if (blessed($response) and $response->is_success) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($response) and $response->is_success) { } 2 0 2
elsif ($silent) { } 0 0 0
Condition33%1/3
builtin::blessed($response) and $response->is_success
!ll&&!rl&&r
002
MC/DC0%0/2
builtin::blessed($response) and $response->is_success
builtin::blessed($response) $response->is_success
Truth table33%1/3
A and B
A= builtin::blessed($response)B= $response->is_success
ABresult
0X0
100
111
106 2 my $type = $response->header('Content-Type');
107 2 ▶ my $pclass = eval { Attean->get_parser(media_type => $type) or die "No parser for media type: $type" };
Condition50%1/2
'Attean'->get_parser('media_type', $type) || die("No parser for media type: $type")
l!l
20
MC/DC0%0/1
'Attean'->get_parser('media_type', $type) || die("No parser for media type: $type")
'Attean'->get_parser('media_type', $type)
Truth table50%1/2
A
A= 'Attean'->get_parser('media_type', $type)
Aresult
11
00
108 2 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 2 2
109 0 ▶ if ($silent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($silent) { } 0 0 0
110 0 my $b = Attean::Result->new( bindings => {} );
111 0 return Attean::ListIterator->new(variables => [], values => [$b], item_type => 'Attean::API::Result');
112 } else {
113 0 die $@;
114 }
115 }
116 2 ▶ if (not($pclass)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pclass) 0 2 2
117 0 ▶ if ($silent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($silent) { } 0 0 0
118 0 my $b = Attean::Result->new( bindings => {} );
119 0 return Attean::ListIterator->new(variables => [], values => [$b], item_type => 'Attean::API::Result');
120 } else {
121 0 die "No parser found for type [$type]";
122 }
123 }
124 2 my $parser = $pclass->new();
125 2 my $xml = $response->decoded_content;
126 2 my $bytes = encode('UTF-8', $xml, Encode::FB_CROAK);
127 2 return $parser->parse_iter_from_bytes($bytes);
128 } elsif ($silent) {
129 0 my $b = Attean::Result->new( bindings => {} );
130 0 return Attean::ListIterator->new(variables => [], values => [$b], item_type => 'Attean::API::Result');
131 } else {
132 0 my $endpoint = $self->endpoint->value;
133 0 die "SPARQL Protocol error on endpoint <${endpoint}>: " . $response->status_line;
134 }
135 }
136 }
137  
138 51 1;
139  
140 __END__
« Attean/API/Model.pm ↑ Summary AtteanX/Serializer/SPARQLXML.pm »