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

filter: Statementstmt 28.9% 24 / 83 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 58.3% 7 / 12 Podpod 50.0% 2 / 4 totaltotal 21.1% 33 / 156 SCAR 63.4
CC
31
Cov
18%
apply_type
702.0
generate
22.3
__ANON__
6.0
CRAP 567.7
↑ Summary lib/Net/Async/Spotify/API/Base.pm »
1 package Net::Async::Spotify::Object::Base;
2  
3 127 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 127 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 127 our $VERSION = '0.001'; # VERSION
7 127 our $AUTHORITY = 'cpan:VNEALV'; # AUTHORITY
8  
9 127 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 127 ▶use Syntax::Keyword::Try;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 127 ▶use Module::Runtime qw(require_module);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 127 ▶use Time::Moment;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 =head1 NAME
15  
16 Net::Async::Spotify::Object::Base - Base Package for Spotify Object Classes.
17  
18 =head1 DESCRIPTION
19  
20 Base class to be used by all Autogenerated modules of Spotify Objects
21 It does so by creating an accessor that goes by the name of each field in the object.
22 It also applies type casting to passed values, making sure all types are matching.
23 If an undefined field passed when creating, it will be populated in L</extra_fields>
24 Defined fields are specific to each L<Net::Async::Spotify::Object> and can be viewed in class itself.
25  
26 =head1 METHODS
27  
28 =cut
29  
30 sub new {
31 0 ▶ my ( $class, $fields, %args ) = @_;
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
32 0 my $self = bless {}, $class;
33 0 $self->generate($fields, %args);
34 0 return $self;
35 }
36  
37 sub generate {
38 0 ▶ my ( $self, $fields, %args ) = @_;
Subroutine generate: not called CC=5 SCAR=31.0
Pod undocumented
39  
40 0 for my $field (keys %$fields) {
41 0 ▶ next if $field eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $field eq "" 0 0 0
42 0 my $accessor = join '::', ref($self), $field;
43 {
44 # here we need to allow hard references
45 # so we can add accessors to calling class.
46 127 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.2
47  
48 # Only add accessor when it does not exist
49 # also do not repeat added ones.
50 0 *{$accessor} = sub {
51 0 ▶ my ( $self, $value ) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
52  
53 0 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 0 0
54 0 $value = $self->apply_type($fields->{$field}, $value);
55 0 $self->{$field} = $value;
56 }
57 0 return $self->{$field};
58 }
59 0 ▶ unless defined &{$accessor};
Branch: 1 branch
BranchTrueFalseTotal
unless defined &{$accessor;} 0 0 0
60 }
61 0 $self->$field(delete $args{$field});
62 }
63 # Fields that are added but not present in documentation and current Object class
64 0 for my $k (keys %args) {
65 0 $log->debugf('Unknown field %s passed | Object: %s', $k, ref $self);
66 0 $self->{extra_fields}{$k} = $args{$k};
67 }
68  
69 }
70  
71 =head2 apply_type
72  
73 an internal function attached to every field accessor.
74 Casting the value of each field depending on it's type.
75 I believe this may not be the optimum way to do it.
76 However for the time being I think it works great.
77 Here are the available types that it checks for:
78  
79 =over 4
80  
81 =item *
82  
83 Integer
84  
85 =item *
86  
87 Float
88  
89 =item *
90  
91 String
92  
93 =item *
94  
95 Boolean
96  
97 =item *
98  
99 Timestamp
100  
101 =item *
102  
103 L<Net::Async::Spotify::Object>
104  
105 =item *
106  
107 Array[L<Net::Async::Spotify::Object>]
108  
109 =back
110  
111 =cut
112  
113 sub apply_type {
114 0 ▶ my ($self, $type, $value) = @_;
Subroutine apply_type: not called CC=26 SCAR=65.5
Pod documented
115 0 $log->tracef('applying type %s', $type);
116 # TODO: Implement better Type casting, maybe Inline C ?
117 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
118 0 ▶ $value = 0 unless $value;
Branch: 1 branch
BranchTrueFalseTotal
unless $value 0 0 0
119 0 ▶ return $value if $value =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /^\d+$/ 0 0 0
120 } elsif ( $type eq 'Float' ) {
121 0 ▶ $value = "0.0" unless $value;
Branch: 1 branch
BranchTrueFalseTotal
unless $value 0 0 0
122 0 ▶ return $value if $value =~ /[+-]?[0-9]*[.]?[0-9]+/;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /[+-]?[0-9]*[.]?[0-9]+/ 0 0 0
123 } elsif ( $type eq 'String' ) {
124 0 ▶ return $value unless ref $value;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value 0 0 0
125 } elsif ( $type eq 'Boolean' ) {
126 0 ▶ return $value if ref $value eq 'JSON::PP::Boolean';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "JSON::PP::Boolean" 0 0 0
127 } elsif ( $type eq 'Timestamp' ) {
128 try {
129 0 return Time::Moment->from_string($value);
130 0 } catch ($e) {
131 0 $log->warnf('Unrecognized Timestamp string: %s | Error: %s', $value, $e);
132 0 return {corrupt_timestamp => $value};
133 }
134 } elsif ( my ( $obj ) = $type =~ /^(?!Array)(.*?)(Object)/ ) {
135 0 my $module = "Net::Async::Spotify::Object::$obj";
136 try {
137 0 require_module($module);
138 0 } catch($e) {
139 0 $log->errorf('Unrecognized Object type %s | Error: %s', $module, $e);
140 }
141 # When $value is empty.
142 # Not sure if I want to pass it as empty hashref
143 # or empty blessed object
144 # will revisit later.
145 0 ▶ $value = {} unless $value;
Branch: 1 branch
BranchTrueFalseTotal
unless $value 0 0 0
146 0 return $module->new($value->%*);
147 } elsif ( my ($array_type, $object_array) = $type =~ /^Array\[(.*?)(Object)?\]/m ) {
148 0 my $array = [];
149 0 ▶ if ( $object_array ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($object_array) { } 0 0 0
elsif ($value) { } 0 0 0
150 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
151 try {
152 # TODO: check if this step can be removed.
153 0 require_module($module);
154 0 } catch($e) {
155 0 $log->errorf('Unrecognized Object type %s | Error: %s', $module, $e);
156 }
157 0 ▶ if ( $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value) 0 0 0
158 # Can be attached to pagination object.
159 # TODO: maybe unify implementation with Object.pm
160 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
161 0 push @$array, $module->new($_->%*) for $value->{items}->@*;
162 0 delete $value->{items};
163 0 my $paging = Net::Async::Spotify::Object::Paging->new($value->%*);
164 0 $paging->{items} = $array;
165 0 return $paging;
166 } else {
167 0 push @$array, $module->new($_->%*) for @$value;
168 }
169 }
170 } elsif ( $value ) {
171 0 push @$array, $_ for @$value;
172 }
173 0 return $array;
174 }
175 0 $log->errorf('Unmatching Type. %s is not %s | %s', $value, $type, ref $self);
176 0 return $value;
177 }
178  
179 =head2 extra_fields
180  
181 Special field, where it holds any undefined keys passed when creating.
182  
183 =cut
184  
185 0 ▶sub extra_fields { shift->{extra_fields} }
Subroutine extra_fields: not called CC=1 SCAR=6.9
Pod documented
186  
187 127 1;
↑ Summary lib/Net/Async/Spotify/API/Base.pm »