| 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 | 55 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 55 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 55 | ▶ | use Moo; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | 55 | extends 'Business::GoCardless::Resource'; | |||||||||||||||||||
| 18 | 55 | ▶ | use JSON (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | 55 | ▶ | use Business::GoCardless::Bill; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 21 | 55 | ▶ | use Business::GoCardless::PreAuthorization; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 22 | 55 | ▶ | use Business::GoCardless::Payout; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 23 | 55 | ▶ | use Business::GoCardless::User; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 24 | 55 | ▶ | use Business::GoCardless::Paginator; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | =head1 ATTRIBUTES | ||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | client | ||||||||||||||||||||
| 29 | objects | ||||||||||||||||||||
| 30 | class | ||||||||||||||||||||
| 31 | info | ||||||||||||||||||||
| 32 | links | ||||||||||||||||||||
| 33 | page_loader | ||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | page_loader is an optional coderef that takes a URL and returns an arrayref of | ||||||||||||||||||||
| 36 | objects, a hashref of links, and a hashref of info for that page. It is used | ||||||||||||||||||||
| 37 | for the v2 API, which has cursor based pagination rather than Link headers. | ||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | =cut | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | 55 | has [ qw/ | |||||||||||||||||||
| 42 | client | ||||||||||||||||||||
| 43 | objects | ||||||||||||||||||||
| 44 | class | ||||||||||||||||||||
| 45 | page_loader | ||||||||||||||||||||
| 46 | / ] => ( | ||||||||||||||||||||
| 47 | is => 'rw' | ||||||||||||||||||||
| 48 | ); | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | has info => ( | ||||||||||||||||||||
| 51 | is => 'rw', | ||||||||||||||||||||
| 52 | required => 1, | ||||||||||||||||||||
| 53 | 33 | ▶ | coerce => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||
| 54 | 33 | my ( $info ) = @_; | |||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | 33 | ▶ | return {} if ! $info; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | 33 | ▶ | if ( $info =~ /^[{\[]/ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 59 | # defensive decoding | ||||||||||||||||||||
| 60 | 6 | eval { $info = JSON->new->decode( $info ) }; | |||||||||||||||||||
| 61 | 6 | ▶ | $@ && do { return "Failed to parse JSON response ($info): $@"; }; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 62 | } | ||||||||||||||||||||
| 63 | 28 | return $info; | |||||||||||||||||||
| 64 | } | ||||||||||||||||||||
| 65 | 55 | ); | |||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | has links => ( | ||||||||||||||||||||
| 68 | is => 'rw', | ||||||||||||||||||||
| 69 | required => 1, | ||||||||||||||||||||
| 70 | 33 | ▶ | coerce => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||
| 71 | 33 | my ( $links ) = @_; | |||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | 33 | my $links_hash = {}; | |||||||||||||||||||
| 74 | 33 | ▶ | return $links_hash if ! $links; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | # already parsed (v2 API cursor links) | ||||||||||||||||||||
| 77 | 33 | ▶ | return $links if ref( $links ) eq 'HASH'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | 6 | foreach my $link ( split( /, /,$links ) ) { | |||||||||||||||||||
| 80 | 24 | my ( $rel,$url ) = reverse split( />; /,$link ); | |||||||||||||||||||
| 81 | 24 | $url =~ s/^<//; | |||||||||||||||||||
| 82 | 24 | $rel =~ s/^.*?"([A-z]+)"/$1/; | |||||||||||||||||||
| 83 | 24 | $links_hash->{$rel} = $url; | |||||||||||||||||||
| 84 | } | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | 6 | return $links_hash; | |||||||||||||||||||
| 87 | }, | ||||||||||||||||||||
| 88 | 55 | ); | |||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | =head1 PAGER METHODS | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | next | ||||||||||||||||||||
| 93 | previous | ||||||||||||||||||||
| 94 | first | ||||||||||||||||||||
| 95 | last | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | Return the objects from the next/previous/first/last page: | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | my @objects = $Paginator->next; | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | =cut | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | 6 | ▶ | sub next { | ||||||||||||||||||
|
Subroutine
next: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||
| 104 | 6 | my ( $self ) = @_; | |||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | 6 | ▶ | if ( my @objects = @{ $self->objects // [] } ) { | ||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$self->objects // []
MC/DC0%0/1
$self->objects // []
$self->objects
Truth table50%1/2
A
A= $self->objects
| |||||||||||||||||||||
| 107 | # get the next chunk and return the current chunk | ||||||||||||||||||||
| 108 | 4 | $self->objects( $self->_objects_from_page( 'next' ) ); | |||||||||||||||||||
| 109 | 4 | return @objects; | |||||||||||||||||||
| 110 | } | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | 2 | return; | |||||||||||||||||||
| 113 | } | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | 1 | ▶ | sub previous { | ||||||||||||||||||
|
Subroutine
previous: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 116 | 1 | my ( $self ) = @_; | |||||||||||||||||||
| 117 | 1 | return @{ $self->_objects_from_page( 'previous' ) }; | |||||||||||||||||||
| 118 | } | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | 1 | ▶ | sub first { | ||||||||||||||||||
|
Subroutine
first: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 121 | 1 | my ( $self ) = @_; | |||||||||||||||||||
| 122 | 1 | return @{ $self->_objects_from_page( 'first' ) }; | |||||||||||||||||||
| 123 | } | ||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | 1 | ▶ | sub last { | ||||||||||||||||||
|
Subroutine
last: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 126 | 1 | my ( $self ) = @_; | |||||||||||||||||||
| 127 | 1 | return @{ $self->_objects_from_page( 'last' ) }; | |||||||||||||||||||
| 128 | } | ||||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | 7 | ▶ | sub _objects_from_page { | ||||||||||||||||||
|
Subroutine
_objects_from_page: called CC=3 SCAR=11.0
| |||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | 7 | my ( $self,$page ) = @_; | |||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | # see if we have more data to get | ||||||||||||||||||||
| 135 | 7 | ▶ | if ( my $url = $self->links->{$page} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | 6 | ▶ | if ( my $page_loader = $self->page_loader ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 138 | 1 | my ( $objects,$links,$info ) = $page_loader->( $url ); | |||||||||||||||||||
| 139 | 1 | $self->links( $links ); | |||||||||||||||||||
| 140 | 1 | $self->info( $info ); | |||||||||||||||||||
| 141 | 1 | return $objects; | |||||||||||||||||||
| 142 | } | ||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | 5 | my ( $data,$links,$info ) = $self->client->api_get( $url ); | |||||||||||||||||||
| 145 | |||||||||||||||||||||
| 146 | 5 | my $class = $self->class; | |||||||||||||||||||
| 147 | 12 | my @objects = map { $class->new( client => $self->client,%{ $_ } ) } | |||||||||||||||||||
| 148 | 5 | @{ $data }; | |||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | 5 | $self->links( $links ); | |||||||||||||||||||
| 151 | 5 | $self->info( $info ); | |||||||||||||||||||
| 152 | 5 | return [ @objects ]; | |||||||||||||||||||
| 153 | } | ||||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | 1 | return []; | |||||||||||||||||||
| 156 | } | ||||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | =head1 AUTHOR | ||||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | Lee Johnson - C<lee@payprop.com> | ||||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | This library is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||
| 163 | the same terms as Perl itself. If you would like to contribute documentation, | ||||||||||||||||||||
| 164 | features, bug fixes, or anything else then please raise an issue / pull request: | ||||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | https://github.com/payprop/business-gocardless | ||||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | =cut | ||||||||||||||||||||
| 169 | |||||||||||||||||||||
| 170 | 55 | 1; | |||||||||||||||||||