lib/Finance/Robinhood/Utilities/Iterator.pm

filter: Statementstmt 100% 94 / 94 Branchbran 92.8% 13 / 14 Conditioncond 75.0% 9 / 12 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 100% 17 / 17 Podpod 100% 7 / 7 totaltotal 92.8% 142 / 153 CC 18 SCAR 13.6
CC
18
Cov
97%
_check_next_page
9.0
take
4.0
all
3.0
CRAP 3.9
« lib/Finance/Robinhood/User/BasicInfo.pm ↑ Summary lib/Finance/Robinhood/Equity/Quote.pm »
1 package Finance::Robinhood::Utilities::Iterator;
2  
3 =encoding utf-8
4  
5 =for stopwords th
6  
7 =head1 NAME
8  
9 Finance::Robinhood::Utilities::Iterator - Sugary Access to Paginated Data
10  
11 =head1 SYNOPSIS
12  
13 use Finance::Robinhood;
14 my $rh = t::Utility::rh_instance(1);
15 my $instruments = $rh->instruments();
16  
17 for my $instrument ($instruments->all) {
18 CORE::say $instrument->symbol;
19 }
20  
21 =cut
22  
23 1 our $VERSION = '0.92_003';
24 1 ▶use Mojo::Base-base, -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use Mojo::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 =head1 METHODS
28  
29 =cut
30  
31 # Destructive iterator because memory isn't free
32 # inspired by Array::Iterator and rust's Vec and IntoIter
33 1 has _rh => undef => weak => 1;
34 1 has [
35 '_current', '_next_page', '_class', '_first_page',
36  
37 # midlands returns a count with news and feed which is nice
38 'count'
39 ];
40 31 ▶has _results => sub { [] };
Subroutine __ANON__: called CC=1 SCAR=0.0
41  
42 =head2 C<reset( )>
43  
44 Reset the iterator. All elements will be removed and you'll basically start
45 from scratch.
46  
47 =cut
48  
49 1 ▶sub reset($s) {
Subroutine reset: called CC=2 SCAR=7.6
Pod documented
50 1 ▶ $s->_next_page($s->_first_page // $s->_next_page);
Condition33%1/3
$s->_first_page // $s->_next_page
l!l&&r!l&&!r
100
MC/DC0%0/2
$s->_first_page // $s->_next_page
$s->_first_page $s->_next_page
Truth table33%1/3
A // B
A= $s->_first_pageB= $s->_next_page
ABresult
1X1
011
000
51 1 $s->_current(());
52 1 $s->_results([]);
53 }
54  
55 1 ▶sub _test_reset {
Subroutine _test_reset: called CC=1 SCAR=0.0
56 1 my $rh = t::Utility::rh_instance(0);
57 1 my $instruments = $rh->equity_instruments;
58 1 isa_ok($instruments, __PACKAGE__);
59 1 my $next = $instruments->next;
60 1 $instruments->take(300);
61 1 $instruments->reset;
62 1 is($instruments->next, $next);
63 }
64  
65 =head2 C<current( )>
66  
67 Get the current element without removing it from the stack. This is
68 non-destructive but will move the cursor if there is no current element.
69  
70 =cut
71  
72 19 ▶sub current($s) {
Subroutine current: called CC=2 SCAR=6.9
Pod documented
73 19 ▶ $s->_current // $s->next;
Condition100%2/2
$s->_current // $s->next
l!l
316
MC/DC0%0/2
$s->_current // $s->next
$s->_current $s->next
Truth table66%2/3
A // B
A= $s->_currentB= $s->next
ABresult
1X1
011
000
74 19 $s->_current;
75 }
76  
77 1 ▶sub _test_current {
Subroutine _test_current: called CC=1 SCAR=0.0
78 1 my $rh = t::Utility::rh_instance(0);
79 1 my $instruments = $rh->equity_instruments;
80 1 isa_ok($instruments, __PACKAGE__);
81 1 my $next = $instruments->next;
82 1 isa_ok($instruments->current, 'Finance::Robinhood::Equity::Instrument');
83 1 is($instruments->current, $next);
84 }
85  
86 =head2 C<next( )>
87  
88 Gets the next element and removes it from the stack. If there are no elements
89 and all pages have been exhausted, this will return an undefined value.
90  
91 =cut
92  
93 32188 ▶sub next($s) {
Subroutine next: called CC=1 SCAR=0.0
Pod documented
94 32188 $s->_check_next_page;
95 32188 my ($retval, @values) = @{$s->_results};
96 32188 $s->_results(\@values);
97 32188 $s->_current($retval);
98 32188 $retval;
99 }
100  
101 =head2 C<peek( ... )>
102  
103 my $ten_more = $list->peek(10);
104  
105 Returns the I<n>th element without removing it from the stack. The index is
106 optional and, by default, the first element is returned.
107  
108 =cut
109  
110 1 ▶sub peek ($s, $pos = 1) {
Subroutine peek: called CC=2 SCAR=6.9
Pod documented
111 1 $s->_check_next_page($pos);
112 1 $s->_results->[$pos - 1];
113 }
114  
115 1 ▶sub _test_peek {
Subroutine _test_peek: called CC=1 SCAR=0.0
116 1 my $rh = t::Utility::rh_instance(0);
117 1 my $instruments = $rh->equity_instruments;
118 1 isa_ok($instruments, __PACKAGE__);
119 1 my $peek = $instruments->peek;
120 1 is($instruments->next, $peek);
121 }
122  
123 =head2 C<has_next( ... )>
124  
125 Returns a boolean indicating whether or not we have another I<X> elements. The
126 length is optional and checks the results for a a single element by default.
127  
128 =cut
129  
130 32206 ▶sub has_next ($s, $pos = 1) {
Subroutine has_next: called CC=2 SCAR=6.9
Pod documented
131 32206 $s->_check_next_page($pos);
132 32206 !!defined $s->_results->[$pos - 1];
133 }
134  
135 =head2 C<take( ... )>
136  
137 Removes a number of elements from the stack and returns them. By default, this
138 returns a single element.
139  
140 =cut
141  
142 # Grab a certain number of elements
143 37 ▶sub take ($s, $count = 1) {
Subroutine take: called CC=4 SCAR=13.9
Pod documented
144 37 $s->_check_next_page($count);
145 37 my @retval;
146 37 ▶ for (1 .. $count) { push @retval, $s->next; last if !$s->has_next }
Branch: 1 branch
BranchTrueFalseTotal
unless $s->has_next 3 32159 32162
147 37 $s->_current($retval[-1]);
148 37 @retval;
149 }
150  
151 1 ▶sub _test_take {
Subroutine _test_take: called CC=1 SCAR=0.0
152 1 my $rh = t::Utility::rh_instance(0);
153 1 my $instruments = $rh->equity_instruments;
154 1 isa_ok($instruments, __PACKAGE__);
155 {
156 1 my @take = $instruments->take(3);
157 1 is(3, scalar @take, '...take(3) returns 3 items');
158 }
159 {
160 1 my @take = $instruments->take(300);
161 1 is(300, scalar @take, '...take(300) returns 300 items');
162 }
163 }
164  
165 =head2 C<all( )>
166  
167 Grabs every page and returns every element we see.
168  
169 =cut
170  
171 4 ▶sub all($s) {
Subroutine all: called CC=3 SCAR=11.0
Pod documented
172 4 my @retval;
173 4 ▶ push @retval, $s->take($s->count // 1000) until !$s->has_next;
Branch: 1 branch
BranchTrueFalseTotal
not $s->has_next or do { push @retval, $s->take($s->count // 1000) } 4 34 38
Condition50%1/2
$s->count // 1000
l!l
034
MC/DC0%0/1
$s->count // 1000
$s->count
Truth table50%1/2
A
A= $s->count
Aresult
11
00
174 4 $s->_current($retval[-1]);
175 4 @retval;
176 }
177  
178 1 ▶sub _test_all_and_has_next {
Subroutine _test_all_and_has_next: called CC=1 SCAR=0.0
179 1 my $rh = t::Utility::rh_instance(0); # Do not log in!
180 1 my $instruments = $rh->equity_instruments;
181 1 isa_ok($instruments, __PACKAGE__);
182 1 diag('Grabbing all instruments... please hold...');
183 1 my @take = $instruments->all;
184 1 cmp_ok(11000, '<=', scalar(@take), sprintf '...all() returns %d items',
185 scalar @take);
186 1 isnt($instruments->has_next, !!1,
187 '...has_next() works at the end of the list');
188 }
189  
190 # Check if we need to slurp the next page of elements to fill a position
191 64432 ▶sub _check_next_page ($s, $count = 1) {
Subroutine _check_next_page: called CC=9 SCAR=22.0
192 64432 my @push = @{$s->_results};
193 64432 my $pre = scalar @push;
194 64432 ▶ $s->_first_page // $s->_first_page($s->_next_page);
Condition100%2/2
$s->_first_page // $s->_first_page($s->_next_page)
l!l
6440131
MC/DC0%0/2
$s->_first_page // $s->_first_page($s->_next_page)
$s->_first_page $s->_first_page($s->_next_page)
Truth table66%2/3
A // B
A= $s->_first_pageB= $s->_first_page($s->_next_page)
ABresult
1X1
011
000
195 64432 ▶ while (($count > scalar @push) && defined $s->_next_page) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count > scalar @push and defined $s->_next_page) 353 64432 64785
Condition100%3/3
$count > scalar @push and defined $s->_next_page
!ll&&!rl&&r
6441220353
MC/DC100%2/2
$count > scalar @push and defined $s->_next_page
$count > scalar @push defined $s->_next_page
Truth table100%3/3
A and B
A= $count > scalar @pushB= defined $s->_next_page
ABresult
0X0
100
111
196 353 my $res = $s->_rh->_get($s->_next_page);
197  
198 #use Data::Dump;
199 #ddx $res;
200 #ddx $res->json;
201 #die;
202 353 ▶ if ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 346 7 353
203 346 my $json = $res->json;
204 346 $s->_next_page($json->{next});
205 push @push, map {
206 defined $_
207 ? defined $s->_class
208 35030 ▶ ? do {
Branch: 2 branches
BranchTrueFalseTotal
defined $_ ? : 35029 1 35030
defined $s->_class ? : 35029 0 35029
209 35029 eval 'require ' . $s->_class;
210 35029 $s->_class->new(_rh => $s->_rh, %$_);
211 }
212 : $_
213 : ()
214 346 } @{$json->{results}};
215 }
216 else { # Trouble! Let's not try another page
217 7 $s->_next_page(undef);
218 }
219 }
220 64432 ▶ $s->_results(\@push) if scalar @push > $pre;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @push > $pre 87 64345 64432
221 }
222  
223 1 ▶sub _test_check_next_page {
Subroutine _test_check_next_page: called CC=1 SCAR=0.0
224 1 my $rh = t::Utility::rh_instance(0); # Do not log in!
225 1 is($rh->equity_instruments_by_id('c7d4323d-9512-4b15-977a-7cb2d1381d00'),
226 ()); # Fake id
227 }
228  
229 =head1 LEGAL
230  
231 This is a simple wrapper around the API used in the official apps. The author
232 provides no investment, legal, or tax advice and is not responsible for any
233 damages incurred while using this software. This software is not affiliated
234 with Robinhood Financial LLC in any way.
235  
236 For Robinhood's terms and disclosures, please see their website at
237 https://robinhood.com/legal/
238  
239 =head1 LICENSE
240  
241 Copyright (C) Sanko Robinson.
242  
243 This library is free software; you can redistribute it and/or modify it under
244 the terms found in the Artistic License 2. Other copyrights, terms, and
245 conditions may apply to data transmitted through this module. Please refer to
246 the L<LEGAL> section.
247  
248 =head1 AUTHOR
249  
250 Sanko Robinson E<lt>sanko@cpan.orgE<gt>
251  
252 =cut
253  
254 1 1;
« lib/Finance/Robinhood/User/BasicInfo.pm ↑ Summary lib/Finance/Robinhood/Equity/Quote.pm »