AtteanX/Serializer/SPARQLCSV.pm

filter: Statementstmt 98.7% 77 / 78 Branchbran 92.8% 13 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 93.3% 14 / 15 Podpod 100% 5 / 5 totaltotal 97.3% 109 / 112 CC 10 SCAR 11.4
CC
10
Cov
98%
serialize_iter_to_bytes
6.0
serialize_iter_to_io
5.0
canonical_iri
2.0
CRAP 3.1
« Attean/TermMap.pm ↑ Summary Test/Attean/TestSimpleQueryEvaluator.pm »
1 =head1 NAME
2  
3 AtteanX::Serializer::SPARQLCSV - SPARQL Results CSV Serializer
4  
5 =head1 VERSION
6  
7 This document describes AtteanX::Serializer::SPARQLCSV version 0.041
8  
9 =head1 SYNOPSIS
10  
11 use Attean;
12 my $s = Attean->get_serializer('SPARQLCSV')->new();
13 $s->serialize_iter_to_io( $fh, $iter );
14  
15 =head1 DESCRIPTION
16  
17 ...
18  
19 =head1 ATTRIBUTES
20  
21 =over 4
22  
23 =item C<< canonical_media_type >>
24  
25 =item C<< file_extensions >>
26  
27 =back
28  
29 =head1 METHODS
30  
31 =over 4
32  
33 =item C<< canonical_iri >>
34  
35 Returns the canonical IRI of this format.
36  
37 =cut
38  
39 4 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 3 package AtteanX::Serializer::SPARQLCSV 0.041 {
43 4 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 4 ▶ use Types::Standard qw(Str ArrayRef);
Subroutine BEGIN: called CC=1 SCAR=0.0
45 4 ▶ use Encode qw(encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
46 4 ▶ use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
47 4 ▶ use Attean::ListIterator;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 4 ▶ use List::Util qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
49 4 ▶ use Text::CSV;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 4 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 0 ▶ sub canonical_iri { return 'http://www.w3.org/ns/formats/SPARQL_Results_CSV' }
Subroutine canonical_iri: not called CC=1 SCAR=6.9
Pod documented
53 3 has 'canonical_media_type' => (is => 'ro', isa => Str, init_arg => undef, default => 'text/csv');
54  
55 =item C<< media_types >>
56  
57 Returns a list of media types that identify the format produced by this serializer.
58  
59 =cut
60  
61 13 ▶ sub media_types {
Subroutine media_types: called CC=1 SCAR=0.0
Pod documented
62 13 return [qw(text/csv)];
63 }
64  
65 =item C<< file_extensions >>
66  
67 Returns a list of file extensions associated with the serialized format.
68  
69 =cut
70  
71 9 ▶ sub file_extensions { return [qw(csv)] };
Subroutine file_extensions: called CC=1 SCAR=0.0
Pod documented
72
73 =item C<< serialize_iter_to_io( $fh, $iterator ) >>
74  
75 Serializes the L<Attean::API::Binding> objects from C<< $iterator >> to the
76 L<IO::Handle> object C<< $fh >>.
77  
78 =cut
79  
80 2 ▶ sub serialize_iter_to_io {
Subroutine serialize_iter_to_io: called CC=5 SCAR=16.1
Pod documented
81 2 my $self = shift;
82 2 my $io = shift;
83 2 my $iter = shift;
84 2 my $csv = Text::CSV->new ( { binary => 1 } );
85  
86 2 my @vars = @{ $iter->variables };
87 2 $csv->print($io, \@vars);
88 2 print $io "\n";
89
90 2 ▶ while (my $t = $iter->next()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $t = $iter->next) 6 2 8
91 6 my @strings;
92 6 foreach my $var (@vars) {
93 18 my $term = $t->value($var);
94 18 ▶ if (blessed($term)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($term)) { } 14 4 18
95 14 ▶ if ($term->does('Attean::API::Blank')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($term->does('Attean::API::Blank')) { } 4 10 14
96 4 push(@strings, $term->ntriples_string);
97 } else {
98 10 push(@strings, $term->value);
99 }
100 } else {
101 4 push(@strings, '');
102 }
103 }
104 6 $csv->print($io, [@strings]);
105 6 print $io "\n";
106 }
107 2 return;
108 }
109
110 =item C<< serialize_iter_to_bytes( $iterator ) >>
111  
112 Serializes the L<Attean::API::Binding> objects from C<< $iterator >>
113 and returns the serialization as a UTF-8 encoded byte string.
114  
115 =cut
116  
117 1 ▶ sub serialize_iter_to_bytes {
Subroutine serialize_iter_to_bytes: called CC=6 SCAR=17.9
Pod documented
118 1 my $self = shift;
119 1 my $iter = shift;
120 1 my $data = encode('UTF-8', '');
121 1 my $csv = Text::CSV->new ( { binary => 1 } );
122  
123 1 my @vars = @{ $iter->variables };
124 1 $csv->combine(map { encode('UTF-8', $_) } @vars);
125 1 $data .= $csv->string . "\n";
126  
127 1 ▶ while (my $t = $iter->next()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $t = $iter->next) 3 1 4
128 3 my @strings;
129 3 foreach my $var (@vars) {
130 9 my $term = $t->value($var);
131 9 ▶ if (blessed($term)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($term)) { } 7 2 9
132 7 ▶ if ($term->does('Attean::API::Blank')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($term->does('Attean::API::Blank')) { } 2 5 7
133 2 push(@strings, $term->ntriples_string);
134 } else {
135 5 push(@strings, $term->value);
136 }
137 } else {
138 2 push(@strings, '');
139 }
140 }
141 3 ▶ if ($csv->combine(map { encode('UTF-8', $_) } @strings)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($csv->combine(map({&encode("UTF-8", $_);} @strings))) 3 0 3
142 3 $data .= $csv->string . "\n";
143 }
144 }
145 1 return $data;
146 }
147  
148 3 with 'Attean::API::ResultSerializer', 'Attean::API::AppendableSerializer';
149 }
150  
151 3 1;
152  
153 __END__
« Attean/TermMap.pm ↑ Summary Test/Attean/TestSimpleQueryEvaluator.pm »