lib/Net/Async/Spotify/Util.pm

filter: Statementstmt 55.5% 20 / 36 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 83.3% 5 / 6 Podpod 100% 1 / 1 totaltotal 46.4% 26 / 56 SCAR 25.5
CC
6
Cov
43%
response_object_map
42.0
BEGIN
1.0
BEGIN
1.0
CRAP 12.8
« lib/Net/Async/Spotify/API.pm ↑ Summary lib/Net/Async/Spotify/Object.pm »
1 package Net::Async::Spotify::Util;
2  
3 31 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 31 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 31 our $VERSION = '0.001'; # VERSION
7 31 our $AUTHORITY = 'cpan:VNEALV'; # AUTHORITY
8  
9 31 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 31 ▶use Syntax::Keyword::Try;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 31 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 31 our @EXPORT_OK = qw(response_object_map);
14  
15 =encoding utf8
16  
17 =head1 NAME
18  
19 Net::Async::Spotify::Util - Helper class for some common functions.
20  
21 =head1 DESCRIPTION
22  
23 Helper class that will have some functions that are general and can be used across library.
24  
25 =cut
26  
27 # TODO: Make it our, and allow permenant type set.
28 31 my %uri_responses = (
29 'https://api.spotify.com/v1/artists/{id}/albums' => 'Album',
30 'https://api.spotify.com/v1/me/player/devices' => 'Devices',
31 );
32  
33 =head1 METHODS
34  
35 =head2 response_object_map
36  
37 Due to the unstructured response details in Spotify documentation page in regards they type of response for every request.
38 This method will act as a reliable mapping for Spotify API responses to their desired Objects type.
39 Takes:
40  
41 =over 4
42  
43 =item available_types
44  
45 which consists of the availabe Object types that are loaded by Caller.
46  
47 =item response_hash
48  
49 which consists of possible `response_objs` types and `uri` that was called for this response.
50  
51 =back
52  
53 It uses smart matching mechanism; meaning that first it will check if that Spotify uri has a specific retun type
54 set in C<%uri_responses> hash, if nothing is set for the uri. It will check C<@$possible_types> passed by
55 trying to find for the exact Object name, if not found it will get the first object that includes the passed name
56 as part of it's own name.
57 It will return the Object class name if found, and `undef` when it can't find a suitable object.
58  
59 =cut
60  
61 sub response_object_map {
62 0 ▶ my ($available_types, $res_hash) = @_;
Subroutine response_object_map: not called CC=6 SCAR=37.4
Pod documented
63  
64 0 my $possible_types = $res_hash->{response_objs};
65 0 my $for_uri = $res_hash->{uri};
66 0 $log->tracef('Got response object to map. Possible_types: %s | for_uri: %s', $possible_types, $for_uri);
67 0 ▶ return undef if scalar(@$possible_types) == 0 and !exists $uri_responses{$for_uri};
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$possible_types == 0 and not exists $uri_responses{$for_uri} 0 0 0
Condition0%0/3
scalar @$possible_types == 0 and not exists $uri_responses{$for_uri}
!ll&&!rl&&r
000
MC/DC0%0/2
scalar @$possible_types == 0 and not exists $uri_responses{$for_uri}
scalar @$possible_types == 0 not exists $uri_responses{$for_uri}
Truth table0%0/3
A and not(B)
A= scalar @$possible_types == 0B= not exists $uri_responses{$for_uri}
ABresult
0X0
100
111
68  
69 0 my $possible_name;
70 0 ▶ unless ( exists $uri_responses{$for_uri} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $uri_responses{$for_uri}) { } 0 0 0
71 0 my $possible_t = $possible_types->[0];
72 #my $type = ($possible_t =~ /^[^\s]+/);
73 0 my @type = split ' ', $possible_t;
74 # Search for exact first, then check composite ones.
75 0 ($possible_name) = grep { /^$type[0]$/i } @$available_types;
76 0 ▶ ($possible_name) = grep { /$type[0]/gi } @$available_types unless defined $possible_name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $possible_name 0 0 0
77 } else {
78 0 $possible_name = $uri_responses{$for_uri};
79 }
80  
81 0 ▶ return undef unless defined $possible_name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $possible_name 0 0 0
82 0 return join '::', 'Net::Async::Spotify::Object', $possible_name;
83 }
84  
85 31 1;
« lib/Net/Async/Spotify/API.pm ↑ Summary lib/Net/Async/Spotify/Object.pm »