OPDS/Acquisition.pm

filter: Statementstmt 97.0% 98 / 101 Branchbran 88.4% 23 / 26 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 88.2% 15 / 17 Podpod 100% 4 / 4 totaltotal 92.1% 141 / 153 CC 19 SCAR 14.8
CC
19
Cov
94%
files_as_links
8.0
authors_as_links
6.0
as_entry
6.0
CRAP 4.4
« OPDS.pm ↑ Summary OPDS/OpenSearch/Query.pm »
1 package XML::OPDS::Acquisition;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use Types::Standard qw/Str ArrayRef InstanceOf Object/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use XML::Atom;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use XML::Atom::Person;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use XML::Atom::Entry;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =head1 NAME
13  
14 XML::OPDS::Acquisition - Acquisition elements for OPDS feeds
15  
16 =head1 SETTERS/ACCESSORS
17  
18 The following accessors are read-only and are meant to be passed as an
19 hash to the C<new> constructor.
20  
21 =head2 id
22  
23 Optional. The example specification uses uuid like
24 C<urn:uuid:2853dacf-ed79-42f5-8e8a-a7bb3d1ae6a2>. If not provided, the
25 C<href> (with the prefix if provided) will be used instead.
26  
27 =head2 prefix
28  
29 If provided, every uri will have this string prepended, so you can
30 just pass URIs like '/path/to/file' and have them consistently turned
31 to 'http://myserver.org/path/to/file' if you set this to
32 'http://myserver.org'. See also L<XML::OPDS> C<prefix> method.
33  
34 =cut
35  
36 5 has id => (is => 'ro', isa => Str);
37  
38 2 ▶has prefix => (is => 'ro', isa => Str, default => sub { '' });
Subroutine __ANON__: called CC=1 SCAR=0.0
39  
40 =head2 href
41  
42 The URI of the resource. Required.
43  
44 =cut
45  
46 5 has href => (is => 'ro', isa => Str, required => 1);
47  
48 =head2 title
49  
50 The title. Required.
51  
52 =cut
53  
54 5 has title => (is => 'ro', isa => Str, required => 1);
55  
56 =head2 files
57  
58 An arrayref with the download files. The prefix is added if set.
59  
60 =head1 OPTIONAL ATTRIBUTES
61  
62 The following attributes are optional and describe the publication.
63  
64 =head2 authors
65  
66 An arrayref of either scalars with names, or hashrefs with C<name> and
67 C<uri> as keys. C<uri> is optional.
68  
69 =head2 summary
70  
71 Plain text.
72  
73 =head2 description
74  
75 HTML allowed.
76  
77 =head2 language
78  
79 =head2 issued
80  
81 The publication date.
82  
83 =head2 publisher
84  
85 =head2 updated
86  
87 A DateTime object with the time of the last update of the resource.
88  
89 =cut
90  
91 5 has authors => (is => 'ro', isa => ArrayRef);
92  
93 5 has summary => (is => 'ro', isa => Str);
94  
95 5 has description => (is => 'ro', isa => Str);
96  
97 5 has language => (is => 'ro', isa => Str);
98  
99 5 has issued => (is => 'ro', isa => Str);
100  
101 5 has publisher => (is => 'ro', isa => Str);
102  
103 has updated => (is => 'rw', isa => InstanceOf['DateTime'],
104 0 ▶ default => sub { return DateTime->now });
Subroutine __ANON__: not called CC=1 SCAR=1.2
105  
106 0 ▶has files => (is => 'ro', isa => ArrayRef[Str], default => sub { [] });
Subroutine __ANON__: not called CC=1 SCAR=1.2
107  
108 7 ▶has _dt_formatter => (is => 'ro', isa => Object, default => sub { DateTime::Format::RFC3339->new });
Subroutine __ANON__: called CC=1 SCAR=0.0
109  
110 =head2 thumbnail
111  
112 The uri of the thumbnail
113  
114 =head2 image
115  
116 The uri of the image
117  
118 =head1 METHODS
119  
120 Usually they are for internal usage.
121  
122 =head2 identifier
123  
124 =head2 authors_as_links
125  
126 Return a list of L<XML::Atom::Person> objects from the C<authors>
127 value.
128  
129 =head2 files_as_links
130  
131 Return a list of L<XML::Atom::Link> objects constructed from C<files>,
132 C<image>, C<thumbnail>, with the appropriate C<rel> and C<type>.
133  
134 =head2 as_entry
135  
136 Return the acquisition L<XML::Atom::Entry> object.
137  
138 =cut
139  
140 5 has thumbnail => (is => 'ro', isa => Str);
141  
142 5 has image => (is => 'ro', isa => Str);
143  
144 has _dc => (is => 'lazy',
145 isa => Object,
146 1 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
147 1 XML::Atom::Namespace->new(dc => 'http://purl.org/dc/elements/1.1/');
148 5 });
149  
150 18 ▶sub identifier {
Subroutine identifier: called CC=2 SCAR=8.1
Pod documented
151 18 my $self = shift;
152 18 ▶ return $self->id || $self->prefix . $self->href;
Condition33%1/3
$self->id || $self->prefix . $self->href
l!l&&r!l&&!r
0180
MC/DC0%0/2
$self->id || $self->prefix . $self->href
$self->id $self->prefix . $self->href
Truth table33%1/3
A || B
A= $self->idB= $self->prefix . $self->href
ABresult
1X1
011
000
153 }
154  
155 18 ▶sub authors_as_links {
Subroutine authors_as_links: called CC=6 SCAR=17.9
Pod documented
156 18 my $self = shift;
157 18 my @out;
158 18 ▶ if (my $authors = $self->authors) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $authors = $self->authors) 1 17 18
159 1 foreach my $author (@$authors) {
160 2 ▶ my $hash = ref($author) ? $author : { name => $author };
Branch: 1 branch
BranchTrueFalseTotal
ref $author ? : 1 1 2
161 2 ▶ if (my $name = $hash->{name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $name = $hash->{"name"}) 2 0 2
162 2 my $author_obj = XML::Atom::Person->new(Version => 1.0);
163 2 $author_obj->name($hash->{name});
164 2 ▶ if (my $uri = $hash->{uri}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $uri = $hash->{"uri"}) 1 1 2
165 1 $author_obj->uri($self->prefix . $uri);
166 }
167 2 push @out, $author_obj;
168 }
169 }
170 }
171 18 return @out;
172 }
173  
174 18 ▶sub files_as_links {
Subroutine files_as_links: called CC=8 SCAR=20.8
Pod documented
175 18 my $self = shift;
176 18 my @out;
177 18 my %mime = (
178 tex => 'application/x-tex',
179 pdf => 'application/pdf',
180 html => 'text/html',
181 epub => 'application/epub+zip',
182 muse => 'text/plain',
183 txt => 'text/plain',
184 zip => 'application/zip',
185 png => 'image/png',
186 jpg => 'image/jpeg',
187 jpeg => 'image/jpeg',
188 gif => 'image/gif',
189 mobi => 'application/x-mobipocket-ebook',
190 );
191 # maybe support open-access, borrow, buy, sample, subscribe ? 8.4.1
192 18 my @all = map { +{ rel => 'acquisition', href => $_ } } @{$self->files};
193 18 ▶ die "Missing acquisition links" unless @all;
Branch: 1 branch
BranchTrueFalseTotal
unless @all 0 18 18
194  
195 18 ▶ if (my $thumb = $self->thumbnail) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $thumb = $self->thumbnail) 5 13 18
196 5 push @all, { rel => 'image/thumbnail', href => $thumb };
197 }
198 18 ▶ if (my $image = $self->image) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $image = $self->image) 5 13 18
199 5 push @all, { rel => 'image', href => $image };
200 }
201 18 foreach my $file (@all) {
202 30 my $mime_type;
203 30 ▶ if ($file->{href} =~ m/\.(\w+)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file->{"href"} =~ /\.(\w+)\z/) 28 2 30
204 28 my $ext = $1;
205 28 $mime_type = $mime{$ext};
206 }
207 30 ▶ next unless $mime_type;
Branch: 1 branch
BranchTrueFalseTotal
unless $mime_type 2 28 30
208 28 my $link = XML::Atom::Link->new(Version => 1.0);
209 28 $link->rel("http://opds-spec.org/$file->{rel}");
210 28 $link->href($self->prefix . $file->{href});
211 28 $link->type($mime_type);
212 28 push @out, $link;
213 }
214 18 ▶ if (@out) {
Branch: 1 branch
BranchTrueFalseTotal
if (@out) { } 18 0 18
215 18 return @out;
216 }
217 else {
218 0 die "Links are required"
219 };
220 }
221  
222 18 ▶sub as_entry {
Subroutine as_entry: called CC=6 SCAR=17.9
Pod documented
223 18 my $self = shift;
224 18 my $entry = XML::Atom::Entry->new(Version => 1.0);
225 18 $entry->id($self->identifier);
226 18 $entry->title($self->title);
227 18 $entry->updated($self->_dt_formatter->format_datetime($self->updated));
228 18 ▶ if (my $lang = $self->language) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $lang = $self->language) 1 17 18
229 1 $entry->set($self->_dc, language => $lang);
230 }
231 18 foreach my $author ($self->authors_as_links) {
232 2 $entry->add_author($author);
233 }
234 18 ▶ if (my $summary = $self->summary) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $summary = $self->summary) 1 17 18
235 1 $entry->summary($summary);
236 }
237 18 ▶ if (my $desc = $self->description) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $desc = $self->description) 5 13 18
238 5 $entry->content($desc);
239 }
240 18 foreach my $link ($self->files_as_links) {
241 28 $entry->add_link($link);
242 }
243 18 return $entry;
244 }
245  
246 5 1;
« OPDS.pm ↑ Summary OPDS/OpenSearch/Query.pm »