blib/lib/Finance/Robinhood/Utils/Paginated.pm

filter: Statementstmt 80.5% 29 / 36 Branchbran 56.2% 9 / 16 Conditioncond 46.1% 6 / 13 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 80.0% 4 / 5 Podpod 100% 3 / 3 totaltotal 65.0% 52 / 80 CC 14 SCAR 19.8
CC
14
Cov
68%
next_page
8.4
next
8.1
all
6.0
CRAP 7.2
« blib/lib/Finance/Robinhood.pm ↑ Summary blib/lib/Finance/Robinhood/Options/Order/Leg.pm »
1 package Finance::Robinhood::Utils::Paginated;
2  
3 =encoding utf-8
4  
5 =head1 NAME
6  
7 Finance::Robinhood::Utils::Paginated - Represent paginated data in an iterative object
8  
9 =head1 SYNOPSIS
10  
11 use Finance::Robinhood;
12  
13 my $rh = Finance::Robinhood->new();
14  
15 my $instruments = $rh->instruments();
16  
17 while(my $instrument = $instruments->next()) {
18 # Do something fun here
19 }
20  
21 =cut
22  
23 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 3 our $VERSION = '0.9.0_001';
25 3 ▶use Finance::Robinhood::Utils::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 =head1 METHODS
28  
29 Some data returned by Robinhood's API is so exhaustive that it is broken up into pages.
30  
31 This class wraps that data in a friendly way.
32  
33 =cut
34  
35 3 has '_results' => ( is => 'rw', predicate => 1 );
36 3 has '_next' => ( is => 'rw', init_arg => 'next', predicate => 1, clearer => 1 );
37 3 has '_class' => ( is => 'ro', init_arg => 'class', predicate => 1 );
38  
39 =head2 C<next( )>
40  
41 while (my $record = $paginator->next()) { ... }
42  
43 Returns the next record in the current page. If all records have been exhausted then
44 the next page will automatically be loaded. This way if you want to ignore pagination
45 you can just call C<next( )> over and over again to walk through all the records.
46  
47 When we're out of pages and items, an undefined value is returned.
48  
49 =cut
50  
51 sub next {
52 10 ▶ my ($s) = @_;
Subroutine next: called CC=7 SCAR=21.0
Pod documented
53 10 my $records = $s->_results();
54 10 ▶ return shift(@$records) if $records && scalar @$records;
Branch: 1 branch
BranchTrueFalseTotal
if $records and scalar @$records 6 4 10
Condition100%2/2
$records and scalar @$records
!ll
46
MC/DC100%1/1
$records and scalar @$records
$records
Truth table100%2/2
A
A= $records
Aresult
00
11
55 4 my ( $status, $data ) = $s->next_page;
56 4 ▶ return $data if defined $status && $status != 200;
Branch: 1 branch
BranchTrueFalseTotal
if defined $status and $status != 200 0 4 4
Condition33%1/3
defined $status and $status != 200
!ll&&!rl&&r
040
MC/DC0%0/2
defined $status and $status != 200
defined $status $status != 200
Truth table33%1/3
A and B
A= defined $statusB= $status != 200
ABresult
0X0
100
111
57 4 $records = $s->_results();
58 4 ▶ return shift(@$records) if $records && scalar @$records;
Branch: 1 branch
BranchTrueFalseTotal
if $records and scalar @$records 4 0 4
Condition50%1/2
$records and scalar @$records
!ll
04
MC/DC0%0/1
$records and scalar @$records
$records
Truth table50%1/2
A
A= $records
Aresult
00
11
59 0 return $data;
60 }
61  
62 =head2 C<next_page( )>
63  
64 while (my $records = $paginator->next_page()) { ... }
65  
66 Returns an array ref of records for the next page.
67  
68 =cut
69  
70 sub next_page {
71 4 ▶ my ($s) = @_;
Subroutine next_page: called CC=7 SCAR=21.3
Pod documented
72 4 ▶ return if !$s->_has_next();
Branch: 1 branch
BranchTrueFalseTotal
unless $s->_has_next 0 4 4
73 4 my $page = $s->_next();
74 4 my ( $status, $data ) = Finance::Robinhood::Utils::Client->instance->get($page);
75 4 ▶ if ( !$data || !$data->{next} || $data->{next} eq $page ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $data && $data->{'next'} or $data->{'next'} eq $page) { } 4 0 4
Condition33%2/6
not $data && $data->{"next"} or $data->{"next"} eq $page
l!l&&r!l&&!r
400
$data && $data->{'next'}
!ll&&!rl&&r
040
MC/DC0%0/3
not $data && $data->{"next"} or $data->{"next"} eq $page
$data $data->{'next'} $data->{"next"} eq $page
Truth table25%1/4
not(A && B) or C
A= $dataB= $data->{'next'}C= $data->{"next"} eq $page
ABCresult
0XX1
10X1
1111
1100
76 4 $s->_clear_next;
77 }
78 0 else { $s->_next( $data->{next} ) }
79 4 ▶ $data->{results} = [ map { $_ = $_ ? $s->_class->new($_) : $_ } @{ $data->{results} } ]
Branch: 2 branches
BranchTrueFalseTotal
if $s->_has_class 4 0 4
$_ ? : 8 2 10
80 if $s->_has_class;
81 4 $s->_results( $data->{results} );
82 4 return ( $status, $data->{results} );
83 }
84  
85 =head2 all
86  
87 my $records = $paginator->all();
88  
89 This is rolls through every page building one giant array ref of all records.
90  
91 =cut
92  
93 sub all {
94 0 ▶ my ($s) = @_;
Subroutine all: not called CC=2 SCAR=17.9
Pod documented
95 0 my @records;
96 0 ▶ while ( my $items = $s->next_page() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $items = $s->next_page) 0 0 0
97 0 push @records, @$items;
98 }
99 0 return \@records;
100 }
101 3 1;
« blib/lib/Finance/Robinhood.pm ↑ Summary blib/lib/Finance/Robinhood/Options/Order/Leg.pm »