Exporter/PNX.pm untested

filter: Statementstmt 0.0% 0 / 40 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 68 CC 5 SCAR 20.4
CC
5
Cov
0%
add
20.0
_oai_record_header
6.0
_build_pnx
2.0
CRAP 7.7
1 package Catmandu::Exporter::PNX;
2  
3 use Catmandu::Sane;
4  
5 our $VERSION = '0.04';
6  
7 use Moo;
8 use Catmandu::PNX;
9  
10 with 'Catmandu::Exporter';
11  
12 has 'pnx' => (is => 'lazy');
13  
14 sub _build_pnx {
15 return Catmandu::PNX->new;
16 }
17  
18 sub add {
19 my ($self, $data) = @_;
20  
21 my $id = $data->{control}->{sourcerecordid} // 'undefined';
22  
23 if ($self->count == 0) {
24 $self->fh->print($self->_oai_header());
25 }
26  
27 my $deleted = $data->{deleted} ? 1 : 0;
28  
29 $self->fh->print($self->_oai_record_header($id, deleted => $deleted));
30  
31 delete $data->{deleted};
32  
33 my $xml = $self->pnx->to_xml($data);
34  
35 $xml =~ s{<\?xml.*\?>\n}{};
36  
37 $self->fh->print($xml) if $xml;
38  
39 $self->fh->print($self->_oai_record_footer());
40 }
41  
42 sub commit {
43 my ($self) = @_;
44  
45 $self->fh->print(_oai_footer());
46 }
47  
48 sub _oai_header {
49 my $str =<<EOF;
50 <?xml version="1.0" encoding="UTF-8"?>
51 <OAI-PMH xmlns="http://www.openarchives.org/OAI/2.0/"
52 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
53 xsi:schemaLocation="
54 http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd">
55 <ListRecords>
56 EOF
57 $str;
58 }
59  
60 sub _oai_record_header {
61 my ($self,$id,%opts) = @_;
62  
63 my $str =<<EOF;
64 <record>
65 EOF
66  
67 if ($opts{deleted}) {
68 $str .= "<header status=\"deleted\">\n";
69 }
70 else {
71 $str .= "<header>\n";
72 }
73  
74 $str .= <<EOF;
75 <identifier>$id</identifier>
76 </header>
77 <metadata>
78 EOF
79 $str;
80 }
81  
82 sub _oai_record_footer {
83 my $str =<<EOF;
84 </metadata>
85 </record>
86 EOF
87 $str;
88 }
89  
90 sub _oai_footer {
91 my $str =<<EOF;
92 </ListRecords>
93 </OAI-PMH>
94 EOF
95 $str;
96 }
97  
98 1;
99  
100 __END__