lib/Net/Async/Spotify/Object/Base.pm

filter: Statementstmt 29.2% 33 / 113 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 52.6% 10 / 19 Podpod 62.5% 5 / 8 totaltotal 22.1% 48 / 217 CC 40 SCAR 58.2
CC
40
Cov
18%
apply_type
702.0
to_hash
110.0
generate
22.6
CRAP 336.2
↑ Summary lib/Net/Async/Spotify/API/Base.pm »
1 package Net::Async::Spotify::Object::Base;
2  
3 128 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 128 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 128 our $VERSION = '0.002'; # VERSION
7 128 our $AUTHORITY = 'cpan:VNEALV'; # AUTHORITY
8  
9 128 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 128 ▶use Syntax::Keyword::Try;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 128 ▶use Module::Runtime qw(require_module);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 128 ▶use Time::Moment;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 128 ▶use JSON::MaybeUTF8 qw(:v1);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 128 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 128 ▶use Net::Async::Spotify::Util qw(hash_to_string);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 =head1 NAME
18  
19 Net::Async::Spotify::Object::Base - Base Package for Spotify Object Classes.
20  
21 =head1 DESCRIPTION
22  
23 Base class to be used by all Autogenerated modules of Spotify Objects
24 It does so by creating an accessor that goes by the name of each field in the object.
25 It also applies type casting to passed values, making sure all types are matching.
26 If an undefined field passed when creating, it will be populated in L</extra_fields>
27 Defined fields are specific to each L<Net::Async::Spotify::Object> and can be viewed in class itself.
28  
29 =head1 METHODS
30  
31 =cut
32  
33 0 ▶sub fields { shift->{fields} }
Subroutine fields: not called CC=1 SCAR=6.9
Pod undocumented
34  
35 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
36 0 my ( $class, $fields, %args ) = @_;
37 0 my $self = bless {}, $class;
38 0 $self->{fields} = $fields;
39 0 $self->generate(%args);
40 0 return $self;
41 }
42  
43 0 ▶sub generate {
Subroutine generate: not called CC=5 SCAR=31.2
Pod undocumented
44 0 my ( $self, %args ) = @_;
45 0 my %fields = $self->fields->%*;
46  
47 0 for my $field (keys %fields) {
48 0 ▶ next if $field eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $field eq "" 0 0 0
49 0 my $accessor = join '::', ref($self), $field;
50 {
51 # here we need to allow hard references
52 # so we can add accessors to calling class.
53 0 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.2
54  
55 # Only add accessor when it does not exist
56 # also do not repeat added ones.
57 0 ▶ *{$accessor} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
58 0 my ( $self, $value ) = @_;
59  
60 0 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 0 0
61 0 $value = $self->apply_type($fields{$field}, $value, $field);
62 0 $self->{$field} = $value;
63 }
64 0 return $self->{$field};
65 }
66 0 ▶ unless defined &{$accessor};
Branch: 1 branch
BranchTrueFalseTotal
unless defined &{$accessor;} 0 0 0
67 }
68 0 $self->$field(delete $args{$field});
69 }
70 # Fields that are added but not present in documentation and current Object class
71 0 for my $k (keys %args) {
72 0 $log->tracef('Unknown field %s passed | Object: %s | will add to `extra_fields`', $k, ref $self);
73 0 $self->{extra_fields}{$k} = $args{$k};
74 }
75  
76 }
77  
78 =head2 apply_type
79  
80 an internal function attached to every field accessor.
81 Casting the value of each field depending on it's type.
82 I believe this may not be the optimum way to do it.
83 However for the time being I think it works great.
84 Here are the available types that it checks for:
85  
86 =over 4
87  
88 =item *
89  
90 Integer
91  
92 =item *
93  
94 Float
95  
96 =item *
97  
98 String
99  
100 =item *
101  
102 Boolean
103  
104 =item *
105  
106 Timestamp
107  
108 =item *
109  
110 L<Net::Async::Spotify::Object>
111  
112 =item *
113  
114 Array[L<Net::Async::Spotify::Object>]
115  
116 =back
117  
118 =cut
119  
120 0 ▶sub apply_type {
Subroutine apply_type: not called CC=26 SCAR=65.5
Pod documented
121 0 my ($self, $type, $value, $field) = @_;
122 # TODO: Implement better Type casting, maybe Inline C ?
123 0 ▶ if ( $type eq 'Integer' ) {
Branch: 7 branches
BranchTrueFalseTotal
if ($type eq 'Integer') { } 0 0 0
elsif ($type eq 'Float') { } 0 0 0
elsif ($type eq 'String') { } 0 0 0
elsif ($type eq 'Boolean') { } 0 0 0
elsif ($type eq 'Timestamp') { } 0 0 0
elsif (my($obj) = $type =~ /^(?!Array)(.*?)(Object)/) { } 0 0 0
elsif (() = $type =~ /^Array\[(.*?)(Object)?\]/m) { } 0 0 0
124 0 ▶ $value = 0 unless $value;
Branch: 1 branch
BranchTrueFalseTotal
unless $value 0 0 0
125 0 ▶ return $value if $value =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /^\d+$/ 0 0 0
126 } elsif ( $type eq 'Float' ) {
127 0 ▶ $value = "0.0" unless $value;
Branch: 1 branch
BranchTrueFalseTotal
unless $value 0 0 0
128 0 ▶ return $value if $value =~ /[+-]?[0-9]*[.]?[0-9]+/;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /[+-]?[0-9]*[.]?[0-9]+/ 0 0 0
129 } elsif ( $type eq 'String' ) {
130 0 ▶ return $value unless ref $value;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value 0 0 0
131 } elsif ( $type eq 'Boolean' ) {
132 0 ▶ return $value if ref $value eq 'JSON::PP::Boolean';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "JSON::PP::Boolean" 0 0 0
133 } elsif ( $type eq 'Timestamp' ) {
134 try {
135 0 return Time::Moment->from_string($value);
136 0 } catch ($e) {
137 0 $log->warnf('Unrecognized Timestamp string: %s | Error: %s | %s | %s', $value, $e, ref($self), $field);
138 0 return {corrupt_timestamp => $value};
139 }
140 } elsif ( my ( $obj ) = $type =~ /^(?!Array)(.*?)(Object)/ ) {
141 0 my $module = "Net::Async::Spotify::Object::$obj";
142 try {
143 0 require_module($module);
144 0 } catch($e) {
145 0 $log->errorf('Unrecognized Object type %s | Error: %s | %s | %s', $module, $e, ref($self), $field);
146 }
147 # When $value is empty.
148 # Not sure if I want to pass it as empty hashref
149 # or empty blessed object
150 # will revisit later.
151 0 ▶ $value = {} unless $value;
Branch: 1 branch
BranchTrueFalseTotal
unless $value 0 0 0
152 0 return $module->new($value->%*);
153 } elsif ( my ($array_type, $object_array) = $type =~ /^Array\[(.*?)(Object)?\]/m ) {
154 0 my $array = [];
155 0 ▶ if ( $object_array ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($object_array) { } 0 0 0
elsif ($value) { } 0 0 0
156 0 ▶ my $module = $array_type ? "Net::Async::Spotify::Object::$array_type" : "Net::Async::Spotify::Object::General";
Branch: 1 branch
BranchTrueFalseTotal
$array_type ? : 0 0 0
157 try {
158 # TODO: check if this step can be removed.
159 0 require_module($module);
160 0 } catch($e) {
161 0 $log->errorf('Unrecognized Object type %s | Error: %s | %s | %s', $module, $e, ref($self), $field);
162 }
163 0 ▶ if ( $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value) 0 0 0
164 # Can be attached to pagination object.
165 # TODO: maybe unify implementation with Object.pm
166 0 ▶ if ( ref $value eq 'HASH' and exists $value->{items} and exists $value->{limit} and exists $value->{total} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq 'HASH' and exists $value->{'items'} and exists $value->{'limit'} and exists $value->{'total'}) { } 0 0 0
Condition0%0/9
ref $value eq "HASH" and exists $value->{"items"} and exists $value->{"limit"} and exists $value->{"total"}
!ll&&!rl&&r
000
ref $value eq "HASH" and exists $value->{"items"} and exists $value->{"limit"}
!ll&&!rl&&r
000
ref $value eq "HASH" and exists $value->{"items"}
!ll&&!rl&&r
000
MC/DC0%0/4
ref $value eq "HASH" and exists $value->{"items"} and exists $value->{"limit"} and exists $value->{"total"}
ref $value eq "HASH" exists $value->{"items"} exists $value->{"limit"} exists $value->{"total"}
Truth table0%0/5
A and B and C and D
A= ref $value eq "HASH"B= exists $value->{"items"}C= exists $value->{"limit"}D= exists $value->{"total"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
167 0 push @$array, $module->new($_->%*) for $value->{items}->@*;
168 0 delete $value->{items};
169 0 my $paging = Net::Async::Spotify::Object::Paging->new($value->%*);
170 0 $paging->{items} = $array;
171 0 return $paging;
172 } else {
173 0 push @$array, $module->new($_->%*) for @$value;
174 }
175 }
176 } elsif ( $value ) {
177 0 push @$array, $_ for @$value;
178 }
179 0 return $array;
180 }
181 0 $log->debugf('Unmatching Type. %s is not %s | %s | %s', $value, $type, ref($self), $field);
182 0 return $value;
183 }
184  
185 =head2 extra_fields
186  
187 Special field, where it holds any undefined keys passed when creating.
188  
189 =cut
190  
191 0 ▶sub extra_fields { shift->{extra_fields} }
Subroutine extra_fields: not called CC=1 SCAR=6.9
Pod documented
192  
193 =head2 to_hash
194  
195 Converts object into a hash rather than blessed object with methods.
196  
197 =cut
198  
199 0 ▶sub to_hash {
Subroutine to_hash: not called CC=10 SCAR=47.0
Pod documented
200 0 my $self = shift;
201  
202 0 my %hash = ();
203 0 for my $attr (sort keys $self->fields->%*) {
204 0 ▶ if ( $self->$attr ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->$attr) 0 0 0
205 0 ▶ if (blessed $self->$attr and $self->$attr->can('to_hash') ) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($self->$attr) and $self->$attr->can('to_hash')) { } 0 0 0
elsif (ref $self->$attr eq 'ARRAY') { } 0 0 0
Condition0%0/3
builtin::blessed($self->$attr) and $self->$attr->can("to_hash")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($self->$attr) and $self->$attr->can("to_hash")
builtin::blessed($self->$attr) $self->$attr->can("to_hash")
Truth table0%0/3
A and B
A= builtin::blessed($self->$attr)B= $self->$attr->can("to_hash")
ABresult
0X0
100
111
206 0 $hash{$attr} = $self->$attr->to_hash;
207 } elsif (ref $self->$attr eq 'ARRAY' ) {
208 0 my @array = ();
209 0 for my $e ( $self->$attr->@* ) {
210 0 ▶ if (blessed $e and $e->can('to_hash') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($e) and $e->can('to_hash')) { } 0 0 0
Condition0%0/3
builtin::blessed($e) and $e->can("to_hash")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($e) and $e->can("to_hash")
builtin::blessed($e) $e->can("to_hash")
Truth table0%0/3
A and B
A= builtin::blessed($e)B= $e->can("to_hash")
ABresult
0X0
100
111
211 0 push @array, $e->to_hash;
212 } else {
213 0 push @array, $e;
214 }
215 }
216 0 $hash{$attr} = \@array;
217 } else {
218 0 $hash{$attr} = $self->$attr;
219 }
220 }
221 }
222 0 ▶ $hash{extra_fields} = $self->extra_fields if defined $self->extra_fields;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->extra_fields 0 0 0
223 0 return \%hash;
224 }
225  
226 =head2 to_json
227  
228 return object as JSON encoded string
229  
230 =cut
231  
232 0 ▶sub to_json {
Subroutine to_json: not called CC=1 SCAR=6.9
Pod documented
233 0 my $self = shift;
234  
235 0 return encode_json_utf8($self->to_hash);
236  
237 }
238  
239 =head2 to_human
240  
241 return object as readable string
242  
243 =cut
244  
245 0 ▶sub to_human {
Subroutine to_human: not called CC=1 SCAR=6.9
Pod documented
246 0 my $self = shift;
247  
248 0 return hash_to_string($self->to_hash);
249 }
250  
251 128 1;
↑ Summary lib/Net/Async/Spotify/API/Base.pm »