lib/Net/Async/Spotify/Object.pm

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