lib/Net/Async/Spotify/Object.pm

filter: Statementstmt 71.1% 37 / 52 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 91.6% 11 / 12 Podpod 0.0% 0 / 1 totaltotal 61.5% 48 / 78 SCAR 25.0
CC
8
Cov
60%
new
42.0
BEGIN
3.0
BEGIN
1.0
CRAP 12.2
« lib/Net/Async/Spotify/Util.pm ↑ Summary lib/Net/Async/Spotify/Scope.pm »
1 package Net::Async::Spotify::Object;
2  
3 30 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 30 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 30 our $VERSION = '0.001'; # VERSION
7 30 our $AUTHORITY = 'cpan:VNEALV'; # AUTHORITY
8  
9 30 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 30 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 30 ▶use Syntax::Keyword::Try;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 30 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 30 ▶use Module::Path qw(module_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 30 ▶use Module::Runtime qw(require_module);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 30 ▶use Net::Async::Spotify::Util qw(response_object_map);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 30 ▶use Net::Async::Spotify::Object::General;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 =encoding utf8
19  
20 =head1 NAME
21  
22 Net::Async::Spotify::Object - Common Wrapper package for Spotify response Objects
23  
24 =head1 SYNOPSIS
25  
26 To be used internally, and it will be used as a response to L<Net::Async::Spotify::API>
27 However this is how it's actually being used:
28  
29 use Net::Async::Spotify::Object;
30  
31 my $sp_json_decoded_res = {danceability => 0.735,,...}; # Audio Features response.
32 my $obj = Net::Async::Spotify::Object->new(
33 $sp_json_decoded_res,
34 {
35 response_objs => ['features'],
36 uri => 'https://api.spotify.com/v1/audio-features/{id}',
37 },
38 );
39 ref $obj; # => Net::Async::Spotify::Object::AudioFeatures
40  
41 =head1 DESCRIPTION
42  
43 Common wrapper for Spotify response Objects. To be used to dynamically be able to create
44 resonse objects.
45 It does so by so by figuring out needed Object type using L<Net::Async::Spotify::Util::response_object_map>
46 and returning a new instance of that L<perlsyn/"Objects">. When it can't determine an exact object type it will
47 return an instance of L<Net::Async::Spotify::Object::General>.
48 Note that some responses are paginated, so might contain L<Net::Async::Spotify::Object::Paging>.
49 and if it's an erroneous response it will be L<Net::Async::Spotify::Object::Error>.
50  
51 =head1 PARAMETERS
52  
53 We would need to pass what we think this Object might be along with the content.
54  
55 =over 4
56  
57 =item possible_types
58  
59 A list of possible types for this object we want to create.
60  
61 =item data
62  
63 the content that we want to create the object for.
64  
65 =back
66  
67 =cut
68  
69 30 my $available_types;
70 BEGIN {
71 # Include all Spotify Object classes
72 30 ▶ my $current_path = path(module_path(__PACKAGE__) =~ s/\.pm/\//r );
Subroutine BEGIN: called CC=3 SCAR=11.0
73 30 push @$available_types, $_->basename =~ s/\.pm//r for $current_path->child('Generated')->children(qr/.pm$/);
74 30 require_module(join '::', __PACKAGE__, $_) for @$available_types;
75 }
76  
77 sub new {
78 0 ▶ my ( $obj, $data, $res_hash ) = @_;
Subroutine new: not called CC=6 SCAR=37.4
Pod undocumented
79  
80 0 my $class = response_object_map($available_types, $res_hash);
81 0 $log->debugf('Response object mapping; params: %s | Class selected: %s', $res_hash, $class);
82 # return generic when not found.
83 0 ▶ return Net::Async::Spotify::Object::General->new($data->%*) unless defined $class;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class 0 0 0
84  
85 0 my $content = [];
86 try {
87 # Check if pagination or Error object.
88 0 ▶ if ( exists $data->{items} and exists $data->{limit} and exists $data->{total} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{'items'} and exists $data->{'limit'} and exists $data->{'total'}) { } 0 0 0
Condition0%0/6
exists $data->{"items"} and exists $data->{"limit"} and exists $data->{"total"}
!ll&&!rl&&r
000
exists $data->{"items"} and exists $data->{"limit"}
!ll&&!rl&&r
000
MC/DC0%0/3
exists $data->{"items"} and exists $data->{"limit"} and exists $data->{"total"}
exists $data->{"items"} exists $data->{"limit"} exists $data->{"total"}
Truth table0%0/4
A and B and C
A= exists $data->{"items"}B= exists $data->{"limit"}C= exists $data->{"total"}
ABCresult
0XX0
10X0
1100
1111
89 0 push @$content, $class->new($_->%*) for $data->{items}->@*;
90 0 delete $data->{items};
91 0 my $page = Net::Async::Spotify::Object::Paging->new($data->%*);
92 0 $page->{items} = $content;
93 0 return $page;
94 # TODO: Add error checking.
95 } else {
96 0 return $class->new($data->%*);
97 }
98 0 } catch ($e) {
99 0 $log->warnf('Could not create Spotify Object %s | error: %s | res_hash: %s ', $class, $e, $res_hash);
100 }
101 0 return Net::Async::Spotify::Object::General->new($data->%*);
102 }
103  
104 30 1;
105  
106 =head1 Objects
107  
108 Here is a list of the current available Spotify Object types.
109 Found in link here L<https://developer.spotify.com/documentation/web-api/reference/#objects-index>
110  
111 =over 4
112  
113 =item *
114  
115 L<Net::Async::Spotify::Object::Album>
116  
117 =item *
118  
119 L<Net::Async::Spotify::Object::AlbumRestriction>
120  
121 =item *
122  
123 L<Net::Async::Spotify::Object::Artist>
124  
125 =item *
126  
127 L<Net::Async::Spotify::Object::AudioFeatures>
128  
129 =item *
130  
131 L<Net::Async::Spotify::Object::Base>
132  
133 =item *
134  
135 L<Net::Async::Spotify::Object::Category>
136  
137 =item *
138  
139 L<Net::Async::Spotify::Object::Context>
140  
141 =item *
142  
143 L<Net::Async::Spotify::Object::Copyright>
144  
145 =item *
146  
147 L<Net::Async::Spotify::Object::CurrentlyPlaying>
148  
149 =item *
150  
151 L<Net::Async::Spotify::Object::CurrentlyPlayingContext>
152  
153 =item *
154  
155 L<Net::Async::Spotify::Object::Cursor>
156  
157 =item *
158  
159 L<Net::Async::Spotify::Object::CursorPaging>
160  
161 =item *
162  
163 L<Net::Async::Spotify::Object::Device>
164  
165 =item *
166  
167 L<Net::Async::Spotify::Object::Devices>
168  
169 =item *
170  
171 L<Net::Async::Spotify::Object::Disallows>
172  
173 =item *
174  
175 L<Net::Async::Spotify::Object::Episode>
176  
177 =item *
178  
179 L<Net::Async::Spotify::Object::EpisodeRestriction>
180  
181 =item *
182  
183 L<Net::Async::Spotify::Object::Error>
184  
185 =item *
186  
187 L<Net::Async::Spotify::Object::ExplicitContentSettings>
188  
189 =item *
190  
191 L<Net::Async::Spotify::Object::ExternalId>
192  
193 =item *
194  
195 L<Net::Async::Spotify::Object::ExternalUrl>
196  
197 =item *
198  
199 L<Net::Async::Spotify::Object::Followers>
200  
201 =item *
202  
203 L<Net::Async::Spotify::Object::General>
204  
205 =item *
206  
207 L<Net::Async::Spotify::Object::Image>
208  
209 =item *
210  
211 L<Net::Async::Spotify::Object::LinkedTrack>
212  
213 =item *
214  
215 L<Net::Async::Spotify::Object::Paging>
216  
217 =item *
218  
219 L<Net::Async::Spotify::Object::PlayHistory>
220  
221 =item *
222  
223 L<Net::Async::Spotify::Object::PlayerError>
224  
225 =item *
226  
227 L<Net::Async::Spotify::Object::Playlist>
228  
229 =item *
230  
231 L<Net::Async::Spotify::Object::PlaylistTrack>
232  
233 =item *
234  
235 L<Net::Async::Spotify::Object::PlaylistTracksRef>
236  
237 =item *
238  
239 L<Net::Async::Spotify::Object::PrivateUser>
240  
241 =item *
242  
243 L<Net::Async::Spotify::Object::PublicUser>
244  
245 =item *
246  
247 L<Net::Async::Spotify::Object::RecommendationSeed>
248  
249 =item *
250  
251 L<Net::Async::Spotify::Object::Recommendations>
252  
253 =item *
254  
255 L<Net::Async::Spotify::Object::ResumePoint>
256  
257 =item *
258  
259 L<Net::Async::Spotify::Object::SavedAlbum>
260  
261 =item *
262  
263 L<Net::Async::Spotify::Object::SavedEpisode>
264  
265 =item *
266  
267 L<Net::Async::Spotify::Object::SavedShow>
268  
269 =item *
270  
271 L<Net::Async::Spotify::Object::SavedTrack>
272  
273 =item *
274  
275 L<Net::Async::Spotify::Object::Show>
276  
277 =item *
278  
279 L<Net::Async::Spotify::Object::SimplifiedAlbum>
280  
281 =item *
282  
283 L<Net::Async::Spotify::Object::SimplifiedArtist>
284  
285 =item *
286  
287 L<Net::Async::Spotify::Object::SimplifiedEpisode>
288  
289 =item *
290  
291 L<Net::Async::Spotify::Object::SimplifiedPlaylist>
292  
293 =item *
294  
295 L<Net::Async::Spotify::Object::SimplifiedShow>
296  
297 =item *
298  
299 L<Net::Async::Spotify::Object::SimplifiedTrack>
300  
301 =item *
302  
303 L<Net::Async::Spotify::Object::Track>
304  
305 =item *
306  
307 L<Net::Async::Spotify::Object::TrackRestriction>
308  
309 =item *
310  
311 L<Net::Async::Spotify::Object::TuneableTrack>
312  
313 =back
314  
315 =cut
« lib/Net/Async/Spotify/Util.pm ↑ Summary lib/Net/Async/Spotify/Scope.pm »