Exporter/MARC/MiJ.pm

filter: Statementstmt 97.1% 34 / 35 Branchbran 50.0% 1 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod 0.0% 0 / 2 totaltotal 91.8% 45 / 49 CC 2 SCAR 1.7
CC
2
Cov
95%
add
2.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.2
« Fix/Condition/marc_has_many.pm ↑ Summary Fix/Inline/marc_add.pm »
1 =head1 NAME
2  
3 Catmandu::Exporter::MARC::MiJ - Exporter for MARC records to MARC in JSON
4  
5 =head1 SYNOPSIS
6  
7 # From the command line
8 $ catmandu convert MARC --type XML to MARC --type MiJ < /foo/bar.xml
9  
10 # From Perl
11 use Catmandu;
12  
13 my $importer = Catmandu->importer('MARC', file => "/foo/bar.xml" , type => 'XML');
14 my $exporter = Catmandu->exporter('MARC', file => "bar.json", type => 'MiJ' );
15  
16 $exporter->add($importer);
17 $exporter->commit;
18  
19 =head1 CONFIGURATION
20  
21 =over
22  
23 =item file
24  
25 Write output to a local file given by its path or file handle. Alternatively a
26 scalar reference can be passed to write to a string and a code reference can be
27 used to write to a callback function.
28  
29 =item fh
30  
31 Write the output to an L<IO::Handle>. If not specified,
32 L<Catmandu::Util::io|Catmandu::Util/IO-functions> is used to create the output
33 handle from the C<file> argument or by using STDOUT.
34  
35 =item fix
36  
37 An ARRAY of one or more fixes or file scripts to be applied to exported items.
38  
39 =item encoding
40  
41 Binmode of the output stream C<fh>. Set to "C<:utf8>" by default.
42  
43 =back
44  
45 =head1 METHODS
46  
47 See L<Catmandu::Exporter>, L<Catmandu::Addable>, L<Catmandu::Fixable>,
48 L<Catmandu::Counter>, and L<Catmandu::Logger> for a full list of methods.
49  
50 =head1 FORMAT
51  
52 The MARC-in-JSON record format contains two fields:
53  
54 * 'leader' - the MARC leader
55 * 'fields' - an array of MARC fields
56  
57 Each item in the MARC fields array contains the MARC tag and as value a hash
58 containing three fields:
59  
60 * 'subfields' - an array of MARC subfields
61 * 'ind1' - the first indicator of the MARC tag
62 * 'ind2' - the second indicator of the MARC tag
63  
64 Each subfield item is an hash containing the MARC subfield tag and its value.
65  
66 An example of one MARC record in the MiJ serialization format is given below:
67  
68 {
69 "leader": "0000cam 2200000 4500",
70 "fields": [
71 {
72 "100": {
73 "subfields": [
74 {
75 "a": "Huberman, Leo,"
76 },
77 {
78 "d": "1903-1968."
79 }
80 ],
81 "ind1": "1",
82 "ind2": " "
83 }
84 },
85 {
86 "700": {
87 "subfields": [
88 {
89 "a": "Sweezy, Paul M."
90 },
91 {
92 "q": "(Paul Marlor),"
93 },
94 {
95 "d": "1910-2004."
96 }
97 ],
98 "ind1": "1",
99 "ind2": " "
100 }
101 },
102 ...
103 }
104  
105 =head1 SEE ALSO
106  
107 L<Catmandu::Exporter>
108  
109 =cut
110 package Catmandu::Exporter::MARC::MiJ;
111 1 ▶use Catmandu::Sane;
Subroutine BEGIN: called CC=1 SCAR=0.0
112 1 ▶use Catmandu::Util qw(xml_escape is_different :array :is);
Subroutine BEGIN: called CC=1 SCAR=0.0
113 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
114 1 ▶use MARC::Record;
Subroutine BEGIN: called CC=1 SCAR=0.0
115 1 ▶use MARC::Field;
Subroutine BEGIN: called CC=1 SCAR=0.0
116 1 ▶use MARC::File::MiJ;
Subroutine BEGIN: called CC=1 SCAR=0.0
117  
118 1 our $VERSION = '1.33';
119  
120 1 with 'Catmandu::Exporter', 'Catmandu::Exporter::MARC::Base';
121  
122 1 ▶has record => (is => 'ro' , default => sub { 'record'});
Subroutine __ANON__: called CC=1 SCAR=0.0
123 1 ▶has record_format => (is => 'ro' , default => sub { 'raw'} );
Subroutine __ANON__: called CC=1 SCAR=0.0
124  
125 1 ▶sub add {
Subroutine add: called CC=2 SCAR=7.1
Pod undocumented
126 1 my ($self, $data) = @_;
127  
128 1 ▶ if ($self->record_format eq 'MARC-in-JSON') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->record_format eq "MARC-in-JSON") 0 1 1
129 0 $data = $self->_json_to_raw($data);
130 }
131  
132 1 my $marc = $self->_raw_to_marc_record($data->{$self->record});
133  
134 1 my $res = MARC::File::MiJ::encode($marc);
135 1 utf8::decode($res);
136 1 $self->fh->print("$res\n");
137 }
138  
139 1 ▶sub commit {
Subroutine commit: called CC=1 SCAR=0.0
Pod undocumented
140 1 my ($self) = @_;
141 1 $self->fh->flush;
142  
143 1 1;
144 }
145  
146 1 1;
« Fix/Condition/marc_has_many.pm ↑ Summary Fix/Inline/marc_add.pm »