| 1 | # | ||||||||||||||||||||||||||||
| 2 | # This file is part of DNS-NIOS | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # This software is Copyright (c) 2021 by Christian Segundo. | ||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||
| 6 | # This is free software, licensed under: | ||||||||||||||||||||||||||||
| 7 | # | ||||||||||||||||||||||||||||
| 8 | # The Artistic License 2.0 (GPL Compatible) | ||||||||||||||||||||||||||||
| 9 | # | ||||||||||||||||||||||||||||
| 10 | ## no critic | ||||||||||||||||||||||||||||
| 11 | package DNS::NIOS::Traits::AutoPager; | ||||||||||||||||||||||||||||
| 12 | 2 | $DNS::NIOS::Traits::AutoPager::VERSION = '0.003'; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | # ABSTRACT: Handle pagination automatically | ||||||||||||||||||||||||||||
| 15 | # VERSION | ||||||||||||||||||||||||||||
| 16 | # AUTHORITY | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | ## use critic | ||||||||||||||||||||||||||||
| 19 | 2 | ▶ | use strictures 2; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 2 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 2 | ▶ | use Role::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 2 | requires qw( get ); | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | around 'get' => sub { | ||||||||||||||||||||||||||||
| 26 | 2 | ▶ | my ( $orig, $self, %args ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 2 | my %params = ( | |||||||||||||||||||||||||||
| 29 | _return_as_object => 1, | ||||||||||||||||||||||||||||
| 30 | _max_results => 100, | ||||||||||||||||||||||||||||
| 31 | _paging => 1 | ||||||||||||||||||||||||||||
| 32 | ); | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 2 | my @responses; | |||||||||||||||||||||||||||
| 35 | 2 | ▶ | my $max_results = $args{params}->{_max_results} // 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$args{"params"}{"_max_results"} // 0
MC/DC100%1/1
$args{"params"}{"_max_results"} // 0
$args{"params"}{"_max_results"}
Truth table100%2/2
A
A= $args{"params"}{"_max_results"}
| |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | $args{params} | ||||||||||||||||||||||||||||
| 38 | 2 | ? %{ $args{params} } = | |||||||||||||||||||||||||||
| 39 | 2 | ( %{ $args{params} }, %params ) | |||||||||||||||||||||||||||
| 40 | 2 | ▶ | : $args{params} = \%params; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 2 | my $response = $orig->( $self, %args ); | |||||||||||||||||||||||||||
| 43 | 2 | ▶ | return [$response] if !$response->is_success; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 2 | push( @responses, $response ); | |||||||||||||||||||||||||||
| 46 | 2 | ▶ | while ( $response->content->{next_page_id} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 1199 | ▶ | last if $max_results and _is_max( $max_results, \@responses ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$max_results and _is_max($max_results, \@responses)
MC/DC100%2/2
$max_results and _is_max($max_results, \@responses)
$max_results
_is_max($max_results, \@responses)
Truth table100%3/3
A and B
A= $max_resultsB= _is_max($max_results, \@responses)
| |||||||||||||||||||||||||||||
| 48 | 1198 | %{ $args{params} } = | |||||||||||||||||||||||||||
| 49 | 1198 | ( %{ $args{params} }, _page_id => $response->content->{next_page_id} ); | |||||||||||||||||||||||||||
| 50 | 1198 | $response = $orig->( $self, %args ); | |||||||||||||||||||||||||||
| 51 | 1198 | push( @responses, $response ); | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 2 | return \@responses; | |||||||||||||||||||||||||||
| 55 | 2 | }; | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | sub _is_max { | ||||||||||||||||||||||||||||
| 58 | 200 | ▶ | my ( $max, $responses ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_is_max: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 200 | my $i = 0; | |||||||||||||||||||||||||||
| 61 | 200 | foreach ( @{$responses} ) { | |||||||||||||||||||||||||||
| 62 | 20100 | foreach ( $_->{results} ) { | |||||||||||||||||||||||||||
| 63 | 20100 | ▶ | last if $i >= $max; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 20100 | $i++; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | 200 | return $i >= $max; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 2 | 1; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | __END__ | ||||||||||||||||||||||||||||