lib/Net/API/CPAN/List.pm

filter: Statementstmt 50.4% 188 / 373 Branchbran 27.1% 57 / 210 Conditioncond 21.1% 56 / 265 MC/DCmcdc 0.7% 1 / 132 Subroutinesub 77.0% 37 / 48 Podpod 100% 32 / 32 totaltotal 35.0% 371 / 1060 CC 209 SCAR 60.8
CC
209
Cov
35%
load_data
1134.9
next
691.9
prev
478.9
CRAP 436.2
« blib/lib/Net/API/CPAN/File.pm ↑ Summary blib/lib/Net/API/CPAN/Release.pm »
1 ##----------------------------------------------------------------------------
2 ## Meta CPAN API - ~/lib/Net/API/CPAN/List.pm
3 ## Version v0.1.1
4 ## Copyright(c) 2023 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2023/08/02
7 ## Modified 2023/09/26
8 ## All rights reserved
9 ##
10 ##
11 ## This program is free software; you can redistribute it and/or modify it
12 ## under the same terms as Perl itself.
13 ##----------------------------------------------------------------------------
14 package Net::API::CPAN::List;
15 BEGIN
16 {
17 3 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶ use parent qw( Net::API::CPAN::Generic );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶ use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶ use Clone ();
Subroutine BEGIN: called CC=1 SCAR=0.0
22 3 ▶ use HTTP::Promise;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 3 ▶ use Want;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 use constant {
25 # Elastic Search default value.
26 # See <https://www.elastic.co/guide/en/elasticsearch/reference/2.4/search-request-from-size.html>
27 3 DEFAULT_PAGE_SIZE => 10,
28 3 ▶ };
Subroutine BEGIN: called CC=1 SCAR=0.0
29 3 ▶ our $VERSION = 'v0.1.1';
Subroutine BEGIN: called CC=1 SCAR=0.0
30 };
31  
32 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 sub init
36 {
37 1 ▶ my $self = CORE::shift( @_ );
Subroutine init: called CC=21 SCAR=35.6
Pod documented
38 1 ▶ $self->{api} = undef unless( CORE::exists( $self->{api} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"api"} 1 0 1
39 # The name of the JSON property containing the array reference of data
40 # This is used for data in format other than search results such as hits->hits
41 1 ▶ $self->{container} = undef unless( CORE::exists( $self->{container} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"container"} 1 0 1
42 # The full URI to the endpoint used for loading data
43 # $self->{endpoint} = undef unless( CORE::exists( $self->{endpoint} ) );
44 1 ▶ $self->{filter} = undef unless( CORE::exists( $self->{filter} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"filter"} 1 0 1
45 # Is this list a list that can load more data or is this set of data the only one available?
46 1 ▶ $self->{pageable} = 1 unless( CORE::exists( $self->{pageable} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"pageable"} 1 0 1
47 1 ▶ $self->{page} = 1 unless( CORE::exists( $self->{page} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"page"} 1 0 1
48 # Either 'from' or 'page' is used to navigate through pages of data
49 1 ▶ $self->{page_type} = 'from' unless( CORE::exists( $self->{page_type} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"page_type"} 1 0 1
50 1 ▶ $self->{postprocess} = sub{$_[0]} unless( CORE::exists( $self->{postprocess} ) );
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"postprocess"} 1 0 1
51 # $self->{preprocess} = sub{$_[0]} unless( CORE::exists( $self->{preprocess} ) );
52 1 ▶ $self->{preprocess} = undef unless( CORE::exists( $self->{preprocess} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"preprocess"} 1 0 1
53 1 ▶ $self->{request} = undef unless( CORE::exists( $self->{request} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"request"} 1 0 1
54 # We use this if we are not using filters
55 1 ▶ $self->{size} = undef unless( CORE::exists( $self->{size} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"size"} 1 0 1
56 # Usually this is 'size', but sometimes it is 'page_size'
57 1 ▶ $self->{size_prop} = 'size' unless( CORE::exists( $self->{size_prop} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"size_prop"} 1 0 1
58 1 ▶ $self->{timed_out} = 0 unless( CORE::exists( $self->{timed_out} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"timed_out"} 1 0 1
59 1 ▶ $self->{took} = undef unless( CORE::exists( $self->{took} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"took"} 1 0 1
60 1 ▶ $self->{total} = undef unless( CORE::exists( $self->{total} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"total"} 1 0 1
61 1 ▶ $self->{type} = undef unless( CORE::exists( $self->{type} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"type"} 1 0 1
62 1 $self->{_init_strict_use_sub} = 1;
63 1 $self->{_init_params_order} = [qw( debug type api container preprocess postprocess total data )];
64 1 ▶ $self->SUPER::init( @_ ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::init(@_) or return $self->pass_error 0 1 1
65 1 ▶ unless( CORE::exists( $self->{api} ) && $self->_is_a( $self->{api} => 'Net::API::CPAN' ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"api"} and $self->_is_a($self->{"api"}, "Net::API::CPAN")) 1 0 1
Condition33%1/3
exists $self->{"api"} and $self->_is_a($self->{"api"}, "Net::API::CPAN")
!ll&&!rl&&r
010
MC/DC0%0/2
exists $self->{"api"} and $self->_is_a($self->{"api"}, "Net::API::CPAN")
exists $self->{"api"} $self->_is_a($self->{"api"}, "Net::API::CPAN")
Truth table33%1/3
A and B
A= exists $self->{"api"}B= $self->_is_a($self->{"api"}, "Net::API::CPAN")
ABresult
0X0
100
111
66 {
67 1 ▶ $self->_load_class( 'Net::API::CPAN' ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("Net::API::CPAN") or return $self->pass_error 0 1 1
68 1 ▶ $self->{api} = Net::API::CPAN->new( debug => $self->debug ) ||
Condition50%1/2
'Net::API::CPAN'->new('debug', $self->debug) || (return $self->pass_error('Net::API::CPAN'->error))
l!l
10
MC/DC0%0/1
'Net::API::CPAN'->new('debug', $self->debug) || (return $self->pass_error('Net::API::CPAN'->error))
'Net::API::CPAN'->new('debug', $self->debug)
Truth table50%1/2
A
A= 'Net::API::CPAN'->new('debug', $self->debug)
Aresult
11
00
69 return( $self->pass_error( Net::API::CPAN->error ) );
70 }
71 # Simple initialisation
72 1 $self->{_raw_items} = $self->new_array;
73 1 return( $self );
74 }
75  
76 3 ▶sub api { return( CORE::shift->_set_get_object( 'api', 'Net::API::CPAN', @_ ) ); }
Subroutine api: called CC=1 SCAR=0.0
Pod documented
77  
78 3 ▶sub container { return( CORE::shift->_set_get_scalar( 'container', @_ ) ); }
Subroutine container: called CC=1 SCAR=0.0
Pod documented
79  
80 sub data
81 {
82 0 ▶ my $self = CORE::shift( @_ );
Subroutine data: not called CC=3 SCAR=24.8
Pod documented
83 0 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
84 {
85 0 ▶ $self->load_data( @_ ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->load_data(@_) or return $self->pass_error 0 0 0
86 }
87 0 return( $self->items );
88 }
89  
90 # sub endpoint { return( CORE::shift->_set_get_uri( 'endpoint', @_ ) ); }
91  
92 7 ▶sub filter { return( CORE::shift->_set_get_object_without_init( 'filter', 'Net::API::CPAN::Filter', @_ ) ); }
Subroutine filter: called CC=1 SCAR=0.0
Pod documented
93  
94 sub get
95 {
96 1 ▶ my $self = CORE::shift( @_ );
Subroutine get: called CC=6 SCAR=26.2
Pod documented
97 1 ▶ my $pos = @_ ? int( CORE::shift( @_ ) ) : ( $self->{_pos} || 0 );
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 1 0 1
Condition0%0/2
$self->{"_pos"} or 0
l!l
00
MC/DC0%0/1
$self->{"_pos"} or 0
$self->{"_pos"}
Truth table0%0/2
A
A= $self->{"_pos"}
Aresult
11
00
98 1 my $data = $self->items;
99 1 my $what = lc( Want::wantref );
100 1 ▶ if( !defined( $data->[ $pos ] ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $data->[$pos]) 0 1 1
101 {
102 0 ▶ if( $what eq 'object' || $what eq 'hash' )
Branch: 1 branch
BranchTrueFalseTotal
if ($what eq "object" or $what eq "hash") 0 0 0
Condition0%0/3
$what eq "object" or $what eq "hash"
l!l&&r!l&&!r
000
MC/DC0%0/2
$what eq "object" or $what eq "hash"
$what eq "object" $what eq "hash"
Truth table0%0/3
A or B
A= $what eq "object"B= $what eq "hash"
ABresult
1X1
011
000
103 {
104 0 return( $self->new_null( type => $what ) );
105 }
106 0 return;
107 }
108 1 return( $data->[ $pos ] );
109 }
110  
111 sub has_more
112 {
113 1 ▶ my $self = CORE::shift( @_ );
Subroutine has_more: called CC=12 SCAR=32.6
Pod documented
114 1 ▶ my $total = $self->total // 0;
Condition50%1/2
$self->total // 0
l!l
10
MC/DC0%0/1
$self->total // 0
$self->total
Truth table50%1/2
A
A= $self->total
Aresult
11
00
115 1 my( $filter, $size );
116 1 ▶ if( $filter = $self->filter )
Branch: 1 branch
BranchTrueFalseTotal
if ($filter = $self->filter) 1 0 1
117 {
118 1 $size = $filter->size;
119 }
120 # This is the default Elastic Search value
121 1 ▶ $size //= $self->size // DEFAULT_PAGE_SIZE;
Condition40%2/5
$size //= $self->size // 10
l!l&&r!l&&!r
010
$self->size // 10
l!l
01
MC/DC0%0/2
$size //= $self->size // 10
$size $self->size
Truth table0%0/3
A //= B
A= $sizeB= $self->size
ABresult
1X1
011
000
122 1 ▶ my $offset = $self->offset // 0;
Condition50%1/2
$self->offset // 0
l!l
10
MC/DC0%0/1
$self->offset // 0
$self->offset
Truth table50%1/2
A
A= $self->offset
Aresult
11
00
123 1 $self->message( 4, "\$total = $total, \$offset = $offset, \$size = $size" );
124 1 ▶ $self->message( 4, "Returning true. There is more." ) if( $total && $total > $size && ( $total - ( $offset + 1 ) > 0 ) );
Branch: 1 branch
BranchTrueFalseTotal
if $total and $total > $size and $total - ($offset + 1) > 0 1 0 1
Condition33%2/6
$total and $total > $size and $total - ($offset + 1) > 0
!ll&&!rl&&r
001
$total and $total > $size
!ll&&!rl&&r
001
MC/DC0%0/3
$total and $total > $size and $total - ($offset + 1) > 0
$total $total > $size $total - ($offset + 1) > 0
Truth table25%1/4
A and B and C
A= $totalB= $total > $sizeC= $total - ($offset + 1) > 0
ABCresult
0XX0
10X0
1100
1111
125 # Do we have data and is it bigger than the page size and we are not on the last page
126 1 ▶ return(1) if( $total && $total > $size && ( $total - ( $offset + 1 ) > 0 ) );
Branch: 1 branch
BranchTrueFalseTotal
if $total and $total > $size and $total - ($offset + 1) > 0 1 0 1
Condition33%2/6
$total and $total > $size and $total - ($offset + 1) > 0
!ll&&!rl&&r
001
$total and $total > $size
!ll&&!rl&&r
001
MC/DC0%0/3
$total and $total > $size and $total - ($offset + 1) > 0
$total $total > $size $total - ($offset + 1) > 0
Truth table25%1/4
A and B and C
A= $totalB= $total > $sizeC= $total - ($offset + 1) > 0
ABCresult
0XX0
10X0
1100
1111
127 0 $self->message( 4, "Returning false. There is no more data to fetch." );
128 0 return(0);
129 }
130  
131 sub items
132 {
133 8 ▶ my $self = CORE::shift( @_ );
Subroutine items: called CC=19 SCAR=33.4
Pod documented
134 8 ▶ @_ = () if( @_ == 1 && !defined( $_[0] ) );
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 and not defined $_[0] 0 8 8
Condition66%2/3
@_ == 1 and not defined $_[0]
!ll&&!rl&&r
710
MC/DC0%0/2
@_ == 1 and not defined $_[0]
@_ == 1 not defined $_[0]
Truth table66%2/3
A and not(B)
A= @_ == 1B= not defined $_[0]
ABresult
0X0
100
111
135 8 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 7 8
136 {
137 1 my $ref = CORE::shift( @_ );
138 1 my $opts = $self->_get_args_as_hash( @_ );
139 1 ▶ return( $self->error( "I was expecting an array reference, but instead got '", overload::StrVal( $ref ), "'." ) ) if( !$self->_is_array( $ref ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_array($ref) 0 1 1
140 1 my $cache = {};
141 1 my $api = $self->api;
142 1 my $arr = $self->new_array;
143 1 my $def_type = $self->type->scalar;
144 1 $self->message( 4, "Processing ", scalar( @$ref ), " elements with type '$def_type'" );
145 1 ▶ for( my $i = 0; $i < scalar( @$ref ); $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$ref) 10 1 11
146 {
147 10 ▶ return( $self->error( "I was expecting an array of hash reference, but instead of an hash I found '", overload::StrVal( $ref->[$i] ), "'" ) ) if( ref( $ref->[$i] ) ne 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $ref->[$i] ne "HASH" 0 10 10
148 10 my $hash = $ref->[$i];
149 10 ▶ my $type = $opts->{type} || $hash->{_type} || $def_type ||
Condition37%3/8
$opts->{'type'} || $hash->{'_type'} || $def_type || (return $self->error("No object type was specified nor any could be found in the hash reference at offset $i"))
l!l
100
$opts->{"type"} or $hash->{"_type"} or $def_type
l!l&&r!l&&!r
1000
$opts->{"type"} or $hash->{"_type"}
l!l&&r!l&&!r
0100
MC/DC0%0/3
$opts->{'type'} || $hash->{'_type'} || $def_type || (return $self->error("No object type was specified nor any could be found in the hash reference at offset $i"))
$opts->{"type"} $hash->{"_type"} $def_type
Truth table25%1/4
A or B or C
A= $opts->{"type"}B= $hash->{"_type"}C= $def_type
ABCresult
1XX1
01X1
0011
0000
150 return( $self->error( "No object type was specified nor any could be found in the hash reference at offset $i" ) );
151 10 my $class;
152 10 ▶ unless( $class = $cache->{ $type } )
Branch: 1 branch
BranchTrueFalseTotal
unless ($class = $cache->{$type}) 1 9 10
153 {
154 1 ▶ $class = $self->_object_type_to_class( $type ) || return( $self->error( "Could not find corresponding class for ojbect type \"$type\"." ) );
Condition50%1/2
$self->_object_type_to_class($type) || (return $self->error(qq[Could not find corresponding class for ojbect type "$type".]))
l!l
10
MC/DC0%0/1
$self->_object_type_to_class($type) || (return $self->error(qq[Could not find corresponding class for ojbect type "$type".]))
$self->_object_type_to_class($type)
Truth table50%1/2
A
A= $self->_object_type_to_class($type)
Aresult
11
00
155 1 $self->message( 4, "Loading class $class (", overload::StrVal( $class ), ") for object type $type" );
156 1 ▶ $self->_load_class( $class ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class($class) or return $self->pass_error 0 1 1
157 1 $cache->{ $type } = $class;
158 # XXX
159 # if( $class eq 'Net::API::CPAN::List::Web::Element' )
160 # {
161 # my @symbols = $self->_list_symbols( 'Net::API::CPAN::List::Web::Element' );
162 # $self->message( 4, "Symbols found for class Net::API::CPAN::List::Web::Element are -> ", sub{ $self->Module::Generic::dump( \@symbols ) } );
163 # }
164 }
165 10 ▶ $hash = $hash->{_source} if( exists( $hash->{_source} ) && ref( $hash->{_source} ) eq 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
if exists $hash->{"_source"} and ref $hash->{"_source"} eq "HASH" 10 0 10
Condition33%1/3
exists $hash->{"_source"} and ref $hash->{"_source"} eq "HASH"
!ll&&!rl&&r
0010
MC/DC0%0/2
exists $hash->{"_source"} and ref $hash->{"_source"} eq "HASH"
exists $hash->{"_source"} ref $hash->{"_source"} eq "HASH"
Truth table33%1/3
A and B
A= exists $hash->{"_source"}B= ref $hash->{"_source"} eq "HASH"
ABresult
0X0
100
111
166 # $self->message( 4, "Instantiating a $class object at offset $i for object type $type with data: ", sub{ $self->Module::Generic::dump( $hash ) } );
167 10 $hash->{debug} = $self->debug;
168 10 ▶ my $o = $class->new( %$hash, api => $api ) || return( $self->pass_error( $class->error ) );
Condition50%1/2
$class->new(%$hash, 'api', $api) || (return $self->pass_error($class->error))
l!l
100
MC/DC0%0/1
$class->new(%$hash, 'api', $api) || (return $self->pass_error($class->error))
$class->new(%$hash, 'api', $api)
Truth table50%1/2
A
A= $class->new(%$hash, 'api', $api)
Aresult
11
00
169 10 $self->message( 4, "Adding new $class object $o (", overload::StrVal( $o ), ") to the stack." );
170 10 $arr->push( $o );
171 }
172 1 $self->{items} = $arr;
173 }
174 8 ▶ if( !$self->{items} || !$self->_is_a( $self->{items} => 'Module::Generic::Array' ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"items"} and $self->_is_a($self->{"items"}, "Module::Generic::Array")) 0 8 8
Condition33%1/3
$self->{"items"} and $self->_is_a($self->{"items"}, "Module::Generic::Array")
!ll&&!rl&&r
008
MC/DC0%0/2
$self->{"items"} and $self->_is_a($self->{"items"}, "Module::Generic::Array")
$self->{"items"} $self->_is_a($self->{"items"}, "Module::Generic::Array")
Truth table33%1/3
A and B
A= $self->{"items"}B= $self->_is_a($self->{"items"}, "Module::Generic::Array")
ABresult
0X0
100
111
175 {
176 0 ▶ $self->{items} = $self->new_array( defined( $self->{items} ) ? $self->{items} : [] );
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'items'} ? : 0 0 0
177 }
178 8 return( $self->{items} );
179 }
180  
181 1 ▶sub length { return( CORE::shift->items->length ); }
Subroutine length: called CC=1 SCAR=0.0
Pod documented
182  
183 sub load
184 {
185 0 ▶ my $self = CORE::shift( @_ );
Subroutine load: not called CC=9 SCAR=45.0
Pod documented
186 0 my $opts = $self->_get_args_as_hash( @_ );
187 my $req = $opts->{request} ||
188 0 ▶ return( $self->error( "No HTTP request was provided to load data." ) );
Condition0%0/2
$opts->{'request'} || (return $self->error('No HTTP request was provided to load data.'))
l!l
00
MC/DC0%0/1
$opts->{'request'} || (return $self->error('No HTTP request was provided to load data.'))
$opts->{'request'}
Truth table0%0/2
A
A= $opts->{'request'}
Aresult
11
00
189 0 ▶ my $api = $self->api ||
Condition0%0/2
$self->api || (return $self->error('No Net::API::CPAN obejct is currently set. This should not happen.'))
l!l
00
MC/DC0%0/1
$self->api || (return $self->error('No Net::API::CPAN obejct is currently set. This should not happen.'))
$self->api
Truth table0%0/2
A
A= $self->api
Aresult
11
00
190 return( $self->error( "No Net::API::CPAN obejct is currently set. This should not happen." ) );
191 0 ▶ my $type = $self->type ||
Condition0%0/2
$self->type || (return $self->error('No object type set for this list.'))
l!l
00
MC/DC0%0/1
$self->type || (return $self->error('No object type set for this list.'))
$self->type
Truth table0%0/2
A
A= $self->type
Aresult
11
00
192 return( $self->error( "No object type set for this list." ) );
193 0 my $filter = $self->filter;
194 0 ▶ if( $filter )
Branch: 1 branch
BranchTrueFalseTotal
if ($filter) { } 0 0 0
195 {
196 0 ▶ return( $self->error( "No search query set." ) ) if( !$filter->query );
Branch: 1 branch
BranchTrueFalseTotal
unless $filter->query 0 0 0
197 0 ▶ my $json = $filter->as_json ||
Condition0%0/2
$filter->as_json || (return $self->pass_error($filter->error))
l!l
00
MC/DC0%0/1
$filter->as_json || (return $self->pass_error($filter->error))
$filter->as_json
Truth table0%0/2
A
A= $filter->as_json
Aresult
11
00
198 return( $self->pass_error( $filter->error ) );
199 0 $req->method( 'POST' );
200 }
201 else
202 {
203 0 $req->method( 'GET' );
204 }
205 0 $req->headers->header( Accept => 'application/json' );
206 my $data = $api->fetch( $type => {
207 request => $req,
208 # We simply want the raw data back
209 0 ▶ class => sub{ $_[0] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
210 0 ▶ }) || return( $self->pass_error( $api->error ) );
Condition0%0/2
$api->fetch($type, {'request', $req, 'class', sub { || (return $self->pass_error($api->error))
l!l
00
MC/DC0%0/1
$api->fetch($type, {'request', $req, 'class', sub { $_[0]; } }) || (return $self->pass_error($api->error))
$api->fetch($type, {'request', $req, 'class', sub { $_[0]; } })
Truth table0%0/2
A
A= $api->fetch($type, {'request', $req, 'class', sub { $_[0]; } })
Aresult
11
00
211
212 0 ▶ $self->load_data( $data ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->load_data($data) or return $self->pass_error 0 0 0
213 0 return( $self );
214 }
215  
216 sub load_data
217 {
218 1 ▶ my $self = CORE::shift( @_ );
Subroutine load_data: called CC=65 SCAR=70.3
Pod documented
219 1 ▶ my $data = CORE::shift( @_ ) ||
Condition50%1/2
CORE::shift @_ || (return $self->error('No data was provided to load.'))
l!l
10
MC/DC0%0/1
CORE::shift @_ || (return $self->error('No data was provided to load.'))
CORE::shift @_
Truth table50%1/2
A
A= CORE::shift @_
Aresult
11
00
220 return( $self->error( "No data was provided to load." ) );
221 1 ▶ return( $self->error( "Data provided is not an hash reference." ) ) if( ref( $data ) ne 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $data ne "HASH" 0 1 1
222 1 $self->message( 4, "Loading data received with ", scalar( keys( %$data ) ), " properties: ", join( ', ', sort( keys( %$data ) ) ) );
223 1 my $filter = $self->filter;
224 1 my $container = $self->container;
225 1 ▶ $self->message( 4, "Container to use is '", ( $container // 'undef', ), "'" );
Condition50%1/2
$container // "undef"
l!l
01
MC/DC0%0/1
$container // "undef"
$container
Truth table50%1/2
A
A= $container
Aresult
11
00
226  
227 1 ▶ if( my $code = $self->preprocess )
Branch: 1 branch
BranchTrueFalseTotal
if (my $code = $self->preprocess) 0 1 1
228 {
229 0 $self->message( 4, "Executing preprocess." );
230 # try-catch
231 0 local $@;
232 $data = eval
233 0 {
234 0 $code->( $data );
235 };
236 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
237 {
238 0 return( $self->error( $@ ) );
239 }
240 # $self->message( 5, "After preprocess, data now is: ", sub{ $self->Module::Generic::dump( $data ) } );
241 }
242  
243 # $ref is the variable containing the array reference of data
244 1 my( $ref, $total );
245 1 ▶ if( ( $container &&
Branch: 1 branch
BranchTrueFalseTotal
if ($container and $container eq 'hits' and exists $data->{$container} and ref $data->{$container} eq 'HASH' and exists $data->{$container}{'hits'} and ref $data->{$container}{'hits'} eq 'ARRAY' or not defined $container and exists $data->{'hits'} and ref $data->{'hits'} eq 'HASH' and exists $data->{'hits'}{'hits'} and ref $data->{'hits'}{'hits'} eq 'ARRAY') { } 1 0 1
Condition33%10/30
$container and $container eq "hits" and exists $data->{$container} and ref $data->{$container} eq "HASH" and exists $data->{$container}{"hits"} and ref $data->{$container}{"hits"} eq "ARRAY" or not defined $container and exists $data->{"hits"} and ref $data->{"hits"} eq "HASH" and exists $data->{"hits"}{"hits"} and ref $data->{"hits"}{"hits"} eq "ARRAY"
l!l&&r!l&&!r
010
$container and $container eq "hits" and exists $data->{$container} and ref $data->{$container} eq "HASH" and exists $data->{$container}{"hits"} and ref $data->{$container}{"hits"} eq "ARRAY"
!ll&&!rl&&r
100
$container and $container eq "hits" and exists $data->{$container} and ref $data->{$container} eq "HASH" and exists $data->{$container}{"hits"}
!ll&&!rl&&r
100
$container and $container eq "hits" and exists $data->{$container} and ref $data->{$container} eq "HASH"
!ll&&!rl&&r
100
$container and $container eq "hits" and exists $data->{$container}
!ll&&!rl&&r
100
$container and $container eq "hits"
!ll&&!rl&&r
100
not defined $container and exists $data->{"hits"} and ref $data->{"hits"} eq "HASH" and exists $data->{"hits"}{"hits"} and ref $data->{"hits"}{"hits"} eq "ARRAY"
!ll&&!rl&&r
001
not defined $container and exists $data->{"hits"} and ref $data->{"hits"} eq "HASH" and exists $data->{"hits"}{"hits"}
!ll&&!rl&&r
001
not defined $container and exists $data->{"hits"} and ref $data->{"hits"} eq "HASH"
!ll&&!rl&&r
001
not defined $container and exists $data->{"hits"}
!ll&&!rl&&r
001
MC/DC0%0/11
$container and $container eq "hits" and exists $data->{$container} and ref $data->{$container} eq "HASH" and exists $data->{$container}{"hits"} and ref $data->{$container}{"hits"} eq "ARRAY" or not defined $container and exists $data->{"hits"} and ref $data->{"hits"} eq "HASH" and exists $data->{"hits"}{"hits"} and ref $data->{"hits"}{"hits"} eq "ARRAY"
$container $container eq "hits" exists $data->{$container} ref $data->{$container} eq "HASH" exists $data->{$container}{"hits"} ref $data->{$container}{"hits"} eq "ARRAY" not defined $container exists $data->{"hits"} ref $data->{"hits"} eq "HASH" exists $data->{"hits"}{"hits"} ref $data->{"hits"}{"hits"} eq "ARRAY"
Truth table2%1/37
A and B and C and D and E and F or not(G) and H and I and J and K
A= $containerB= $container eq "hits"C= exists $data->{$container}D= ref $data->{$container} eq "HASH"E= exists $data->{$container}{"hits"}F= ref $data->{$container}{"hits"} eq "ARRAY"G= not defined $containerH= exists $data->{"hits"}I= ref $data->{"hits"} eq "HASH"J= exists $data->{"hits"}{"hits"}K= ref $data->{"hits"}{"hits"} eq "ARRAY"
ABCDEFGHIJKresult
111111XXXXX1
0XXXXX111111
10XXXX111111
110XXX111111
1110XX111111
11110X111111
111110111111
0XXXXX0XXXX0
0XXXXX10XXX0
0XXXXX110XX0
0XXXXX1110X0
0XXXXX111100
10XXXX0XXXX0
10XXXX10XXX0
10XXXX110XX0
10XXXX1110X0
10XXXX111100
110XXX0XXXX0
110XXX10XXX0
110XXX110XX0
110XXX1110X0
110XXX111100
1110XX0XXXX0
1110XX10XXX0
1110XX110XX0
1110XX1110X0
1110XX111100
11110X0XXXX0
11110X10XXX0
11110X110XX0
11110X1110X0
11110X111100
1111100XXXX0
11111010XXX0
111110110XX0
1111101110X0
111110111100
246 $container eq 'hits' &&
247 exists( $data->{ $container } ) &&
248 ref( $data->{ $container } ) eq 'HASH' &&
249 exists( $data->{ $container }->{hits} ) &&
250 ref( $data->{ $container }->{hits} ) eq 'ARRAY'
251 ) ||
252 ( !defined( $container ) &&
253 exists( $data->{hits} ) &&
254 ref( $data->{hits} ) eq 'HASH' &&
255 exists( $data->{hits}->{hits} ) &&
256 ref( $data->{hits}->{hits} ) eq 'ARRAY'
257 ) )
258 {
259 1 $self->message( 4, "Guessed container to be 'hits->hits'" );
260 1 ▶ unless( defined( $container ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $container) 1 0 1
261 {
262 1 $self->container( $container = 'hits' );
263 }
264 1 ▶ if( !exists( $data->{hits} ) ||
Branch: 2 branches
BranchTrueFalseTotal
if (not exists $data->{'hits'} && defined $data->{'hits'} or ref $data->{'hits'} ne 'HASH') { } 0 1 1
elsif (not exists $data->{'hits'}{'hits'} && defined $data->{'hits'}{'hits'} or ref $data->{'hits'}{'hits'} ne 'ARRAY') { } 0 1 1
Condition33%2/6
not exists $data->{"hits"} && defined $data->{"hits"} or ref $data->{"hits"} ne "HASH"
l!l&&r!l&&!r
001
exists $data->{'hits'} && defined $data->{'hits'}
!ll&&!rl&&r
001
MC/DC0%0/3
not exists $data->{"hits"} && defined $data->{"hits"} or ref $data->{"hits"} ne "HASH"
exists $data->{'hits'} defined $data->{'hits'} ref $data->{"hits"} ne "HASH"
Truth table25%1/4
not(A && B) or C
A= exists $data->{'hits'}B= defined $data->{'hits'}C= ref $data->{"hits"} ne "HASH"
ABCresult
0XX1
10X1
1111
1100
265 !defined( $data->{hits} ) ||
266 ref( $data->{hits} ) ne 'HASH' )
267 {
268 # return( $self->error( "Malformed data received. I was expecting a top property 'hits' to be an hash reference." ) );
269 # Actually not necessarily an error, but simply no more data
270 0 $self->items->reset;
271 }
272 ▶ elsif( !exists( $data->{hits}->{hits} ) ||
Condition33%2/6
not exists $data->{"hits"}{"hits"} && defined $data->{"hits"}{"hits"} or ref $data->{"hits"}{"hits"} ne "ARRAY"
l!l&&r!l&&!r
001
exists $data->{'hits'}{'hits'} && defined $data->{'hits'}{'hits'}
!ll&&!rl&&r
001
MC/DC0%0/3
not exists $data->{"hits"}{"hits"} && defined $data->{"hits"}{"hits"} or ref $data->{"hits"}{"hits"} ne "ARRAY"
exists $data->{'hits'}{'hits'} defined $data->{'hits'}{'hits'} ref $data->{"hits"}{"hits"} ne "ARRAY"
Truth table25%1/4
not(A && B) or C
A= exists $data->{'hits'}{'hits'}B= defined $data->{'hits'}{'hits'}C= ref $data->{"hits"}{"hits"} ne "ARRAY"
ABCresult
0XX1
10X1
1111
1100
273 !defined( $data->{hits}->{hits} ) ||
274 ref( $data->{hits}->{hits} ) ne 'ARRAY' )
275 {
276 # return( $self->error( "Malformed data received. I was expecting the property 'hits' within the top property 'hits' to be an array reference." ) );
277 # Actually not necessarily an error, but simply no more data
278 0 $self->items->reset;
279 }
280 else
281 {
282 1 $ref = $data->{hits}->{hits};
283 # The overall number of hits
284 1 ▶ if( exists( $data->{hits}->{total} ) &&
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{'hits'}{'total'} and defined $data->{'hits'}{'total'} and CORE::length $data->{'hits'}{'total'}) { } 1 0 1
Condition33%2/6
exists $data->{"hits"}{"total"} and defined $data->{"hits"}{"total"} and CORE::length $data->{"hits"}{"total"}
!ll&&!rl&&r
001
exists $data->{"hits"}{"total"} and defined $data->{"hits"}{"total"}
!ll&&!rl&&r
001
MC/DC0%0/3
exists $data->{"hits"}{"total"} and defined $data->{"hits"}{"total"} and CORE::length $data->{"hits"}{"total"}
exists $data->{"hits"}{"total"} defined $data->{"hits"}{"total"} CORE::length $data->{"hits"}{"total"}
Truth table25%1/4
A and B and C
A= exists $data->{"hits"}{"total"}B= defined $data->{"hits"}{"total"}C= CORE::length $data->{"hits"}{"total"}
ABCresult
0XX0
10X0
1100
1111
285 defined( $data->{hits}->{total} ) &&
286 CORE::length( $data->{hits}->{total} ) )
287 {
288 1 $self->message( 4, "Setting total value using hits->total (", $data->{hits}->{total}, ")" );
289 # $self->total( $data->{hits}->{total} );
290 1 $total = $data->{hits}->{total};
291 }
292 # If the information is not available somehow, use the size of the array
293 else
294 {
295 0 $self->message( 4, "Setting total value using items->length (", $self->items->length, ")" );
296 # $self->total( $self->items->length );
297 0 $total = $self->items->length;
298 }
299 }
300 }
301 else
302 {
303 0 $self->message( 4, "Container is not hits->hits, trying to find the first property with an array." );
304 0 ▶ unless( $container = $self->container )
Branch: 1 branch
BranchTrueFalseTotal
unless ($container = $self->container) 0 0 0
305 {
306 0 foreach my $prop ( keys( %$data ) )
307 {
308 0 ▶ if( defined( $data->{ $prop } ) &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data->{$prop} and ref $data->{$prop} eq "ARRAY") 0 0 0
Condition0%0/3
defined $data->{$prop} and ref $data->{$prop} eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $data->{$prop} and ref $data->{$prop} eq "ARRAY"
defined $data->{$prop} ref $data->{$prop} eq "ARRAY"
Truth table0%0/3
A and B
A= defined $data->{$prop}B= ref $data->{$prop} eq "ARRAY"
ABresult
0X0
100
111
309 ref( $data->{ $prop } ) eq 'ARRAY' )
310 {
311 0 $container = $prop;
312 0 $self->message( 4, "Found the container being the property '$prop'" );
313 # Save it for next time
314 0 $self->container( $container );
315 0 last;
316 }
317 }
318 }
319
320 0 ▶ if( !defined( $container ) )
Branch: 4 branches
BranchTrueFalseTotal
if (not defined $container) { } 0 0 0
elsif (not exists $data->{$container}) { } 0 0 0
elsif (not defined $data->{$container}) { } 0 0 0
elsif (ref $data->{$container} ne 'ARRAY') { } 0 0 0
321 {
322 0 return( $self->error( "No data container name was specified and none could be found in the data provided." ) );
323 }
324 elsif( !exists( $data->{ $container } ) )
325 {
326 0 return( $self->error( "Data container specified '$container' does not exist in the data provided." ) );
327 }
328 # There is simply no data. Admittedly it would be better if it was defined and an empty array
329 elsif( !defined( $data->{ $container } ) )
330 {
331 0 $ref = [];
332 }
333 elsif( ref( $data->{ $container } ) ne 'ARRAY' )
334 {
335 0 ▶ return( $self->error( "Data container specified '$container' does not point to an array reference, but to a '", ( ref( $data->{ $container } ) // 'string' ), "'." ) );
Condition0%0/2
ref $data->{$container} // "string"
l!l
00
MC/DC0%0/1
ref $data->{$container} // "string"
ref $data->{$container}
Truth table0%0/2
A
A= ref $data->{$container}
Aresult
11
00
336 }
337 else
338 {
339 0 $ref = $data->{ $container };
340 }
341
342 # Like for some release data, such as /all_by_author
343 0 ▶ $total = $data->{total} if( exists( $data->{total} ) && CORE::length( $data->{total} // '' ) );
Branch: 1 branch
BranchTrueFalseTotal
if exists $data->{"total"} and CORE::length($data->{"total"} // "") 0 0 0
Condition0%0/5
exists $data->{"total"} and CORE::length($data->{"total"} // "")
!ll&&!rl&&r
000
$data->{"total"} // ""
l!l
00
MC/DC0%0/2
exists $data->{"total"} and CORE::length($data->{"total"} // "")
exists $data->{"total"} $data->{"total"}
Truth table0%0/3
A and B
A= exists $data->{"total"}B= $data->{"total"}
ABresult
0X0
100
111
344 }
345  
346 1 ▶ if( defined( $ref ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ref) { } 1 0 1
347 {
348 1 $self->message( 4, "Container contains ", scalar( @$ref ), " ", $self->type, " elements." );
349 # We take steps to ensure the data we received is not the same as the data we already have
350 1 my $new_items = $self->new_array( $ref );
351 1 ▶ my $old_items = $self->{_raw_items} // $self->new_array;
Condition33%1/3
$self->{"_raw_items"} // $self->new_array
l!l&&r!l&&!r
100
MC/DC0%0/2
$self->{"_raw_items"} // $self->new_array
$self->{"_raw_items"} $self->new_array
Truth table33%1/3
A // B
A= $self->{"_raw_items"}B= $self->new_array
ABresult
1X1
011
000
352 1 ▶ $self->message( 5, "Old items are -> ", sub{ $self->Module::Generic::dump( $old_items ) } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
353 # Set the size of the number of elements per page, so we can rely on it, even if the array is modified afterwards
354 # unless( defined( $filter->size ) )
355 # {
356 # $filter->size( $new_items->length );
357 # }
358 # Set the value for 'type' for our object if it was not set already by the user.
359 # It should already have been set if there was an API call, but not necessarily if the data were simply and directly loaded here.
360 1 ▶ if( !$self->type &&
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->type and scalar @$new_items and ref $new_items->[0] eq "HASH" and exists $new_items->[0]{"_type"} and CORE::length($new_items->[0]{"_type"} // "")) 1 0 1
Condition38%5/13
not $self->type and scalar @$new_items and ref $new_items->[0] eq "HASH" and exists $new_items->[0]{"_type"} and CORE::length($new_items->[0]{"_type"} // "")
!ll&&!rl&&r
001
not $self->type and scalar @$new_items and ref $new_items->[0] eq "HASH" and exists $new_items->[0]{"_type"}
!ll&&!rl&&r
001
not $self->type and scalar @$new_items and ref $new_items->[0] eq "HASH"
!ll&&!rl&&r
001
not $self->type and scalar @$new_items
!ll
01
$new_items->[0]{"_type"} // ""
l!l
10
MC/DC0%0/4
not $self->type and scalar @$new_items and ref $new_items->[0] eq "HASH" and exists $new_items->[0]{"_type"} and CORE::length($new_items->[0]{"_type"} // "")
not $self->type ref $new_items->[0] eq "HASH" exists $new_items->[0]{"_type"} $new_items->[0]{"_type"}
Truth table20%1/5
not(A) and B and C and D
A= not $self->typeB= ref $new_items->[0] eq "HASH"C= exists $new_items->[0]{"_type"}D= $new_items->[0]{"_type"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
361 scalar( @$new_items ) &&
362 ref( $new_items->[0] ) eq 'HASH' &&
363 exists( $new_items->[0]->{_type} ) &&
364 CORE::length( $new_items->[0]->{_type} // '' ) )
365 {
366 1 $self->type( $new_items->[0]->{_type} );
367 }
368
369 1 ▶ if( !$new_items->is_empty &&
Branch: 1 branch
BranchTrueFalseTotal
if (not $new_items->is_empty || $old_items->is_empty and ref $new_items->first eq 'HASH' and ref $old_items->first eq 'HASH') { } 0 1 1
Condition33%3/9
not $new_items->is_empty || $old_items->is_empty and ref $new_items->first eq "HASH" and ref $old_items->first eq "HASH"
!ll&&!rl&&r
100
not $new_items->is_empty || $old_items->is_empty and ref $new_items->first eq "HASH"
!ll&&!rl&&r
100
$new_items->is_empty || $old_items->is_empty
l!l&&r!l&&!r
010
MC/DC0%0/4
not $new_items->is_empty || $old_items->is_empty and ref $new_items->first eq "HASH" and ref $old_items->first eq "HASH"
$new_items->is_empty $old_items->is_empty ref $new_items->first eq "HASH" ref $old_items->first eq "HASH"
Truth table20%1/5
not(A || B) and C and D
A= $new_items->is_emptyB= $old_items->is_emptyC= ref $new_items->first eq "HASH"D= ref $old_items->first eq "HASH"
ABCDresult
1XXX0
01XX0
000X0
00100
00111
370 !$old_items->is_empty &&
371 ref( $new_items->first ) eq 'HASH' &&
372 ref( $old_items->first ) eq 'HASH' )
373 {
374 0 my $json = $self->new_json->canonical;
375 0 my $new_first = $new_items->first;
376 0 my $old_first = $old_items->first;
377 0 my( $new_serial, $old_serial );
378 0 local $@;
379 # try-catch
380 eval
381 0 {
382 0 $new_serial = $json->encode( $new_first );
383 0 $old_serial = $json->encode( $old_first );
384 };
385 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
386 {
387 0 return( $self->error( "Error serialising hash reference into JSON data: $@" ) );
388 }
389  
390 # old and new data are the same. We cannot have that,
391 # so we set an empty data pool
392 0 ▶ if( defined( $new_serial ) &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $new_serial and defined $old_serial and $new_serial eq $old_serial) { } 0 0 0
Condition0%0/6
defined $new_serial and defined $old_serial and $new_serial eq $old_serial
!ll&&!rl&&r
000
defined $new_serial and defined $old_serial
!ll&&!rl&&r
000
MC/DC0%0/3
defined $new_serial and defined $old_serial and $new_serial eq $old_serial
defined $new_serial defined $old_serial $new_serial eq $old_serial
Truth table0%0/4
A and B and C
A= defined $new_serialB= defined $old_serialC= $new_serial eq $old_serial
ABCresult
0XX0
10X0
1100
1111
393 defined( $old_serial ) &&
394 $new_serial eq $old_serial )
395 {
396 0 $self->items->reset;
397 }
398 else
399 {
400 0 $self->items( $ref );
401 0 $self->{_raw_items} = $self->new_array( Clone::clone( $ref ) );
402 }
403 }
404 # Ok, old and new data are not the same
405 else
406 {
407 1 $self->items( $ref );
408 1 $self->{_raw_items} = $self->new_array( Clone::clone( $ref ) );
409 }
410
411 # If the information is not available somehow, use the size of the array
412 1 ▶ $self->total( $total // $self->items->length );
Condition33%1/3
$total // $self->items->length
l!l&&r!l&&!r
100
MC/DC0%0/2
$total // $self->items->length
$total $self->items->length
Truth table33%1/3
A // B
A= $totalB= $self->items->length
ABresult
1X1
011
000
413 # If it was already set the first time, we do not overwrite it.
414 # This is used so we can compute next page offset
415 1 ▶ unless( $self->page_size )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->page_size) 1 0 1
416 {
417 1 $self->page_size( $self->items->length );
418 }
419 }
420 else
421 {
422 0 return( $self->error( "No data found for container '$container'" ) );
423 }
424
425 1 ▶ if( exists( $data->{timed_out} ) &&
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{'timed_out'} and defined $data->{'timed_out'} and CORE::length $data->{'timed_out'}) { } 1 0 1
Condition33%2/6
exists $data->{"timed_out"} and defined $data->{"timed_out"} and CORE::length $data->{"timed_out"}
!ll&&!rl&&r
001
exists $data->{"timed_out"} and defined $data->{"timed_out"}
!ll&&!rl&&r
001
MC/DC0%0/3
exists $data->{"timed_out"} and defined $data->{"timed_out"} and CORE::length $data->{"timed_out"}
exists $data->{"timed_out"} defined $data->{"timed_out"} CORE::length $data->{"timed_out"}
Truth table25%1/4
A and B and C
A= exists $data->{"timed_out"}B= defined $data->{"timed_out"}C= CORE::length $data->{"timed_out"}
ABCresult
0XX0
10X0
1100
1111
426 defined( $data->{timed_out} ) &&
427 CORE::length( $data->{timed_out} ) )
428 {
429 1 $self->timed_out( $data->{timed_out} );
430 }
431 else
432 {
433 0 $self->timed_out(0);
434 }
435
436 1 ▶ if( exists( $data->{took} ) &&
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{'took'} and defined $data->{'took'} and CORE::length $data->{'took'}) { } 1 0 1
Condition33%2/6
exists $data->{"took"} and defined $data->{"took"} and CORE::length $data->{"took"}
!ll&&!rl&&r
001
exists $data->{"took"} and defined $data->{"took"}
!ll&&!rl&&r
001
MC/DC0%0/3
exists $data->{"took"} and defined $data->{"took"} and CORE::length $data->{"took"}
exists $data->{"took"} defined $data->{"took"} CORE::length $data->{"took"}
Truth table25%1/4
A and B and C
A= exists $data->{"took"}B= defined $data->{"took"}C= CORE::length $data->{"took"}
ABCresult
0XX0
10X0
1100
1111
437 defined( $data->{took} ) &&
438 CORE::length( $data->{took} ) )
439 {
440 1 $self->took( $data->{took} );
441 }
442 else
443 {
444 0 $self->took(undef);
445 }
446 # Reset the array position
447 1 delete( $self->{_pos} );
448
449 1 ▶ if( my $code = $self->postprocess )
Branch: 1 branch
BranchTrueFalseTotal
if (my $code = $self->postprocess) 1 0 1
450 {
451 1 $self->message( 4, "Executing postprocess." );
452 # try-catch
453 1 local $@;
454 eval
455 1 {
456 1 $code->( $data );
457 };
458 1 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
459 {
460 0 return( $self->error( $@ ) );
461 }
462 }
463 1 return( $self );
464 }
465  
466 sub next
467 {
468 2 ▶ my $self = CORE::shift( @_ );
Subroutine next: called CC=30 SCAR=65.4
Pod documented
469 2 ▶ $self->{_pos} = -1 if( !exists( $self->{_pos} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"_pos"} 1 1 2
470 2 my $data = $self->items;
471 2 my $what = lc( Want::wantref );
472 2 my $val;
473 # if( $self->{_pos} + 1 < $data->length )
474 # {
475 2 $val = $data->[ ++$self->{_pos} ];
476 # }
477
478 2 ▶ if( !defined( $val ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $val) 0 2 2
479 {
480 # Our offset exceeds the size of our data pool and we have more data, so let's fetch some more
481 0 ▶ if( $self->{_pos} > $data->size && $self->has_more )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_pos"} > $data->size and $self->has_more) 0 0 0
Condition0%0/3
$self->{"_pos"} > $data->size and $self->has_more
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"_pos"} > $data->size and $self->has_more
$self->{"_pos"} > $data->size $self->has_more
Truth table0%0/3
A and B
A= $self->{"_pos"} > $data->sizeB= $self->has_more
ABresult
0X0
100
111
482 {
483 0 ▶ my $req = $self->request ||
Condition0%0/2
$self->request || (return $self->error('No initial HTTP request was provided to load data.'))
l!l
00
MC/DC0%0/1
$self->request || (return $self->error('No initial HTTP request was provided to load data.'))
$self->request
Truth table0%0/2
A
A= $self->request
Aresult
11
00
484 return( $self->error( "No initial HTTP request was provided to load data." ) );
485 0 $req = $req->clone;
486 0 my $filter = $self->filter;
487 # Starting from 1
488 0 ▶ my $page = $self->page // 1;
Condition0%0/2
$self->page // 1
l!l
00
MC/DC0%0/1
$self->page // 1
$self->page
Truth table0%0/2
A
A= $self->page
Aresult
11
00
489 0 my $size;
490 0 ▶ if( $filter )
Branch: 1 branch
BranchTrueFalseTotal
if ($filter) { } 0 0 0
491 {
492 0 ▶ return( $self->error( "No search query set." ) ) if( !$filter->query );
Branch: 1 branch
BranchTrueFalseTotal
unless $filter->query 0 0 0
493 # 10 is Elastic Search default size
494 0 $size = $filter->size;
495 0 ▶ if( defined( $size ) && !$size )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $size and not $size) 0 0 0
Condition0%0/3
defined $size and not $size
!ll&&!rl&&r
000
MC/DC0%0/2
defined $size and not $size
defined $size not $size
Truth table0%0/3
A and not(B)
A= defined $sizeB= not $size
ABresult
0X0
100
111
496 {
497 0 ▶ $size = $self->size // DEFAULT_PAGE_SIZE;
Condition0%0/2
$self->size // 10
l!l
00
MC/DC0%0/1
$self->size // 10
$self->size
Truth table0%0/2
A
A= $self->size
Aresult
11
00
498 0 $filter->size( $size );
499 }
500 0 $filter->from( int( ( $page - 1 ) * $size ) );
501 }
502 else
503 {
504 0 $self->message( 5, "Original query URL is ", $req->uri );
505 0 my $query = $req->uri->query_form_hash;
506 0 ▶ if( !scalar( keys( %$query ) ) ||
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar keys %$query and exists $query->{"page"} || exists $query->{"from"} || exists $query->{"size"}) 0 0 0
Condition0%0/9
scalar keys %$query and exists $query->{"page"} || exists $query->{"from"} || exists $query->{"size"}
!ll&&!rl&&r
000
exists $query->{"page"} or exists $query->{"from"} or exists $query->{"size"}
l!l&&r!l&&!r
000
exists $query->{"page"} or exists $query->{"from"}
l!l&&r!l&&!r
000
MC/DC0%0/4
scalar keys %$query and exists $query->{"page"} || exists $query->{"from"} || exists $query->{"size"}
scalar keys %$query exists $query->{"page"} exists $query->{"from"} exists $query->{"size"}
Truth table0%0/5
A and B or C or D
A= scalar keys %$queryB= exists $query->{"page"}C= exists $query->{"from"}D= exists $query->{"size"}
ABCDresult
0XXX0
10000
11XX1
101X1
10011
507 ( !exists( $query->{page} ) && !exists( $query->{from} ) && !exists( $query->{size} ) ) )
508 {
509 # return( $self->error( "No search query set." ) );
510 }
511 0 $size = $self->size;
512 # If size option is set, otherwise we leave it out
513 0 ▶ if( defined( $size ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $size) 0 0 0
514 {
515 0 ▶ $size //= DEFAULT_PAGE_SIZE;
Condition0%0/2
$size //= 10
l!l
00
MC/DC0%0/1
$size //= 10
$size
Truth table0%0/2
A
A= $size
Aresult
11
00
516 0 ▶ my $size_prop = $self->size_prop // 'size';
Condition0%0/2
$self->size_prop // "size"
l!l
00
MC/DC0%0/1
$self->size_prop // "size"
$self->size_prop
Truth table0%0/2
A
A= $self->size_prop
Aresult
11
00
517 0 $query->{ $size_prop } = $size;
518 }
519 0 ▶ my $page_type = $self->page_type || 'from';
Condition0%0/2
$self->page_type || 'from'
l!l
00
MC/DC0%0/1
$self->page_type || 'from'
$self->page_type
Truth table0%0/2
A
A= $self->page_type
Aresult
11
00
520 0 ▶ $self->message( 4, "Page is '", ( $page // 'undef' ), "' size is '", ( $size // 'undef' ), "', page_type is '", ( $page_type // 'undef' ), "'" );
Condition0%0/6
$page // "undef"
l!l
00
$size // "undef"
l!l
00
$page_type // "undef"
l!l
00
MC/DC0%0/1
$page // "undef"
$page
MC/DC0%0/1
$size // "undef"
$size
MC/DC0%0/1
$page_type // "undef"
$page_type
Truth table0%0/2
A
A= $page
Aresult
11
00
Truth table0%0/2
A
A= $size
Aresult
11
00
Truth table0%0/2
A
A= $page_type
Aresult
11
00
521 0 ▶ if( $page_type eq 'from' )
Branch: 2 branches
BranchTrueFalseTotal
if ($page_type eq 'from') { } 0 0 0
elsif ($page_type eq 'page') { } 0 0 0
522 {
523 0 ▶ my $page_size = $size // $self->page_size;
Condition0%0/3
$size // $self->page_size
l!l&&r!l&&!r
000
MC/DC0%0/2
$size // $self->page_size
$size $self->page_size
Truth table0%0/3
A // B
A= $sizeB= $self->page_size
ABresult
1X1
011
000
524 # 0..9 or 10..19
525 0 my $offset = int( ( $page - 1 ) * $page_size );
526 0 $self->message( 4, "Offset is '$offset'" );
527 0 $query->{from} = $offset;
528 }
529 elsif( $page_type eq 'page' )
530 {
531 0 $query->{page} = ( $page + 1 );
532 }
533 else
534 {
535 0 return( $self->error( "Unknown page type '$page_type'" ) );
536 }
537 0 ▶ $self->message( 4, "Setting new query with hash -> ", sub{ $self->dump( $query ) } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
538 0 $req->uri->query_form( $query );
539 0 $self->message( 4, "Using URI for next load of data -> ", $req->uri );
540 }
541
542 # User will need to check if there is an error if the user gets an undefined value in return,
543 # to distinguish between no more value vs a returned error
544 0 ▶ $self->load( request => $req ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->load("request", $req) or return $self->pass_error 0 0 0
545 # We set our current page to +1 if indeed we have data.
546 0 ▶ $self->page( $page + 1 ) if( !$self->items->is_empty );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->items->is_empty 0 0 0
547 0 return( $self->next );
548 }
549
550 0 ▶ if( $what eq 'object' || $what eq 'hash' )
Branch: 1 branch
BranchTrueFalseTotal
if ($what eq "object" or $what eq "hash") 0 0 0
Condition0%0/3
$what eq "object" or $what eq "hash"
l!l&&r!l&&!r
000
MC/DC0%0/2
$what eq "object" or $what eq "hash"
$what eq "object" $what eq "hash"
Truth table0%0/3
A or B
A= $what eq "object"B= $what eq "hash"
ABresult
1X1
011
000
551 {
552 0 return( $self->new_null( type => $what ) );
553 }
554 0 return;
555 }
556 2 return( $val );
557 }
558  
559 sub offset
560 {
561 3 ▶ my $self = CORE::shift( @_ );
Subroutine offset: called CC=6 SCAR=18.8
Pod documented
562 3 ▶ my $page = $self->page // 1;
Condition50%1/2
$self->page // 1
l!l
30
MC/DC0%0/1
$self->page // 1
$self->page
Truth table50%1/2
A
A= $self->page
Aresult
11
00
563 3 my( $size, $filter );
564 3 ▶ if( $filter = $self->filter )
Branch: 1 branch
BranchTrueFalseTotal
if ($filter = $self->filter) 3 0 3
565 {
566 3 $size = $filter->size;
567 }
568 # $size //= $self->page_size // DEFAULT_PAGE_SIZE;
569 3 ▶ $size ||= $self->page_size || DEFAULT_PAGE_SIZE;
Condition40%2/5
$size ||= $self->page_size || 10
l!l&&r!l&&!r
030
$self->page_size || 10
l!l
30
MC/DC0%0/2
$size ||= $self->page_size || 10
$size $self->page_size
Truth table33%1/3
A ||= B
A= $sizeB= $self->page_size
ABresult
1X1
011
000
570 3 ▶ my $pos = $self->pos // 0;
Condition100%2/2
$self->pos // 0
l!l
21
MC/DC100%1/1
$self->pos // 0
$self->pos
Truth table100%2/2
A
A= $self->pos
Aresult
11
00
571 3 $self->message( 4, "Calculating offset using page '$page', size '$size' and pos '$pos' -> ", ( $pos + ( $size * ( $page - 1 ) ) ) );
572 3 return( $self->new_number( $pos + ( $size * ( $page - 1 ) ) ) );
573 }
574  
575 4 ▶sub page { return( CORE::shift->_set_get_number( 'page', @_ ) ); }
Subroutine page: called CC=1 SCAR=0.0
Pod documented
576  
577 0 ▶sub pageable { return( CORE::shift->_set_get_number( 'pageable', @_ ) ); }
Subroutine pageable: not called CC=1 SCAR=6.9
Pod documented
578  
579 5 ▶sub page_size { return( CORE::shift->_set_get_number( 'page_size', @_ ) ); }
Subroutine page_size: called CC=1 SCAR=0.0
Pod documented
580  
581 1 ▶sub page_type { return( shift->_set_get_scalar_as_object( 'page_type', @_ ) ); }
Subroutine page_type: called CC=1 SCAR=0.0
Pod documented
582  
583 sub pop
584 {
585 0 ▶ my $self = CORE::shift( @_ );
Subroutine pop: not called CC=4 SCAR=30.0
Pod documented
586 0 my $data = $self->items;
587 0 my $what = lc( Want::wantref );
588 0 my $val = $self->items->pop;
589  
590 0 ▶ if( !defined( $val ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $val) 0 0 0
591 {
592 0 ▶ if( $what eq 'object' || $what eq 'hash' )
Branch: 1 branch
BranchTrueFalseTotal
if ($what eq "object" or $what eq "hash") 0 0 0
Condition0%0/3
$what eq "object" or $what eq "hash"
l!l&&r!l&&!r
000
MC/DC0%0/2
$what eq "object" or $what eq "hash"
$what eq "object" $what eq "hash"
Truth table0%0/3
A or B
A= $what eq "object"B= $what eq "hash"
ABresult
1X1
011
000
593 {
594 0 return( $self->new_null( type => $what ) );
595 }
596 0 return;
597 }
598 0 return( $val );
599 }
600  
601 4 ▶sub pos { return( CORE::shift->{_pos} ); }
Subroutine pos: called CC=1 SCAR=0.0
Pod documented
602  
603 1 ▶sub preprocess { return( CORE::shift->_set_get_code( 'preprocess', @_ ) ); }
Subroutine preprocess: called CC=1 SCAR=0.0
Pod documented
604  
605 2 ▶sub postprocess { return( CORE::shift->_set_get_code( 'postprocess', @_ ) ); }
Subroutine postprocess: called CC=1 SCAR=0.0
Pod documented
606  
607 sub prev
608 {
609 1 ▶ my $self = CORE::shift( @_ );
Subroutine prev: called CC=26 SCAR=61.7
Pod documented
610 1 ▶ $self->{_pos} = -1 if( !exists( $self->{_pos} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"_pos"} 0 1 1
611 1 my $data = $self->items;
612 1 my $what = lc( Want::wantref );
613 1 my $val;
614 1 ▶ if( $self->{_pos} - 1 >= 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_pos"} - 1 >= 0) 1 0 1
615 {
616 1 $val = $data->[ --$self->{_pos} ];
617 }
618  
619 1 ▶ if( !defined( $val ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $val) 0 1 1
620 {
621 # Starting from 1
622 0 ▶ my $page = $self->page // 1;
Condition0%0/2
$self->page // 1
l!l
00
MC/DC0%0/1
$self->page // 1
$self->page
Truth table0%0/2
A
A= $self->page
Aresult
11
00
623 0 ▶ if( $self->{_pos} <= 0 && $page > 1 )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_pos"} <= 0 and $page > 1) 0 0 0
Condition0%0/3
$self->{"_pos"} <= 0 and $page > 1
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"_pos"} <= 0 and $page > 1
$self->{"_pos"} <= 0 $page > 1
Truth table0%0/3
A and B
A= $self->{"_pos"} <= 0B= $page > 1
ABresult
0X0
100
111
624 {
625 0 ▶ my $req = $self->request ||
Condition0%0/2
$self->request || (return $self->error('No initial HTTP request was provided to load data.'))
l!l
00
MC/DC0%0/1
$self->request || (return $self->error('No initial HTTP request was provided to load data.'))
$self->request
Truth table0%0/2
A
A= $self->request
Aresult
11
00
626 return( $self->error( "No initial HTTP request was provided to load data." ) );
627 0 $req = $req->clone;
628 0 my $filter = $self->filter;
629 # Next page
630 0 $page--;
631 0 my $size;
632 0 ▶ if( $filter )
Branch: 1 branch
BranchTrueFalseTotal
if ($filter) { } 0 0 0
633 {
634 0 ▶ return( $self->error( "No search query set." ) ) if( !$filter->query );
Branch: 1 branch
BranchTrueFalseTotal
unless $filter->query 0 0 0
635 # 10 is Elastic Search default size
636 0 $size = $filter->size;
637 0 ▶ if( defined( $size ) && !$size )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $size and not $size) 0 0 0
Condition0%0/3
defined $size and not $size
!ll&&!rl&&r
000
MC/DC0%0/2
defined $size and not $size
defined $size not $size
Truth table0%0/3
A and not(B)
A= defined $sizeB= not $size
ABresult
0X0
100
111
638 {
639 0 ▶ $size = $self->size // DEFAULT_PAGE_SIZE;
Condition0%0/2
$self->size // 10
l!l
00
MC/DC0%0/1
$self->size // 10
$self->size
Truth table0%0/2
A
A= $self->size
Aresult
11
00
640 0 $filter->size( $size );
641 }
642 0 $filter->from( int( ( $page - 1 ) * $size ) );
643 }
644 else
645 {
646 0 my $query = $req->uri->query_form_hash;
647 0 ▶ return( $self->error( "No search query set." ) ) if( !scalar( keys( %$query ) ) || !CORE::length( $query->{'q'} // '' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless scalar keys %$query and CORE::length($query->{"q"} // "") 0 0 0
Condition0%0/5
scalar keys %$query and CORE::length($query->{"q"} // "")
!ll&&!rl&&r
000
$query->{"q"} // ""
l!l
00
MC/DC0%0/2
scalar keys %$query and CORE::length($query->{"q"} // "")
scalar keys %$query $query->{"q"}
Truth table0%0/3
A and B
A= scalar keys %$queryB= $query->{"q"}
ABresult
0X0
100
111
648 0 $size = $self->size;
649 # If size option is set, otherwise we leave it out
650 0 ▶ if( defined( $size ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $size) 0 0 0
651 {
652 0 ▶ $size //= DEFAULT_PAGE_SIZE;
Condition0%0/2
$size //= 10
l!l
00
MC/DC0%0/1
$size //= 10
$size
Truth table0%0/2
A
A= $size
Aresult
11
00
653 0 ▶ my $size_prop = $self->size_prop // 'size';
Condition0%0/2
$self->size_prop // "size"
l!l
00
MC/DC0%0/1
$self->size_prop // "size"
$self->size_prop
Truth table0%0/2
A
A= $self->size_prop
Aresult
11
00
654 0 $query->{ $size_prop } = $size;
655 }
656 0 ▶ my $page_type = $self->page_type || 'from';
Condition0%0/2
$self->page_type || 'from'
l!l
00
MC/DC0%0/1
$self->page_type || 'from'
$self->page_type
Truth table0%0/2
A
A= $self->page_type
Aresult
11
00
657 0 ▶ if( $page_type eq 'from' )
Branch: 2 branches
BranchTrueFalseTotal
if ($page_type eq 'from') { } 0 0 0
elsif ($page_type eq 'page') { } 0 0 0
658 {
659 # 0..9 or 10..19
660 0 my $offset = int( ( $page - 1 ) * $size );
661 0 $query->{from} = $offset;
662 }
663 elsif( $page_type eq 'page' )
664 {
665 0 $query->{page} = ( $page + 1 );
666 }
667 else
668 {
669 0 return( $self->error( "Unknown page type '$page_type'" ) );
670 }
671 0 $req->uri->query_form_hash( $query );
672 }
673
674 # User will need to check if there is an error if the user gets an undefined value in return,
675 # to distinguish between no more value vs a returned error
676 0 ▶ $self->load( request => $req ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->load("request", $req) or return $self->pass_error 0 0 0
677 # We set our current page to +1 if indeed we have data.
678 0 ▶ $self->page( $page ) if( !$self->items->is_empty );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->items->is_empty 0 0 0
679 }
680
681 0 ▶ if( $what eq 'object' || $what eq 'hash' )
Branch: 1 branch
BranchTrueFalseTotal
if ($what eq "object" or $what eq "hash") 0 0 0
Condition0%0/3
$what eq "object" or $what eq "hash"
l!l&&r!l&&!r
000
MC/DC0%0/2
$what eq "object" or $what eq "hash"
$what eq "object" $what eq "hash"
Truth table0%0/3
A or B
A= $what eq "object"B= $what eq "hash"
ABresult
1X1
011
000
682 {
683 0 return( $self->new_null( type => $what ) );
684 }
685 0 return;
686 }
687 1 return( $val );
688 }
689  
690 sub push
691 {
692 0 ▶ my $self = CORE::shift( @_ );
Subroutine push: not called CC=3 SCAR=24.8
Pod documented
693 0 ▶ my $this = CORE::shift( @_ ) || return( $self->error( "Nothing was provided to add to the list of object." ) );
Condition0%0/2
CORE::shift @_ || (return $self->error('Nothing was provided to add to the list of object.'))
l!l
00
MC/DC0%0/1
CORE::shift @_ || (return $self->error('Nothing was provided to add to the list of object.'))
CORE::shift @_
Truth table0%0/2
A
A= CORE::shift @_
Aresult
11
00
694 0 ▶ $self->_check( $this ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_check($this) or return $self->pass_error 0 0 0
695 0 $self->items->push( $this );
696 0 return( $self );
697 }
698  
699 1 ▶sub request { CORE::return( shift->_set_get_object_without_init( 'request', 'HTTP::Promise::Request', @_ ) ); }
Subroutine request: called CC=1 SCAR=0.0
Pod documented
700  
701 sub shift
702 {
703 0 ▶ my $self = CORE::shift( @_ );
Subroutine shift: not called CC=4 SCAR=30.0
Pod documented
704 0 my $data = $self->items;
705 0 my $what = lc( Want::wantref );
706 0 my $val = $self->items->shift;
707  
708  
709 0 ▶ if( !defined( $val ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $val) 0 0 0
710 {
711 0 ▶ if( $what eq 'object' || $what eq 'hash' )
Branch: 1 branch
BranchTrueFalseTotal
if ($what eq "object" or $what eq "hash") 0 0 0
Condition0%0/3
$what eq "object" or $what eq "hash"
l!l&&r!l&&!r
000
MC/DC0%0/2
$what eq "object" or $what eq "hash"
$what eq "object" $what eq "hash"
Truth table0%0/3
A or B
A= $what eq "object"B= $what eq "hash"
ABresult
1X1
011
000
712 {
713 0 return( $self->new_null( type => $what ) );
714 }
715 0 return;
716 }
717 0 return( $val );
718 }
719  
720 1 ▶sub size { return( CORE::shift->_set_get_number( 'size', @_ ) ); }
Subroutine size: called CC=1 SCAR=0.0
Pod documented
721  
722 1 ▶sub size_prop { return( CORE::shift->_set_get_scalar_as_object( 'size_prop', @_ ) ); }
Subroutine size_prop: called CC=1 SCAR=0.0
Pod documented
723  
724 # NOTE: timed_out is returned by MetaCPAN API (Elastic Search)
725 2 ▶sub timed_out { return( CORE::shift->_set_get_boolean( 'timed_out', @_ ) ); }
Subroutine timed_out: called CC=1 SCAR=0.0
Pod documented
726  
727 # NOTE: took is returned by MetaCPAN API (Elastic Search)
728 2 ▶sub took { return( CORE::shift->_set_get_number( { field => 'took', undef_ok => 1 }, @_ ) ); }
Subroutine took: called CC=1 SCAR=0.0
Pod documented
729  
730 # NOTE: total returns the number returned by MetaCPAN API, which represents the overal total number of hits across all pages
731 3 ▶sub total { return( CORE::shift->_set_get_number( 'total', @_ ) ); }
Subroutine total: called CC=1 SCAR=0.0
Pod documented
732  
733 5 ▶sub type { return( CORE::shift->_set_get_scalar_as_object( 'type', @_ ) ); }
Subroutine type: called CC=1 SCAR=0.0
Pod documented
734  
735 sub unshift
736 {
737 0 ▶ my $self = CORE::shift( @_ );
Subroutine unshift: not called CC=3 SCAR=24.8
Pod documented
738 0 ▶ my $this = CORE::shift( @_ ) || return( $self->error( "Nothing was provided to add to the list of object." ) );
Condition0%0/2
CORE::shift @_ || (return $self->error('Nothing was provided to add to the list of object.'))
l!l
00
MC/DC0%0/1
CORE::shift @_ || (return $self->error('Nothing was provided to add to the list of object.'))
CORE::shift @_
Truth table0%0/2
A
A= CORE::shift @_
Aresult
11
00
739 0 ▶ $self->_check( $this ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_check($this) or return $self->pass_error 0 0 0
740 0 $self->items->unshift( $this );
741 0 return( $self );
742 }
743  
744 sub _check
745 {
746 0 ▶ my $self = CORE::shift( @_ );
Subroutine _check: not called CC=12 SCAR=50.5
747 0 ▶ my $this = CORE::shift( @_ ) || return( $self->error( "No data was provided to check." ) );
Condition0%0/2
CORE::shift @_ || (return $self->error('No data was provided to check.'))
l!l
00
MC/DC0%0/1
CORE::shift @_ || (return $self->error('No data was provided to check.'))
CORE::shift @_
Truth table0%0/2
A
A= CORE::shift @_
Aresult
11
00
748 0 ▶ return( $self->error( "Data provided is not an object." ) ) if( !$self->_is_object( $this ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_object($this) 0 0 0
749 # Check if there is any data and if there is find out what kind of object we are holding so we can maintain consistency
750 0 my $data = $self->items;
751 0 my $obj_name;
752 0 ▶ if( !$data->is_empty && $self->_is_object( $data->[0] ) )
Branch: 1 branch
BranchTrueFalseTotal
if (not $data->is_empty and $self->_is_object($data->[0])) 0 0 0
Condition0%0/3
not $data->is_empty and $self->_is_object($data->[0])
!ll&&!rl&&r
000
MC/DC0%0/2
not $data->is_empty and $self->_is_object($data->[0])
not $data->is_empty $self->_is_object($data->[0])
Truth table0%0/3
not(A) and B
A= not $data->is_emptyB= $self->_is_object($data->[0])
ABresult
0X0
100
111
753 {
754 0 ▶ $obj_name = $data->[0]->object if( $data->[0]->can( 'object' ) );
Branch: 1 branch
BranchTrueFalseTotal
if $data->[0]->can("object") 0 0 0
755 }
756 0 ▶ if( $this->can( 'object' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($this->can("object")) 0 0 0
757 {
758 0 my $this_object = $this->object;
759 0 ▶ $this_object = '' if( !defined( $this_object ) || !$this_object->defined );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $this_object and $this_object->defined 0 0 0
Condition0%0/3
defined $this_object and $this_object->defined
!ll&&!rl&&r
000
MC/DC0%0/2
defined $this_object and $this_object->defined
defined $this_object $this_object->defined
Truth table0%0/3
A and B
A= defined $this_objectB= $this_object->defined
ABresult
0X0
100
111
760 0 ▶ $obj_name = '' if( !defined( $obj_name ) || !$obj_name->defined );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $obj_name and $obj_name->defined 0 0 0
Condition0%0/3
defined $obj_name and $obj_name->defined
!ll&&!rl&&r
000
MC/DC0%0/2
defined $obj_name and $obj_name->defined
defined $obj_name $obj_name->defined
Truth table0%0/3
A and B
A= defined $obj_nameB= $obj_name->defined
ABresult
0X0
100
111
761 0 ▶ return( $self->error( "Object provided (", overload::StrVal( $this ), ") has an object type (${this_object}) different from the ones currently in our stack (${obj_name})." ) ) if( $this_object ne $obj_name );
Branch: 1 branch
BranchTrueFalseTotal
if $this_object ne $obj_name 0 0 0
762 }
763 0 return( $this );
764 }
765  
766 3 1;
767 # NOTE: POD
768 __END__
769  
770 =encoding utf-8
771  
772 =head1 NAME
773  
774 Net::API::CPAN::List - Meta CPAN API List
775  
776 =head1 SYNOPSIS
777  
778 use Net::API::CPAN::List;
779 my $list = Net::API::CPAN::List->new(
780 items => $array_ref,
781 ) || die( Net::API::CPAN::List->error, "\n" );
782 # or
783 my $list = Net::API::CPAN::List->new;
784 $list->load_data( $hash_ref ) || die( $list->error );
785  
786 =head1 VERSION
787  
788 v0.1.1
789  
790 =head1 DESCRIPTION
791  
792 This class is used to retrieve and manipulate list of data such as the ones resulting from a search query.
793  
794 It inherits from L<Net::API::CPAN::Generic>
795  
796 =head1 CONSTRUCTOR
797  
798 =head2 new
799  
800 Provided with an hash or an hash reference of parameters and this will instantiate a new list object.
801  
802 The valid parmeters that can be used are as below and can also be accessed with their corresponding method:
803  
804 =over 4
805  
806 =item * C<api>
807  
808 An L<Net::API::CPAN> object.
809  
810 =item * C<items>
811  
812 An array reference of data.
813  
814 =back
815  
816 =head1 METHODS
817  
818 =head2 api_uri
819  
820 Sets or gets the MetaCPAN API base URI, which defaults to L<https://fastapi.metacpan.org>
821  
822 This returns an L<URI> object.
823  
824 =head2 api_version
825  
826 Sets or gets the MetaCPAN API version, which defaults to C<1>. This is used to form the base of the endpoints, such as C</v1/author/search>
827  
828 It returns a L<scalar object|Module::Generic::Scalar>
829  
830 =head2 container
831  
832 Sets or gets a string representing the property containing the array of all the data.
833  
834 If this is not set, then L</load_data> will try to guess it.
835  
836 =head2 data
837  
838 $list->data( $hash_ref ) || die( $list->error );
839 my $array_ref = $list->data;
840  
841 This is a convenient shortcut that calls L<load_data|/load_data> when data is provided, and returns the call to L<items|/items> either way.
842  
843 =head2 filter
844  
845 Sets or gets the L<filter object|Net::API::CPAN::Filter>. It returns C<undef> in scalar context if none is set, but it instantiates automatically a new instance in object context.
846  
847 my $filter = $list->filter; # undef
848 my $from = $list->filter->from; # ok, it works, but still undef
849  
850 =head2 get
851  
852 # implicit
853 my $obj = $list->get;
854 # explicit
855 my $obj = $list->get(0);
856 # or
857 my $obj = $list->get(2);
858  
859 This returns the data object at the current offset, or at the provided offset if one was provided.
860  
861 If no data object exists at the offset, this will return C<undef> in scalar context, or an empty list in list context, and it will return a L<null object|Module::Generic::Null> in object context to prevent a perl error of some methods called with an undefined value. The null object virtual method call will return C<undef> eventually.
862  
863 For example, let's assume a list of only 1 element. Calling C<get> with offset C<3> exceeds the size of the data pool, and would return an C<undef> value, but since it is called in object context, it returns a L<null object|Module::Generic::Null> instead.
864  
865 my $undef = $list->get(3)->author;
866  
867 =head2 has_more
868  
869 Read-only. This will return true if there are more data to be fetched from the MetaCPAN API, or false otherwise.
870  
871 =head2 http_request
872  
873 Sets or gets an L<HTTP::Promise::Request> object.
874  
875 =head2 http_response
876  
877 Sets or gets an L<HTTP::Promise::Response> object.
878  
879 =head2 items
880  
881 Provided with an array reference and an hash or hash reference of options and this sets the provided array as the active pool of data.
882  
883 The data contained can be either an array reference of hash reference, or an array reference of objects. If the data provided are an array reference of hash reference, they will be turned into their corresponding object, based on the value of each hash C<_type> property, such as C<< _type => "author" >>
884  
885 It always return an L<array object|Module::Generic::Array>, whether any data were provided or not.
886  
887 =head2 load
888  
889 Using the L<filter object|Net::API::CPAN::Filter> accessible with L</filter>, this will issue a new HTTP C<POST> query to the MetaCPAN API endpoint to retrieve the C<JSON> data.
890  
891 It will then populate the data, notably to C<items>, C<timed_out>, C<total>, C<took> and return the current object for chaining.
892  
893 It also sets the HTTP request object and the HTTP response object that can be retrieved with L</http_request> and L</http_response> respectively.
894  
895 If an error occurred, it will set an L<error object|Net::API::CPAN::Exception>, and return C<undef> in scalar context or an empty list in list context.
896  
897 There is no need to access C<load> directly. This would be called automatically when more data is requested and if there is indeed more data.
898  
899 =head2 load_data
900  
901 Provided with an hash reference of data, and this will load it into the current object, and return the current object for chaining. Upon error, this will set an L<error object|Net::API::CPAN::Exception> and return C<undef> in scalar context, or an empty list in list context.
902  
903 =head2 length
904  
905 Read-only. This returns the size of the data pool as a L<number object|Module::Generic::Number>.
906  
907 A 10-elements data pool would return 10. The value returned is directly related to the sie of the arra reference data pool, so if you use the methods L</pop>, L</unshift>, L</shift>, L</unshift>, it will affect the value returned here.
908  
909 See also L</total>
910  
911 =head2 next
912  
913 my $obj = $list->next;
914  
915 This returns the next data object in the data pool, or if none exists, C<undef> in scalar context and an empty list in list context, and it will return a L<null object|Module::Generic::Null> in object context to prevent a perl error of some methods called with an undefined value. The null object virtual method call will return C<undef> eventually.
916  
917 For example, let's assume the list is empty. Calling C<next> would return an C<undef> value, but since it is called in object context, it returns a L<null object|Module::Generic::Null> instead.
918  
919 my $undef = $list->next->author;
920  
921 The size of the data pool returned from the MetaCPAN REST API is based upon the value of L<size|/size>, which usually defaults to C<10>. Once C<next> has reached the last element in the data pool, it will attempt to load more data, if there are more to load at all. To know that, it calls L</has_more>. Thus, when C<undef> is returned, it really means, there is no more data to retrieve.
922  
923 =head2 offset
924  
925 Read-only. Returns the offset position of the current item across the entire data set.
926  
927 For example, if you are currently checking the 3rd element of the 2 data page, the offset value would be C<12>, because offset starts at C<0>
928  
929 This returns a L<number object|Module::Generic::Number>
930  
931 See also L</pos>
932  
933 =head2 page
934  
935 Integer. Sets or gets an integer representing the current page of data being used.
936  
937 Returns a L<number object|Module::Generic::Number>
938  
939 =head2 page_type
940  
941 Sets or gets a L<scalar object|Module::Generic::Scalar> representing the type of paging used to access next and previous pages. Possible values are C<from> and C<page>, and this defaults to C<from>
942  
943 When it is set to C<from>, L<load|/load> will use a data offset starting from C<0>. For example, on a data set broken into pages of 20 elements each, moving to the 2 pages would set the C<from> value to C<20>.
944  
945 If C<page_type> is set to C<page>, the L<page|/page> number starting from C<1> will be used.
946  
947 =head2 pageable
948  
949 Boolean. Sets or gets a boolean value representing whether more data can be loaded beyond the current set, or if the current set the only set of available data.
950  
951 Returns a L<boolean object|Module::Generic::Boolean>
952  
953 For example, L<autocomplete|Net::API::CPAN/autocomplete> returns a set of 10 elements, but is not pageable.
954  
955 =head2 page_size
956  
957 Integer. This indicates the size of each result page. Contrary to L<size|/size>, which is a preference that can be set to indicate how many result per page one wants, C<page_size> is set upon loading data with L<load_data|/load_data> and reflects the actul page size.
958  
959 If the page contains only a small amount of results, such as 3, then C<page_size> will be 3, but if the overall total exceeds that of the page size, C<page_size> will show how many result per page is provided.
960  
961 This information is then used for new requests to load more data by L<next|/next> and L<prev|/prev>
962  
963 Thus it is more an internal method.
964  
965 =head2 pop
966  
967 my $obj = $list->pop;
968  
969 This removes the last entry from the data pool, thus altering it, and returns it.
970  
971 If the value to be returned is undefined, it will return C<undef> in scalar context and an empty list in list context, and it will return a L<null object|Module::Generic::Null> in object context to prevent a perl error of some methods called with an undefined value. The null object virtual method call will return C<undef> eventually.
972  
973 For example, let's assume the list is empty. Calling C<pop> would return an C<undef> value, but since it is called in object context, it returns a L<null object|Module::Generic::Null> instead.
974  
975 my $undef = $list->pop->author;
976  
977 You might want to use C<< $list->get(-1) >> instead to avoid modifying the array reference of data.
978  
979 =head2 pos
980  
981 Read-ony. Returns the current position in the array reference of data pool. This would be a positive integer, or C<undef> if no data was accessed yet.
982  
983 See also L</offset>
984  
985 =head2 postprocess
986  
987 Sets or gets the caller (anonymous subroutine) that is is called by L</load_data> with the hash reference of data received from the MetaCPAN API, for possible post processing.
988  
989 This method, not the callback, returns the current object for chaining, or upon error, sets an L<error|Net::API::CPAN::Exception> and returns C<undef> in scalar context or an empty list in list context.
990  
991 =head2 preprocess
992  
993 Sets or gets the caller (anonymous subroutine) that is is called by L</load_data> with the hash reference of data received from the MetaCPAN API, for possible pre processing.
994  
995 This method, not the callback, returns the current object for chaining, or upon error, sets an L<error|Net::API::CPAN::Exception> and returns C<undef> in scalar context or an empty list in list context.
996  
997 =head2 prev
998  
999 my $obj = $list->next;
1000  
1001 This returns the previous data object in the data pool, or if none exists, C<undef> in scalar context and an empty list in list context, and it will return a L<null object|Module::Generic::Null> in object context to prevent a perl error of some methods called with an undefined value. The null object virtual method call will return C<undef> eventually.
1002  
1003 For example, let's assume the list is empty. Calling C<prev> would return an C<undef> value, but since it is called in object context, it returns a L<null object|Module::Generic::Null> instead.
1004  
1005 my $undef = $list->prev->author;
1006  
1007 =head2 push
1008  
1009 $list->push( $object );
1010  
1011 Provided with a proper object value, and this will add it to the end of the data pool and returns the current list object for chaining purposes.
1012  
1013 A valid value must be an object of the same type as the ones used in this data pool.
1014  
1015 Upon error, this will set an L<error object|Net::API::CPAN::Exception> and returns C<undef> in scalar context and an empty list in list context.
1016  
1017 =head2 request
1018  
1019 Sets or gets the original L<HTTP request object|HTTP::Promise::Request> that was used for this list object.
1020  
1021 =head2 shift
1022  
1023 my $obj = $list->shift;
1024  
1025 This removes the first entry from the data pool, thus altering it, and returns it.
1026  
1027 If the value to be returned is undefined, it will return C<undef> in scalar context and an empty list in list context, and it will return a L<null object|Module::Generic::Null> in object context to prevent a perl error of some methods called with an undefined value. The null object virtual method call will return C<undef> eventually.
1028  
1029 For example, let's assume the list is empty. Calling C<shift> would return an C<undef> value, but since it is called in object context, it returns a L<null object|Module::Generic::Null> instead.
1030  
1031 my $undef = $list->shift->author;
1032  
1033 =head2 size
1034  
1035 Sets or gets the size of each page. Normally, this is figured out automatically by L<load_data|/load_data>, but sometimes, when large chunk of data are returning at once, and you want to break it down, setting the C<size> makes it possible.
1036  
1037 This returns a L<number object|Module::Generic::Number>
1038  
1039 =head2 size_prop
1040  
1041 Sets or gets the property name in the data containing the size of the data set. Typically this is C<size> and this is the default value.
1042  
1043 This returns a L<scalar object|Module::Generic::Scalar>
1044  
1045 =head2 timed_out
1046  
1047 Sets or gets the boolean value returned from the last API call to MetaCPAN and representing whether the query has timed out.
1048  
1049 =head2 took
1050  
1051 Sets or gets a number returned from the last API call to MetaCPAN. If set, this will return a L<number object|Module::Generic::Number>, otherwise C<undef>
1052  
1053 =head2 total
1054  
1055 Sets or gets the overall size of the data available from MetaCPAN API, possibly across multiple API calls.
1056  
1057 For example, a query might result in data of 120 elements found, and each page with 10 elements. C<total> would then be 120.
1058  
1059 It returns a L<number object|Module::Generic::Number>.
1060  
1061 See also L</length>, which returns the current size of the data at hands stored in C<items>
1062  
1063 =head2 type
1064  
1065 Sets or gets the underlying object type of the data pool. This can be C<author>, C<changes>, C<cover>, C<distribution>, C<favorite>, C<file>, C<mirror>, C<module>, C<package>, C<permission>, C<rating>, C<release>, but is not enforced, so whatever value you set is your responsibility.
1066  
1067 =head2 unshift
1068  
1069 $list->unshift( $object );
1070  
1071 Provided with a proper object value, and this will add it at the beginning of the data pool and returns the current list object for chaining purposes.
1072  
1073 A valid value must be an object of the same type as the ones used in this data pool.
1074  
1075 Upon error, this will set an L<error object|Net::API::CPAN::Exception> and returns C<undef> in scalar context and an empty list in list context.
1076  
1077 =head1 AUTHOR
1078  
1079 Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
1080  
1081 =head1 SEE ALSO
1082  
1083 L<Net::API::CPAN>, L<Net::API::CPAN::Scroll>, L<Net::API::CPAN::Filter>, L<Net::API::CPAN::Exception>, L<HTTP::Promise>
1084  
1085 =head1 COPYRIGHT & LICENSE
1086  
1087 Copyright(c) 2023 DEGUEST Pte. Ltd.
1088  
1089 All rights reserved
1090  
1091 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
1092  
1093 =cut
« blib/lib/Net/API/CPAN/File.pm ↑ Summary blib/lib/Net/API/CPAN/Release.pm »