OPDS.pm

filter: Statementstmt 93.5% 160 / 171 Branchbran 70.5% 24 / 34 Conditioncond 60.0% 3 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 97.1% 34 / 35 Podpod 100% 10 / 10 totaltotal 89.9% 232 / 258 CC 29 SCAR 18.9
CC
29
Cov
89%
atom
17.3
navigation_entries
12.0
navigation_hash
6.1
CRAP 6.6
↑ Summary OPDS/Acquisition.pm »
1 package XML::OPDS;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Types::Standard qw/Str Object ArrayRef InstanceOf Maybe Int/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use DateTime::Format::RFC3339;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use XML::Atom;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use XML::Atom::Feed;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use XML::Atom::Entry;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use XML::OPDS::Navigation;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use XML::OPDS::Acquisition;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use XML::OPDS::OpenSearch::Query;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 =head1 NAME
17  
18 XML::OPDS - OPDS (Open Publication Distribution System) feed creation
19  
20 =head1 VERSION
21  
22 Version 0.05
23  
24 =cut
25  
26 4 our $VERSION = '0.05';
27  
28  
29 =head1 DESCRIPTION
30  
31 This module facilitates the creation of OPDS feeds.
32  
33 The specifications can be found at L<http://opds-spec.org/> while the
34 validator is at L<http://opds-validator.appspot.com/>.
35  
36 The idea is that it should be enough to pass the navigation links and
37 the title entries with some data, and have the feed back.
38  
39 The OPDS feeds are basically Atom feeds, hence this module uses
40 L<XML::Atom> under the hood.
41  
42 Some features are not supported yet, but patches are welcome. Also
43 keep in mind that the applications which are supposed to talk to your
44 server have a level of support which varies from decent to "keep
45 crashing".
46  
47 This is still very much a work in progress, but it's already
48 generating valid and usable catalogs.
49  
50 =head1 SYNOPSIS
51  
52 use XML::OPDS;
53 my $feed = XML::OPDS->new(prefix => 'http://amusewiki.org');
54 # add two links, self and start are mandatory.
55 $feed->add_to_navigations(
56 rel => 'self',
57 title => 'Root',
58 href => '/',
59 );
60 $feed->add_to_navigations(
61 rel => 'start',
62 title => 'Root',
63 href => '/',
64 );
65 # add a navigation for the title list, marking as leaf, where the
66 # download links can be retrieved.
67 $feed->add_to_navigations(
68 title => 'Titles',
69 description => 'texts sorted by title',
70 href => '/titles',
71 acquisition => 1,
72 );
73 # and render
74 print $feed->render;
75 # you can reuse the object for leaf feeds (i.e. the acquistion
76 # feeds), pushing another self navigation, which will replace the
77 # previous one.
78 $feed->add_to_navigations(
79 rel => 'self',
80 title => 'Titles',
81 description => 'texts sorted by title',
82 href => '/titles',
83  
84 );
85 # or, implicitely setting the self rel and cleaning the navigation
86 # stash, keeping the meta
87 $feed->add_to_navigations_new_level(
88 title => 'Titles',
89 acquisition => 1,
90 href => '/titles',
91 );
92 $feed->add_to_acquisitions(
93 href => '/my/title',
94 title => 'My title',
95 files => [ '/my/title.epub' ],
96 );
97 # and here we have an acquisition feed, because of the presence of
98 # the acquisition.
99 print $feed->render;
100  
101 =head1 ENCODING
102  
103 Even if the module wants characters as input (decoded strings, not
104 bytes), the output XML is an UTF-8 encoded string.
105  
106 =head1 SETTERS/ACCESSORS
107  
108 =head2 navigations
109  
110 Arrayref of L<XML::OPDS::Navigation> objects. An object with a rel
111 C<self> (the feed itself) and one with the rel C<start> (the root
112 feed) are mandatory. If not present, the module will crash while
113 rendering the feed.
114  
115 =head2 acquisitions
116  
117 Arrayref of L<XML::OPDS::Acquisition> objects. If one or more objects
118 are present, the feed will become an acquistion feed.
119  
120 =head2 author
121  
122 The producer of the feed. Defaults to this class name and version.
123  
124 =head2 author_uri
125  
126 The uri of the author. Defaults to L<http://amusewiki.org> (which is
127 the home of this class).
128  
129 =head2 prefix
130  
131 Default to the empty string. On instances of this class, by itself has
132 no effect. However, when calling C<add_to_acquisitions> and
133 C<add_to_navigations>, it will be passed to the constructors of those
134 objects.
135  
136 This is usually the hostname of the OPDS server. So you need just to
137 pass, e.g. 'http://amusewiki.org' and have all the links prefixed by
138 that (no slash mangling or adding is performed). If you are going to
139 pass the full urls, leave it at the default.
140  
141 =head2 updated
142  
143 Default to current timestamp. When calling C<create_navigation> or
144 C<create_acquistion>, use this timestamp as default.
145  
146 =head2 logo
147  
148 The feed logo. If prefix is set, prepend it.
149  
150 =head2 icon
151  
152 The feed icon. If prefix is set, prepend it.
153  
154 =head1 METHODS
155  
156 =head2 render
157  
158 Return the generated xml.
159  
160 =head2 atom
161  
162 Return the L<XML::Atom::Feed> object.
163  
164 =head2 create_navigation(%args)
165  
166 Create a L<XML::OPDS::Navigation> object inheriting the prefix.
167  
168 =head2 create_acquisition(%args)
169  
170 Create a L<XML::OPDS::Acquisition> object inheriting the prefix.
171  
172 =head2 add_to_navigations(%args)
173  
174 Call C<create_navigation> and add it to the C<navigations> stack.
175  
176 =head2 add_to_navigations_new_level(%args)
177  
178 Like C<add_to_navigations>, but it's meant to be used for
179 C<rel:self> elements.
180  
181 The C<rel:self> attribute is injected in the arguments which are
182 passed to C<create_navigation>.
183  
184 If a navigation with the attribute C<rel> set to C<self> was already
185 present in the stack, the new one will become the new C<self>, while
186 the old one will become an C<up> rel.
187  
188 Also, this will remove any existing navigation with the C<rel>
189 attribute set to C<subsection>, given that you are creating a new
190 level.
191  
192 This is designed to play well with chained actions (so you can reuse
193 the object, stack selfs, and the result will be correct).
194  
195 =head2 add_to_acquisitions(%args)
196  
197 Call C<create_acquisition> and add it to the C<acquisition> stack.
198  
199 =head1 INTERNAL METHODS
200  
201 =head2 navigation_entries
202  
203 Return a list of L<XML::OPDS::Navigation> objects excluding unique
204 relationships like C<self>, C<start>, C<up>, C<previous>, C<next>,
205 C<first>, C<last>.
206  
207 =head2 navigation_hash
208  
209 Return an hashref, where the keys are the C<rel> attributes of the
210 navigation objects. The value is an object if the navigation is meant
211 to be unique, or an arrayref of objects if not so.
212  
213 =head2 is_acquisition
214  
215 Return true if there are acquisition objects stacked.
216  
217 =cut
218  
219 has navigations => (is => 'rw',
220 isa => ArrayRef[InstanceOf['XML::OPDS::Navigation']],
221 2 ▶ default => sub { [] });
Subroutine __ANON__: called CC=1 SCAR=0.0
222 has acquisitions => (is => 'rw',
223 isa => ArrayRef[InstanceOf['XML::OPDS::Acquisition']],
224 3 ▶ default => sub { [] });
Subroutine __ANON__: called CC=1 SCAR=0.0
225 1 ▶has author => (is => 'rw', isa => Str, default => sub { __PACKAGE__ . ' ' . $VERSION });
Subroutine __ANON__: called CC=1 SCAR=0.0
226 3 ▶has author_uri => (is => 'rw', isa => Str, default => sub { 'http://amusewiki.org' });
Subroutine __ANON__: called CC=1 SCAR=0.0
227 2 ▶has prefix => (is => 'rw', isa => Str, default => sub { '' });
Subroutine __ANON__: called CC=1 SCAR=0.0
228 2 ▶has updated => (is => 'rw', isa => Object, default => sub { DateTime->now });
Subroutine __ANON__: called CC=1 SCAR=0.0
229 2 ▶has icon => (is => 'rw', isa => Str, default => sub { '' });
Subroutine __ANON__: called CC=1 SCAR=0.0
230 2 ▶has logo => (is => 'rw', isa => Str, default => sub { '' });
Subroutine __ANON__: called CC=1 SCAR=0.0
231  
232 has _dt_formatter => (is => 'ro', isa => Object,
233 3 ▶ default => sub { DateTime::Format::RFC3339->new });
Subroutine __ANON__: called CC=1 SCAR=0.0
234 has _fh => (is => 'ro',
235 isa => Object,
236 default => sub {
237 3 ▶ XML::Atom::Namespace->new(fh => 'http://purl.org/syndication/history/1.0');
Subroutine __ANON__: called CC=1 SCAR=0.0
238 4 });
239  
240 # opensearch accessors
241  
242 has _os => (is => 'ro',
243 isa => Object,
244 default => sub {
245 3 ▶ XML::Atom::Namespace->new(opensearch => 'http://a9.com/-/spec/opensearch/1.1/');
Subroutine __ANON__: called CC=1 SCAR=0.0
246 4 });
247  
248  
249 =head1 OPENSEARCH RESULTS
250  
251 The following attributes can be set if you are building an Atom
252 response for OpenSearch. See L<XML::OPDS::OpenSearch::Query> for a
253 concrete example.
254  
255 =head2 search_result_pager
256  
257 A L<Data::Page> object with the specification of the pages.
258  
259 =head2 search_result_terms
260  
261 A string with the query for which you are serving the results.
262  
263 =head2 search_result_queries
264  
265 Additional Query elements, should be an arrayref of
266 L<XML::OPDS::OpenSearch::Query> objects.
267  
268 =cut
269  
270 4 has search_result_pager => (is => 'rw',
271 isa => InstanceOf['Data::Page']);
272  
273 4 has search_result_terms => (is => 'rw',
274 isa => Str);
275  
276 has search_result_queries => (is => 'rw',
277 isa => ArrayRef[InstanceOf['XML::OPDS::OpenSearch::Query']],
278 3 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
279 4 );
280  
281 sub navigation_entries {
282 0 ▶ my $self = shift;
Subroutine navigation_entries: not called CC=3 SCAR=24.8
Pod documented
283 0 my $hash = $self->navigation_hash;
284 0 my @others;
285 0 foreach my $k (sort keys %$hash) {
286 0 my $entries = $hash->{$k};
287 # exclude the uniques
288 0 ▶ if (ref($entries) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $entries eq "ARRAY") 0 0 0
289 0 push @others, @$entries;
290 }
291 }
292 0 return @others;
293 }
294  
295 sub navigation_hash {
296 9 ▶ my $self = shift;
Subroutine navigation_hash: called CC=6 SCAR=18.1
Pod documented
297 9 my $navs = $self->navigations;
298 9 ▶ die "Missing navigations" unless $navs && @$navs;
Branch: 1 branch
BranchTrueFalseTotal
unless $navs and @$navs 0 9 9
Condition33%1/3
$navs and @$navs
!ll&&!rl&&r
009
MC/DC0%0/2
$navs and @$navs
$navs @$navs
Truth table33%1/3
A and B
A= $navsB= @$navs
ABresult
0X0
100
111
299 9 my %out;
300 9 my %uniques = (
301 start => 1,
302 self => 1,
303 up => 1,
304 next => 1,
305 previous => 1,
306 first => 1,
307 last => 1,
308 search => 1,
309 crawlable => 1,
310 );
311 9 foreach my $nav (@$navs) {
312 32 my $rel = $nav->rel;
313 # uniques
314 32 ▶ if ($uniques{$rel}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uniques{$rel}) { } 29 3 32
315 29 $out{$rel} = $nav;
316 }
317 else {
318 3 ▶ $out{$rel} ||= [];
Condition100%2/2
$out{$rel} ||= []
l!l
12
MC/DC100%1/1
$out{$rel} ||= []
$out{$rel}
Truth table100%2/2
A
A= $out{$rel}
Aresult
11
00
319 3 push @{$out{$rel}}, $nav;
320 }
321 }
322 9 return \%out;
323 }
324  
325 sub is_acquisition {
326 9 ▶ if (my $acquisitions = shift->acquisitions) {
Subroutine is_acquisition: called CC=2 SCAR=8.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if (my $acquisitions = (shift())->acquisitions) { } 9 0 9
327 9 return scalar(@$acquisitions);
328 }
329 else {
330 0 return 0;
331 }
332 }
333  
334 sub _is_paged {
335 7 ▶ my $self = shift;
Subroutine _is_paged: called CC=3 SCAR=11.0
336 7 my $partial = 0;
337 7 foreach my $nav (@{$self->navigations}) {
338 23 ▶ if ($nav->rel =~ m/\A(next|previous|first|last)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nav->rel =~ /\A(next|previous|first|last)\z/) 2 21 23
339 2 $partial = 1;
340 2 last;
341 }
342 }
343 7 return $partial;
344 }
345  
346 sub atom {
347 9 ▶ my $self = shift;
Subroutine atom: called CC=17 SCAR=28.5
Pod documented
348 9 my $feed = XML::Atom::Feed->new(Version => 1.0);
349 9 my $navs = $self->navigation_hash;
350 9 my $main = delete $navs->{self};
351 9 ▶ die "Missing self navigation element!" unless $main;
Branch: 1 branch
BranchTrueFalseTotal
unless $main 0 9 9
352 9 $feed->id($main->identifier);
353 9 $feed->add_link($main->as_link);
354 9 my @nav_entries;
355 9 foreach my $rel (sort keys %$navs) {
356 # use only the unique
357 22 my $nav = delete $navs->{$rel};
358 22 ▶ if (ref($nav) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $nav eq 'ARRAY') { } 2 20 22
359 2 push @nav_entries, @$nav;
360 }
361 else {
362 20 $feed->add_link($nav->as_link);
363 }
364 }
365 9 $feed->title($main->title);
366 9 $feed->updated($self->_dt_formatter->format_datetime($main->updated));
367 9 ▶ if (my $icon = $self->icon) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $icon = $self->icon) 2 7 9
368 2 $feed->icon($self->prefix . $icon);
369 }
370 9 ▶ if (my $logo = $self->logo) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $logo = $self->logo) 2 7 9
371 2 $feed->logo($self->prefix . $logo);
372 }
373 9 ▶ if (my $author_name = $self->author) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $author_name = $self->author) 9 0 9
374 9 my $author = XML::Atom::Person->new(Version => 1.0);
375 9 $author->name($author_name);
376 9 ▶ if (my $author_uri = $self->author_uri) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $author_uri = $self->author_uri) 9 0 9
377 9 $author->uri($author_uri);
378 }
379 9 $feed->author($author);
380 }
381  
382 # opensearch element
383 # http://www.opensearch.org/Specifications/OpenSearch/1.1#OpenSearch_response_elements
384 9 ▶ if (my $pager = $self->search_result_pager) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pager = $self->search_result_pager) 5 4 9
385 5 $feed->set($self->_os, totalResults => $pager->total_entries);
386 5 $feed->set($self->_os, startIndex => $pager->first);
387 5 $feed->set($self->_os, itemsPerPage => $pager->entries_per_page);
388 5 ▶ if (my $term = $self->search_result_terms) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $term = $self->search_result_terms) 4 1 5
389 4 my $query = XML::OPDS::OpenSearch::Query->new(
390 role => 'request',
391 searchTerms => $term,
392 );
393 4 $feed->add($self->_os, Query => undef, $query->attributes_hashref);
394 }
395 }
396 9 foreach my $query (@{ $self->search_result_queries }) {
397 4 $feed->add($self->_os, Query => undef, $query->attributes_hashref);
398 }
399 9 ▶ if ($self->is_acquisition) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_acquisition) { } 7 2 9
400 # if it's an acquisition feed, stuff the links in the feed,
401 # but filter out the subsections. And probably other stuff as well.
402 7 foreach my $link (@nav_entries) {
403 0 my %rels = (related => 1, alternate => 1);
404 0 ▶ $feed->add_link($link->as_link) if $rels{$link->rel};
Branch: 1 branch
BranchTrueFalseTotal
if $rels{$link->rel} 0 0 0
405 }
406 7 ▶ unless ($self->_is_paged) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_is_paged) 5 2 7
407 5 $feed->set($self->_fh, complete => undef);
408 }
409 7 foreach my $entry (@{$self->acquisitions}) {
410 8 $feed->add_entry($entry->as_entry);
411 }
412 }
413 else {
414 # othewise use the links to create entries
415 2 foreach my $entry (@nav_entries) {
416 3 $feed->add_entry($entry->as_entry);
417 }
418 }
419 9 return $feed;
420 }
421  
422 sub render {
423 8 ▶ shift->atom->as_xml;
Subroutine render: called CC=1 SCAR=0.0
Pod documented
424 }
425  
426 sub create_navigation {
427 9 ▶ my $self = shift;
Subroutine create_navigation: called CC=1 SCAR=0.0
Pod documented
428 9 return XML::OPDS::Navigation->new(prefix => $self->prefix,
429 updated => $self->updated,
430 @_);
431 }
432  
433 sub add_to_navigations {
434 6 ▶ my $self = shift;
Subroutine add_to_navigations: called CC=1 SCAR=0.0
Pod documented
435 6 my $navigation = $self->create_navigation(@_);
436 6 push @{$self->navigations}, $navigation;
437 6 return $navigation;
438 }
439  
440 sub add_to_navigations_new_level {
441 3 ▶ my $self = shift;
Subroutine add_to_navigations_new_level: called CC=3 SCAR=11.0
Pod documented
442 3 my $navigation = $self->create_navigation(rel => 'self', @_);
443 # turn the previous self in an "up" link.
444 3 ▶ if ($navigation->rel eq 'self') {
Branch: 1 branch
BranchTrueFalseTotal
if ($navigation->rel eq "self") 3 0 3
445 # new level, so remove the subsections
446 3 my @existing = grep { $_->rel ne 'subsection' } @{$self->navigations};
447 # promote the existing self to "up"
448 3 foreach my $previous (grep { $_->rel eq 'self' } @existing) {
449 1 $previous->rel('up');
450 }
451 # reset
452 3 $self->navigations(\@existing);
453 }
454 3 push @{$self->navigations}, $navigation;
455 3 return $navigation;
456 }
457  
458 sub create_acquisition {
459 2 ▶ my $self = shift;
Subroutine create_acquisition: called CC=1 SCAR=0.0
Pod documented
460 2 return XML::OPDS::Acquisition->new(prefix => $self->prefix,
461 updated => $self->updated,
462 @_);
463 }
464  
465 sub add_to_acquisitions {
466 2 ▶ my $self = shift;
Subroutine add_to_acquisitions: called CC=1 SCAR=0.0
Pod documented
467 2 my $acquisition = $self->create_acquisition(@_);
468 2 push @{$self->acquisitions}, $acquisition;
469 2 return $acquisition;
470 }
471  
472  
473 =head1 AUTHOR
474  
475 Marco Pessotto, C<< <melmothx at gmail.com> >>
476  
477 =head1 BUGS
478  
479 Please report any bugs or feature requests to C<bug-xml-opds at rt.cpan.org>, or through
480 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=XML-OPDS>. I will be notified, and then you'll
481 automatically be notified of progress on your bug as I make changes.
482  
483 =head1 SUPPORT
484  
485 You can find documentation for this module with the perldoc command.
486  
487 perldoc XML::OPDS
488  
489  
490 You can also look for information at:
491  
492 =over 4
493  
494 =item * RT: CPAN's request tracker (report bugs here)
495  
496 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=XML-OPDS>
497  
498 =item * MetaCPAN
499  
500 L<http://metacpan.org/pod/XML::OPDS>
501  
502 =back
503  
504 =head1 LICENSE AND COPYRIGHT
505  
506 Copyright 2016 Marco Pessotto.
507  
508 This program is free software; you can redistribute it and/or modify it
509 under the terms of either: the GNU General Public License as published
510 by the Free Software Foundation; or the Artistic License.
511  
512 See L<http://dev.perl.org/licenses/> for more information.
513  
514  
515 =cut
516  
517 4 1; # End of XML::OPDS
↑ Summary OPDS/Acquisition.pm »