blib/lib/Net/API/CPAN/List/Web.pm untested

filter: Statementstmt 0.0% 0 / 70 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 8 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 115 CC 6 SCAR 18.3
CC
6
Cov
0%
load_data
20.0
init
12.0
BEGIN
2.0
CRAP 6.2
1 ##----------------------------------------------------------------------------
2 ## Meta CPAN API - ~/lib/Net/API/CPAN/List/Web.pm
3 ## Version v0.1.0
4 ## Copyright(c) 2023 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2023/09/23
7 ## Modified 2023/09/23
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::Web;
15 BEGIN
16 {
17 use strict;
18 use warnings;
19 use parent qw( Net::API::CPAN::List );
20 use vars qw( $VERSION );
21 our $VERSION = 'v0.1.0';
22 };
23  
24 use strict;
25 use warnings;
26  
27 sub init
28 {
29 my $self = shift( @_ );
30 # We capture the options passed, and we remove 'hits' that is treated specially
31 my $opts = $self->_get_args_as_hash( @_ );
32 $opts->{data} = { hits => delete( $opts->{hits} ) };
33 $self->{container} = 'hits';
34 # The elements will be instantiated as Net::API::CPAN::List::Web::Element objects
35 $self->{type} = 'Net::API::CPAN::List::Web::Element';
36 $self->{_init_strict_use_sub} = 1;
37 # We want the property 'total' to be processed first, so it can be overriden if necessary
38 $self->{_init_params_order} = [qw( distribution total data )];
39 $self->SUPER::init( %$opts) || return( $self->pass_error );
40 $self->message( 4, "Instantiating ", ref( $self ), " object for distribution '", $opts->{distribution}, "' with total ", $self->total );
41 return( $self );
42 }
43  
44 sub load_data
45 {
46 my $self = shift( @_ );
47 my $data = CORE::shift( @_ ) ||
48 return( $self->error( "No data was provided to load." ) );
49 return( $self->error( "Data provided is not an hash reference." ) ) if( ref( $data ) ne 'HASH' );
50 if( !exists( $data->{hits} ) )
51 {
52 return( $self->error( "No property 'hits' in the data provided." ) );
53 }
54 $self->message( 4, "Loading data received with ", scalar( keys( %$data ) ), " properties: ", join( ', ', sort( keys( %$data ) ) ) );
55 $self->distribution( $data->{hits}->[0]->{distribution} ) if( !$self->distribution );
56 $self->SUPER::load_data( $data ) || return( $self->pass_error );
57 # We cannot trust the property 'total' unfortunately
58 $self->message( 4, "Setting total to '", $self->items->length, "'" );
59 $self->total( $self->items->length );
60 return( $self );
61 }
62  
63 sub distribution { return( shift->_set_get_scalar_as_object( 'distribution', @_ ) ); }
64  
65 # NOTE: Net::API::CPAN::List::Web::Element class
66 package
67 Net::API::CPAN::List::Web::Element;
68 BEGIN
69 {
70 use strict;
71 use warnings;
72 use parent qw( Net::API::CPAN::Module );
73 };
74  
75 use strict;
76 use warnings;
77  
78 sub init
79 {
80 my $self = shift( @_ );
81 $self->{favorites} = undef unless( exists( $self->{favorites} ) );
82 $self->{score} = undef unless( exists( $self->{score} ) );
83 $self->{_init_strict_use_sub} = 1;
84 $self->SUPER::init( @_ ) || return( $self->pass_error );
85 $self->{fields} = [qw(
86 abstract author authorized date description distribution documentation favorites
87 id indexed path pod_lines release score status
88 )];
89 return( $self );
90 }
91  
92 sub favorites { return( shift->_set_get_number( { field => 'favorites', undef_ok => 1 }, @_ ) ); }
93  
94 sub score { return( shift->_set_get_number( { field => 'score', undef_ok => 1 }, @_ ) ); }
95  
96 1;
97 # NOTE: POD
98 __END__
99  
100 =encoding utf-8
101  
102 =head1 NAME
103  
104 Net::API::CPAN::List::Web - Web Search Result List Object Class
105  
106 =head1 SYNOPSIS
107  
108 use Net::API::CPAN::List::Web;
109 my $list = Net::API::CPAN::List::Web->new(
110 {
111 distribution => "Folklore-Japan",
112 hits => [
113 {
114 abstract => "Japan Folklore Object Class",
115 author => "MOMOTARO",
116 authorized => 1,
117 date => "2023-07-17T09:43:41",
118 description => "Folklore::Japan is a totally fictious perl 5 module designed to serve as an example for the MetaCPAN API.",
119 distribution => "Folklore-Japan",
120 documentation => "Folklore::Japan",
121 favorites => 1,
122 id => "abcd1234edfgh56789",
123 indexed => 1,
124 module => [
125 {
126 associated_pod => "MOMOTARO/Folklore-Japan-v0.1.0/lib/Folklore/Japan.pm",
127 authorized => 1,
128 indexed => 1,
129 name => "Folklore::Japan",
130 version => 'v0.1.0',,
131 version_numified => 0.001000,
132 },
133 ],
134 path => "lib/Folklore/Japan.pm",
135 pod_lines => [12, 320],
136 release => "Folklore-Japan-v0.1.0",
137 score => 0.031563006,
138 status => "latest",
139 },
140 ],
141 total => 1,
142 }
143 ) || die( Net::API::CPAN::List::Web->error );
144  
145 =head1 VERSION
146  
147 v0.1.0
148  
149 =head1 DESCRIPTION
150  
151 This object class is used to represent web search result. It inherits from L<Net::API::CPAN::List>, because this result set is basically a result set within a result set.
152  
153 This class object type C<list_web> is used when instantiating a new L<Net::API::CPAN::List> object, so that each of the data array elements are instantiated as an object of this class.
154  
155 So the overall structure would look like this:
156  
157 Net::API::CPAN::List object = [
158 Net::API::CPAN::List::Web object,
159 Net::API::CPAN::List::Web object,
160 Net::API::CPAN::List::Web object,
161 # etc..
162 ]
163  
164 =head1 METHODS
165  
166 For all other methods, please refer to this class parent L<Net::API::CPAN::List>
167  
168 =head2 distribution
169  
170 String. This represents the distribution name for this sub-result set.
171  
172 =head1 AUTHOR
173  
174 Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
175  
176 =head1 SEE ALSO
177  
178 L<Net::API::CPAN::List>
179  
180 =head1 COPYRIGHT & LICENSE
181  
182 Copyright(c) 2023 DEGUEST Pte. Ltd.
183  
184 All rights reserved
185  
186 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
187  
188 =cut