lib/Net/API/CPAN/Generic.pm

filter: Statementstmt 35.6% 31 / 87 Branchbran 10.7% 3 / 28 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 73.3% 11 / 15 Podpod 83.3% 5 / 6 totaltotal 29.5% 50 / 169 CC 26 SCAR 42.0
CC
26
Cov
25%
populate
156.0
apply
72.0
TO_JSON
32.2
CRAP 66.6
« blib/lib/Net/API/CPAN/Filter.pm ↑ Summary blib/lib/Net/API/CPAN/Cve.pm »
1 ##----------------------------------------------------------------------------
2 ## Meta CPAN API - ~/lib/Net/API/CPAN/Generic.pm
3 ## Version v0.1.0
4 ## Copyright(c) 2023 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2023/07/26
7 ## Modified 2023/07/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::Generic;
15 BEGIN
16 {
17 24 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 24 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 24 ▶ use parent qw( Module::Generic );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 24 ▶ use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 24 ▶ our $VERSION = 'v0.1.0';
Subroutine BEGIN: called CC=1 SCAR=0.0
22 };
23  
24 24 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 24 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 sub init
28 {
29 33 ▶ my $self = shift( @_ );
Subroutine init: called CC=3 SCAR=11.0
Pod documented
30 33 ▶ $self->{api} = undef unless( CORE::exists( $self->{api} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"api"} 32 1 33
31 33 $self->{_init_strict_use_sub} = 1;
32 33 $self->{_exception_class} = 'Net::API::CPAN::Exception';
33 33 ▶ $self->SUPER::init( @_ ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::init(@_) or return $self->pass_error 0 33 33
34 33 return( $self );
35 }
36  
37 10 ▶sub api { return( shift->_set_get_object( 'api', 'Net::API::CPAN', @_ ) ); }
Subroutine api: called CC=1 SCAR=0.0
Pod documented
38  
39 sub apply
40 {
41 0 ▶ my $self = shift( @_ );
Subroutine apply: not called CC=8 SCAR=42.8
Pod documented
42 0 my $hash = $self->_get_args_as_hash( @_ );
43 0 ▶ return( $self ) if( !scalar( keys( %$hash ) ) );
Branch: 1 branch
BranchTrueFalseTotal
unless scalar keys %$hash 0 0 0
44 0 ▶ if( CORE::exists( $self->{_init_preprocess} ) &&
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"_init_preprocess"} and ref $self->{"_init_preprocess"} eq "CODE") 0 0 0
Condition0%0/3
exists $self->{"_init_preprocess"} and ref $self->{"_init_preprocess"} eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
exists $self->{"_init_preprocess"} and ref $self->{"_init_preprocess"} eq "CODE"
exists $self->{"_init_preprocess"} ref $self->{"_init_preprocess"} eq "CODE"
Truth table0%0/3
A and B
A= exists $self->{"_init_preprocess"}B= ref $self->{"_init_preprocess"} eq "CODE"
ABresult
0X0
100
111
45 ref( $self->{_init_preprocess} ) eq 'CODE' )
46 {
47 0 $hash = $self->{_init_preprocess}->( $hash );
48 }
49
50 0 foreach my $k ( keys( %$hash ) )
51 {
52 0 my $code;
53 # if( !CORE::exists( $dict->{ $k } ) )
54 0 ▶ if( !( $code = $self->can( $k ) ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($code = $self->can($k)) 0 0 0
55 {
56 0 ▶ warn( "No method \"$k\" found in class ", ( ref( $self ) || $self ), " when applying data to this object. Skipping it." ) if( $self->_is_warnings_enabled );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_warnings_enabled 0 0 0
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
57 0 next;
58 }
59 0 $code->( $self, $hash->{ $k } );
60 }
61 0 return( $self );
62 }
63  
64 # sub as_hash
65 # {
66 # my $self = shift( @_ );
67 # my $hash = {};
68 # my $fields;
69 # if( !$self->can( 'fields' ) )
70 # {
71 # warn( "Method fields is not implemented in this class '", ( ref( $self ) || $self ), "'." );
72 # $fields = $self->new_array( [grep( !/^(_|debug|verbose|error|version)/, keys( %$self ) )] );
73 # }
74 # else
75 # {
76 # $fields = $self->fields;
77 # }
78 # $self->fields->foreach(sub
79 # {
80 # $hash->{ $_ } = $self->$_();
81 # });
82 # return( $hash );
83 # }
84  
85 0 ▶sub fields { return( shift->_set_get_array_as_object( 'fields', @_ ) ); }
Subroutine fields: not called CC=1 SCAR=6.9
Pod documented
86  
87 # Takes an hash of data retrieved from the remote REST API, and fill all the class properties with it
88 sub populate
89 {
90 0 ▶ my $self = shift( @_ );
Subroutine populate: not called CC=12 SCAR=50.5
Pod documented
91 0 ▶ my $ref = shift( @_ ) || return( $self->error( "No hash to populate was provided." ) );
Condition0%0/2
shift @_ || (return $self->error('No hash to populate was provided.'))
l!l
00
MC/DC0%0/1
shift @_ || (return $self->error('No hash to populate was provided.'))
shift @_
Truth table0%0/2
A
A= shift @_
Aresult
11
00
92 0 ▶ return( $self->error( "Hash provided is not an hash reference." ) ) if( ref( $ref ) ne 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $ref ne "HASH" 0 0 0
93  
94 0 ▶ if( CORE::exists( $self->{_init_preprocess} ) &&
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"_init_preprocess"} and ref $self->{"_init_preprocess"} eq "CODE") 0 0 0
Condition0%0/3
exists $self->{"_init_preprocess"} and ref $self->{"_init_preprocess"} eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
exists $self->{"_init_preprocess"} and ref $self->{"_init_preprocess"} eq "CODE"
exists $self->{"_init_preprocess"} ref $self->{"_init_preprocess"} eq "CODE"
Truth table0%0/3
A and B
A= exists $self->{"_init_preprocess"}B= ref $self->{"_init_preprocess"} eq "CODE"
ABresult
0X0
100
111
95 ref( $self->{_init_preprocess} ) eq 'CODE' )
96 {
97 0 $ref = $self->{_init_preprocess}->( $ref );
98 }
99
100 0 my $keys;
101 0 my $dubious = 0;
102 0 ▶ if( scalar( @_ ) == 1 && $self->_is_array( $_[0] ) )
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @_ == 1 and $self->_is_array($_[0])) { } 0 0 0
elsif ($self->can('fields')) { } 0 0 0
Condition0%0/3
scalar @_ == 1 and $self->_is_array($_[0])
!ll&&!rl&&r
000
MC/DC0%0/2
scalar @_ == 1 and $self->_is_array($_[0])
scalar @_ == 1 $self->_is_array($_[0])
Truth table0%0/3
A and B
A= scalar @_ == 1B= $self->_is_array($_[0])
ABresult
0X0
100
111
103 {
104 0 $dubious++;
105 0 $keys = $self->new_array( @{$_[0]} );
106 }
107 elsif( $self->can( 'fields' ) )
108 {
109 0 $keys = $self->fields->clone;
110 }
111 else
112 {
113 0 $dubious++;
114 0 $keys = [keys( %$ref )];
115 }
116
117 0 foreach my $this ( @$keys )
118 {
119 0 my $meth = $this;
120 0 $meth =~ tr/-/_/;
121 0 ▶ if( $dubious && !$self->can( $meth ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($dubious and not $self->can($meth)) 0 0 0
Condition0%0/3
$dubious and not $self->can($meth)
!ll&&!rl&&r
000
MC/DC0%0/2
$dubious and not $self->can($meth)
$dubious not $self->can($meth)
Truth table0%0/3
A and not(B)
A= $dubiousB= not $self->can($meth)
ABresult
0X0
100
111
122 {
123 0 ▶ warn( "No method found for \"$meth\" in class ", ( ref( $self ) || $self ), " when populating data. Skipping it." );
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
124 0 next;
125 }
126 0 $self->$meth( $ref->{ $this } );
127 }
128 0 return( $self );
129 }
130  
131 1 ▶sub _object_type_to_class { return( shift->api->_object_type_to_class( @_ ) ); }
Subroutine _object_type_to_class: called CC=1 SCAR=0.0
132  
133 sub TO_JSON
134 {
135 0 ▶ my $self = shift( @_ );
Subroutine TO_JSON: not called CC=6 SCAR=34.7
Pod undocumented
136 0 my $hash = {};
137 0 ▶ if( $self->can( 'fields' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can('fields')) { } 0 0 0
138 {
139 0 my $keys = $self->fields;
140 0 foreach my $f ( @$keys )
141 {
142 0 $hash->{ $f } = $self->$f();
143 }
144 }
145 else
146 {
147 # my $hash = $self->as_hash;
148 # return( $hash );
149 0 my $class = ref( $self );
150 24 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
151 0 my @methods = grep( !/^(?:new|init|TO_JSON|FREEZE|THAW|AUTOLOAD|DESTROY)$/, grep{ defined &{"${class}::$_"} } keys( %{"${class}::"} ) );
152 0 foreach my $meth ( sort( @methods ) )
153 {
154 0 ▶ next if( substr( $meth, 0, 1 ) eq '_' );
Branch: 1 branch
BranchTrueFalseTotal
if substr($meth, 0, 1) eq "_" 0 0 0
155 0 local $@;
156 0 my $rv = eval{ $self->$meth };
157 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
158 {
159 0 warn( "An error occured while accessing method $meth: $@\n" );
160 0 next;
161 }
162 0 $hash->{ $meth } = $rv;
163 }
164 }
165 0 return( $hash );
166 }
167  
168 24 1;
169 # NOTE: POD
170 __END__
171  
172 =encoding utf-8
173  
174 =head1 NAME
175  
176 Net::API::CPAN::Generic - Meta CPAN API Generic Class
177  
178 =head1 SYNOPSIS
179  
180 use Net::API::CPAN::Generic;
181 package Net::API::CPAN::Author;
182 use parent qw( Net::API::CPAN::Generic );
183 # ...
184  
185 =head1 VERSION
186  
187 v0.1.0
188  
189 =head1 DESCRIPTION
190  
191 C<Net::API::CPAN::Generic> contains some standard methods to inherit from.
192  
193 =head1 METHODS
194  
195 =head2 init
196  
197 Initialise some default properties and return the current object.
198  
199 This C<init> method is called by L<Module::Generic/new>
200  
201 =head2 api
202  
203 Sets or gets the C<Net::API::CPAN> API object.
204  
205 In scalar context, this would return C<undef> if none is defined yet, but in object context, this would automatically instantiate a new C<Net::API::CPAN> object. For example:
206  
207 my $api = $obj->api; # undef
208 my $resp = $api->ua->get( $somewhere ); # HTTP::Promise::Response
209  
210 =head2 apply
211  
212 $obj->apply( key1 => $val1, key2 => $val2 );
213 $obj->apply({ key1 => $val1, key2 => $val2 });
214  
215 This takes an hash or an hash reference of key-value pairs, and this will call the corresponding method if they exist in the object class, and set the associated value.
216  
217 It returns the current object.
218  
219 =head2 as_hash
220  
221 my $hash_ref = $obj->as_hash;
222  
223 This returns an hash reference of key-value pairs corresponding to all the object class methods.
224  
225 =head2 fields
226  
227 Sets or gets an L<array object|Module::Generic::Array> of the package methods.
228  
229 =head2 populate
230  
231 This is a variation of L<apply|/apply>. It takes an hash reference, and an optional array reference of associated properties to set their values. If no array reference is specified, it will use the object C<fields> methods to get the object class known properties if the C<fields> method is supported, otherwise, it will use all they hash reference keys as a default array reference of properties to set.
232  
233 It returns the current object upon success, 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.
234  
235 =for Pod::Coverage _object_type_to_class
236  
237 =head1 ERRORS
238  
239 This module does not die or croak, but instead set an L<error object|Net::API::CPAN::Exception> using L<Module::Generic/error> and returns C<undef> in scalar context, or an empty list in list context.
240  
241 You can retrieve the latest error object set by calling L<error|Module::Generic/error> inherited from L<Module::Generic>
242  
243 Errors issued by this distributions are all instances of class L<Net::API::CPAN::Exception>
244  
245 =head1 AUTHOR
246  
247 Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
248  
249 =head1 SEE ALSO
250  
251 L<perl>
252  
253 =head1 COPYRIGHT & LICENSE
254  
255 Copyright(c) 2023 DEGUEST Pte. Ltd.
256  
257 All rights reserved
258  
259 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
260  
261 =cut
« blib/lib/Net/API/CPAN/Filter.pm ↑ Summary blib/lib/Net/API/CPAN/Cve.pm »