AtteanX/Serializer/SPARQLCSV.pm

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