File Coverage

lib/Business/Fixflo/Paginator.pm
Criterion Covered Total %
statement 32 34 94.1
branch 4 6 66.6
condition 1 2 50.0
subroutine 8 8 100.0
pod 0 2 0.0
total 45 52 86.5


line stmt bran cond sub pod time code
1             package Business::Fixflo::Paginator;
2              
3             =head1 NAME
4              
5             Business::Fixflo::Paginator
6              
7             =head1 DESCRIPTION
8              
9             A class for pagination through fixflo data returned as a list.
10              
11             =cut
12              
13 16     16   126 use strict;
  16         39  
  16         586  
14 16     16   82 use warnings;
  16         59  
  16         527  
15              
16 16     16   96 use Moo;
  16         39  
  16         125  
17 16     16   6052 use JSON ();
  16         58  
  16         466  
18              
19 16     16   6747 use Business::Fixflo::Issue;
  16         78  
  16         6837  
20              
21             =head1 ATTRIBUTES
22              
23             client
24             objects
25             class
26             links
27             total_items
28             total_pages
29              
30             =cut
31              
32             has [ qw/ client objects class links total_items total_pages / ] => (
33             is => 'rw'
34             );
35              
36             =head1 PAGER METHODS
37              
38             next
39             previous
40              
41             Return the current set of objects and then gets the next/previous page:
42              
43             my @objects = @{ $Paginator->next };
44              
45             =head2 objects
46              
47             Gets the current set of objects
48              
49             my @objects = @{ $Paginator->objects };
50              
51             =head2 links
52              
53             Returns a hash that has the NextURL and previousURL within
54              
55             my $urls = $Paginator->links
56              
57             =cut
58              
59             sub next {
60 4     4 0 6237 my ( $self ) = @_;
61              
62 4 50 50     13 if ( my @objects = @{ $self->objects // [] } ) {
  4         48  
63             # get the next chunk and return the current chunk
64 4         20 $self->objects( $self->_objects_from_page( 'next' ) );
65 4         42 return [ @objects ];
66             }
67              
68 0         0 return;
69             }
70              
71             sub previous {
72 1     1 0 11156 my ( $self ) = @_;
73 1         7 return $self->_objects_from_page( 'previous' );
74             }
75              
76             sub _objects_from_page {
77              
78 5     5   19 my ( $self,$page ) = @_;
79              
80             # see if we have more data to get
81 5 50       37 if ( my $url = $self->links->{$page} ) {
82              
83 5         33 my $data = $self->client->api_get( $url );
84 5         75 my $class = $self->class;
85              
86             my @objects = map {
87             $class->new(
88             client => $self->client,
89             # $_ might be a list of urls or list of hashes
90 15 100       317 ( ref( $_ ) ? ( %{ $_ } ) : ( url => $_ ) ),
  6         131  
91             )
92 5         15 } @{ $data->{Items} };
  5         19  
93              
94             $self->links({
95             next => $data->{NextURL},
96             previous => $data->{PreviousURL},
97 5         83 });
98              
99 5         103 return [ @objects ];
100             }
101              
102 0           return [];
103             }
104              
105             =head1 AUTHOR
106              
107             Lee Johnson - C
108              
109             This library is free software; you can redistribute it and/or modify it under
110             the same terms as Perl itself. If you would like to contribute documentation,
111             features, bug fixes, or anything else then please raise an issue / pull request:
112              
113             https://github.com/Humanstate/business-fixflo
114              
115             =cut
116              
117             1;
118              
119             # vim: ts=4:sw=4:et