File Coverage

blib/lib/Business/GoCardless/Paginator.pm
Criterion Covered Total %
statement 53 54 98.1
branch 3 4 75.0
condition 1 2 50.0
subroutine 14 14 100.0
pod 0 4 0.0
total 71 78 91.0


line stmt bran cond sub pod time code
1             package Business::GoCardless::Paginator;
2              
3             =head1 NAME
4              
5             Business::GoCardless::Paginator
6              
7             =head1 DESCRIPTION
8              
9             A class for pagination through gocardless data returned as a list.
10              
11             =cut
12              
13 19     19   134 use strict;
  19         47  
  19         577  
14 19     19   104 use warnings;
  19         54  
  19         452  
15              
16 19     19   99 use Moo;
  19         54  
  19         114  
17             extends 'Business::GoCardless::Resource';
18 19     19   6359 use JSON ();
  19         62  
  19         469  
19              
20 19     19   102 use Business::GoCardless::Bill;
  19         51  
  19         507  
21 19     19   120 use Business::GoCardless::PreAuthorization;
  19         41  
  19         519  
22 19     19   124 use Business::GoCardless::Payout;
  19         85  
  19         523  
23 19     19   141 use Business::GoCardless::User;
  19         74  
  19         515  
24 19     19   124 use Business::GoCardless::Paginator;
  19         54  
  19         14319  
25              
26             =head1 ATTRIBUTES
27              
28             client
29             objects
30             class
31             info
32             links
33              
34             =cut
35              
36             has [ qw/
37             client
38             objects
39             class
40             / ] => (
41             is => 'rw'
42             );
43              
44             has info => (
45             is => 'rw',
46             required => 1,
47             coerce => sub {
48             my ( $info ) = @_;
49              
50             return {} if ! $info;
51              
52             if ( $info =~ /^[{\[]/ ) {
53             # defensive decoding
54             eval { $info = JSON->new->decode( $info ) };
55             $@ && do { return "Failed to parse JSON response ($info): $@"; };
56             }
57             return $info;
58             }
59             );
60              
61             has links => (
62             is => 'rw',
63             required => 1,
64             coerce => sub {
65             my ( $links ) = @_;
66              
67             my $links_hash = {};
68             return $links_hash if ! $links;
69              
70             foreach my $link ( split( /, /,$links ) ) {
71             my ( $rel,$url ) = reverse split( />; /,$link );
72             $url =~ s/^
73             $rel =~ s/^.*?"([A-z]+)"/$1/;
74             $links_hash->{$rel} = $url;
75             }
76              
77             return $links_hash;
78             },
79             );
80              
81             =head1 PAGER METHODS
82              
83             next
84             previous
85             first
86             last
87              
88             Return the objects from the next/previous/first/last page:
89              
90             my @objects = $Paginator->next;
91              
92             =cut
93              
94             sub next {
95 3     3 0 14102 my ( $self ) = @_;
96              
97 3 100 50     5 if ( my @objects = @{ $self->objects // [] } ) {
  3         21  
98             # get the next chunk and return the current chunk
99 2         10 $self->objects( $self->_objects_from_page( 'next' ) );
100 2         29 return @objects;
101             }
102              
103 1         5 return;
104             }
105              
106             sub previous {
107 1     1 0 8261 my ( $self ) = @_;
108 1         3 return @{ $self->_objects_from_page( 'previous' ) };
  1         4  
109             }
110              
111             sub first {
112 1     1 0 801 my ( $self ) = @_;
113 1         2 return @{ $self->_objects_from_page( 'first' ) };
  1         5  
114             }
115              
116             sub last {
117 1     1 0 699 my ( $self ) = @_;
118 1         3 return @{ $self->_objects_from_page( 'last' ) };
  1         5  
119             }
120              
121             sub _objects_from_page {
122              
123 5     5   12 my ( $self,$page ) = @_;
124              
125             # see if we have more data to get
126 5 50       123 if ( my $url = $self->links->{$page} ) {
127              
128 5         66 my ( $data,$links,$info ) = $self->client->api_get( $url );
129              
130 5         19 my $class = $self->class;
131 12         87 my @objects = map { $class->new( client => $self->client,%{ $_ } ) }
  12         213  
132 5         10 @{ $data };
  5         12  
133              
134 5         118 $self->links( $links );
135 5         111 $self->info( $info );
136 5         96 return [ @objects ];
137             }
138              
139 0           return [];
140             }
141              
142             =head1 AUTHOR
143              
144             Lee Johnson - C
145              
146             This library is free software; you can redistribute it and/or modify it under
147             the same terms as Perl itself. If you would like to contribute documentation,
148             features, bug fixes, or anything else then please raise an issue / pull request:
149              
150             https://github.com/Humanstate/business-gocardless
151              
152             =cut
153              
154             1;