File Coverage

blib/lib/Catmandu/Store/RKD/API/Parse.pm
Criterion Covered Total %
statement 4 6 66.6
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 6 8 75.0


line stmt bran cond sub pod time code
1             package Catmandu::Store::RKD::API::Parse;
2              
3 3     3   12 use Moo;
  3         3  
  3         17  
4 3     3   1603 use XML::Struct qw(readXML);
  0            
  0            
5              
6             use Catmandu::Sane;
7              
8             has results => (is => 'ro', required => 1);
9              
10             has items => (is => 'lazy');
11              
12             sub _build_items {
13             my $self = shift;
14             return $self->parse($self->results);
15             }
16              
17             ##
18             # Parse the result. Contains the items, but also the total amount of results,
19             # the items per page and the starting item. This allows you to paginate through
20             # the API.
21             sub parse {
22             my ($self, $results) = @_;
23             my $tree = readXML($results, simple => 1);
24             my $items = $tree->{'channel'}->{'item'};
25             if (ref($items) ne ref([])) {
26             $items = [$items];
27             }
28             my $total = $tree->{'channel'}->{'opensearch:totalResults'};
29             my $per_page = $tree->{'channel'}->{'opensearch:itemsPerPage'};
30             my $start = $tree->{'channel'}->{'opensearch:startIndex'};
31             return {
32             'items' => $items,
33             'total' => $total,
34             'per_page' => $per_page,
35             'start' => $start
36             };
37             }
38              
39             1;