Attean/SPARQLClient.pm

filter: Statementstmt 80.0% 60 / 75 Branchbran 28.5% 4 / 14 Conditioncond 40.0% 2 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 92.3% 12 / 13 Podpod 100% 2 / 2 totaltotal 71.4% 80 / 112 CC 10 SCAR 22.1
CC
10
Cov
70%
query
20.6
query_request
2.0
__ANON__
1.4
CRAP 9.1
« Attean/API/Model.pm ↑ Summary AtteanX/Serializer/SPARQLXML.pm »
1 50 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 50 ▶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.040
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 50 package Attean::SPARQLClient 0.040 {
53 50 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 50 ▶ use Types::Standard qw(ConsumerOf Bool Str InstanceOf);
Subroutine BEGIN: called CC=1 SCAR=0.0
55 50 ▶ use Encode qw(encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
56 50 ▶ use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
57 50 ▶ use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 50 ▶ use Attean::RDF qw(iri);
Subroutine BEGIN: called CC=1 SCAR=0.0
59 50 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61 1 ▶ has 'endpoint' => (is => 'ro', isa => ConsumerOf['Attean::API::IRI'], coerce => sub { iri(shift) }, required => 1);
Subroutine __ANON__: called CC=1 SCAR=0.0
62 50 has 'silent' => (is => 'ro', isa => Bool, default => 0);
63 0 ▶ 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__: not called CC=1 SCAR=3.5
64 50 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 1 ▶ sub query_request {
Subroutine query_request: called CC=2 SCAR=7.0
Pod documented
73 1 my $self = shift;
74 1 my $sparql = shift;
75 1 my $endpoint = $self->endpoint->value;
76 1 my $uri = URI->new($endpoint);
77 1 my %params = $uri->query_form;
78 1 $params{'query'} = $sparql;
79 1 $uri->query_form(%params);
80 1 my $url = $uri->as_string;
81 1 my $req = HTTP::Request->new('GET', $url);
82 1 ▶ if (my $signer = $self->request_signer) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $signer = $self->request_signer) 0 1 1
83 0 $signer->sign($req);
84 }
85 1 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 1 ▶ sub query {
Subroutine query: called CC=9 SCAR=30.2
Pod documented
98 1 my $self = shift;
99 1 my $sparql = shift;
100 1 my $req = $self->query_request($sparql);
101 1 my $silent = $self->silent;
102 1 my $ua = $self->user_agent;
103
104 1 my $response = $ua->request($req);
105 1 ▶ if (blessed($response) and $response->is_success) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($response) and $response->is_success) { } 1 0 1
elsif ($silent) { } 0 0 0
Condition33%1/3
builtin::blessed($response) and $response->is_success
!ll&&!rl&&r
001
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 1 my $type = $response->header('Content-Type');
107 1 ▶ 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
10
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 1 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
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 1 ▶ if (not($pclass)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pclass) 0 1 1
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 1 my $parser = $pclass->new();
125 1 my $xml = $response->decoded_content;
126 1 my $bytes = encode('UTF-8', $xml, Encode::FB_CROAK);
127 1 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 die "SPARQL Protocol error: " . $response->status_line;
133 }
134 }
135 }
136  
137 50 1;
138  
139 __END__
« Attean/API/Model.pm ↑ Summary AtteanX/Serializer/SPARQLXML.pm »