blib/lib/SignalWire/REST/Pagination.pm

filter: Statementstmt 27.4% 17 / 62 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 28.5% 4 / 14 Podpod 0.0% 0 / 2 totaltotal 19.4% 21 / 108 CC 13 SCAR 29.3
CC
13
Cov
20%
_fetch_next
56.0
__next__
12.0
_items
6.0
CRAP 18.7
« blib/lib/SignalWire/Skills/SkillManager.pm ↑ Summary blib/lib/SignalWire/Utils/UrlValidator.pm »
1 package SignalWire::REST::Pagination;
2 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # Perl port of signalwire.rest._pagination.PaginatedIterator.
6 #
7 # Iterates items across paginated API responses. Walks the
8 # C<links.next> cursor until no further page is advertised.
9 #
10 # Usage:
11 # my $it = SignalWire::REST::Pagination::PaginatedIterator->new(
12 # http => $http,
13 # path => '/api/relay/rest/whatever',
14 # params => { page_size => 10 },
15 # data_key => 'data',
16 # );
17 # while (defined(my $item = $it->next)) { ... }
18 # # or:
19 # my @items = $it->all;
20 #
21 # The class mirrors the Python iterator's three primary entry points
22 # (__init__, __iter__, __next__) plus the convenience C<all> form.
23  
24 package SignalWire::REST::Pagination::PaginatedIterator;
25 6 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 6 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 6 has 'http' => ( is => 'ro', required => 1 );
29 6 has 'path' => ( is => 'ro', required => 1 );
30 0 ▶has 'params' => ( is => 'rw', default => sub { {} } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
31 0 ▶has 'data_key' => ( is => 'ro', default => sub { 'data' } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
32  
33 # Internal mutable state mirrors Python's _items/_index/_done. We store
34 # it directly on the blessed hashref under leading-underscore keys
35 # (Perl convention for "private") and expose getter/setter methods.
36 # Storing them outside the Moo `has` system keeps them off the surface
37 # audit's __init__ projection — they are not constructor arguments.
38  
39 0 ▶sub BUILD {
Subroutine BUILD: not called CC=1 SCAR=6.9
Pod undocumented
40 0 my ($self) = @_;
41 0 $self->{_items} = [];
42 0 $self->{_index} = 0;
43 0 $self->{_done} = 0;
44 }
45  
46 0 ▶sub _items {
Subroutine _items: not called CC=2 SCAR=17.9
47 0 my $self = shift;
48 0 ▶ $self->{_items} = $_[0] if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
49 0 return $self->{_items};
50 }
51  
52 0 ▶sub _index {
Subroutine _index: not called CC=2 SCAR=17.9
53 0 my $self = shift;
54 0 ▶ $self->{_index} = $_[0] if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
55 0 return $self->{_index};
56 }
57  
58 0 ▶sub _done {
Subroutine _done: not called CC=2 SCAR=17.9
59 0 my $self = shift;
60 0 ▶ $self->{_done} = $_[0] if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
61 0 return $self->{_done};
62 }
63  
64 # Iterable interface (Python-mirroring, returns self so `iter()` parity).
65 0 ▶sub __iter__ { return $_[0]; }
Subroutine __iter__: not called CC=1 SCAR=6.9
66  
67 # Pull the next item; returns undef when exhausted (Perl-idiomatic
68 # alternative to Python's StopIteration).
69 0 ▶sub __next__ {
Subroutine __next__: not called CC=3 SCAR=24.8
70 0 my ($self) = @_;
71 0 ▶ while ($self->_index >= scalar @{ $self->_items }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_index >= scalar @{$self->_items;}) 0 0 0
72 0 ▶ if ($self->_done) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_done) 0 0 0
73 0 return; # empty list scalar context => undef; signals exhaustion
74 }
75 0 $self->_fetch_next;
76 }
77 0 my $item = $self->_items->[$self->_index];
78 0 $self->_index($self->_index + 1);
79 0 return $item;
80 }
81  
82 # Drain the whole iterator into a list. Mirrors the typical Python
83 # usage `list(iter)` — Perl callers can write `my @items = $it->all`
84 # instead of looping over __next__ themselves.
85 0 ▶sub all {
Subroutine all: not called CC=2 SCAR=17.9
Pod undocumented
86 0 my ($self) = @_;
87 0 my @out;
88 0 ▶ while (defined(my $item = $self->__next__)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $item = $self->__next__)) 0 0 0
89 0 push @out, $item;
90 }
91 0 return @out;
92 }
93  
94 0 ▶sub _fetch_next {
Subroutine _fetch_next: not called CC=7 SCAR=40.3
95 0 my ($self) = @_;
96 0 ▶ my $params = (keys %{ $self->params || {} }) ? $self->params : undef;
Branch: 1 branch
BranchTrueFalseTotal
keys %{$self->params or {};} ? : 0 0 0
Condition0%0/2
$self->params || {}
l!l
00
MC/DC0%0/1
$self->params || {}
$self->params
Truth table0%0/2
A
A= $self->params
Aresult
11
00
97 0 my $resp = $self->http->get($self->path, params => $params);
98 0 ▶ my $data = $resp->{ $self->data_key } || [];
Condition0%0/2
$$resp{$self->data_key} || []
l!l
00
MC/DC0%0/1
$$resp{$self->data_key} || []
$$resp{$self->data_key}
Truth table0%0/2
A
A= $$resp{$self->data_key}
Aresult
11
00
99 0 push @{ $self->_items }, @$data;
100  
101 0 ▶ my $links = $resp->{links} || {};
Condition0%0/2
$resp->{'links'} || {}
l!l
00
MC/DC0%0/1
$resp->{'links'} || {}
$resp->{'links'}
Truth table0%0/2
A
A= $resp->{'links'}
Aresult
11
00
102 0 my $next_url = $links->{next};
103 0 ▶ if ($next_url && @$data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next_url and @$data) { } 0 0 0
Condition0%0/3
$next_url and @$data
!ll&&!rl&&r
000
MC/DC0%0/2
$next_url and @$data
$next_url @$data
Truth table0%0/3
A and B
A= $next_urlB= @$data
ABresult
0X0
100
111
104 # Parse cursor/page params from next URL query.
105 0 my $u = URI->new($next_url);
106 0 my %query = $u->query_form;
107 0 $self->params(\%query);
108 } else {
109 0 $self->_done(1);
110 }
111 0 return;
112 }
113  
114 6 1;
115  
116 __END__
« blib/lib/SignalWire/Skills/SkillManager.pm ↑ Summary blib/lib/SignalWire/Utils/UrlValidator.pm »