| 1 | package Net::Async::Spotify::Util; | ||||||||||||||||||||||
| 2 | |||||||||||||||||||||||
| 3 | 129 | ▶ | use strict; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 4 | 129 | ▶ | use warnings; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 5 | |||||||||||||||||||||||
| 6 | 129 | our $VERSION = '0.002'; # VERSION | |||||||||||||||||||||
| 7 | 129 | our $AUTHORITY = 'cpan:VNEALV'; # AUTHORITY | |||||||||||||||||||||
| 8 | |||||||||||||||||||||||
| 9 | 129 | ▶ | use utf8; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 10 | |||||||||||||||||||||||
| 11 | 129 | ▶ | use Log::Any qw($log); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 12 | 129 | ▶ | use Syntax::Keyword::Try; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 13 | 129 | ▶ | use List::Util qw(first); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 14 | 129 | ▶ | use Data::Dumper; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 15 | 129 | ▶ | use Data::Dumper::AutoEncode; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 16 | 129 | ▶ | use Perl::Tidy; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 17 | |||||||||||||||||||||||
| 18 | 129 | ▶ | use Exporter 'import'; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 19 | 129 | our @EXPORT_OK = qw(response_object_map hash_to_string); | |||||||||||||||||||||
| 20 | |||||||||||||||||||||||
| 21 | =encoding utf8 | ||||||||||||||||||||||
| 22 | |||||||||||||||||||||||
| 23 | =head1 NAME | ||||||||||||||||||||||
| 24 | |||||||||||||||||||||||
| 25 | Net::Async::Spotify::Util - Helper class for some common functions. | ||||||||||||||||||||||
| 26 | |||||||||||||||||||||||
| 27 | =head1 DESCRIPTION | ||||||||||||||||||||||
| 28 | |||||||||||||||||||||||
| 29 | Helper class that will have some functions that are general and can be used across library. | ||||||||||||||||||||||
| 30 | |||||||||||||||||||||||
| 31 | =cut | ||||||||||||||||||||||
| 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 it will check C<@$expected> passed by | ||||||||||||||||||||||
| 54 | trying to find for the exact Object name, if not found it will get the first object that includes the passed name | ||||||||||||||||||||||
| 55 | as part of it's own name. | ||||||||||||||||||||||
| 56 | It will return the Object class name if found, and `undef` when it can't find a suitable object. | ||||||||||||||||||||||
| 57 | |||||||||||||||||||||||
| 58 | =cut | ||||||||||||||||||||||
| 59 | |||||||||||||||||||||||
| 60 | 0 | ▶ | sub response_object_map { | ||||||||||||||||||||
|
Subroutine
response_object_map: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||
| 61 | 0 | my ($available_types, $expected) = @_; | |||||||||||||||||||||
| 62 | 0 | my @available_types = $available_types->@*; | |||||||||||||||||||||
| 63 | |||||||||||||||||||||||
| 64 | 0 | ▶ | return undef if scalar(@$expected) == 0; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 65 | |||||||||||||||||||||||
| 66 | 0 | my $possible_name; | |||||||||||||||||||||
| 67 | 0 | my $possible_t = $expected->[0]; | |||||||||||||||||||||
| 68 | #my $type = ($possible_t =~ /^[^\s]+/); | ||||||||||||||||||||||
| 69 | 0 | my @type = split ' ', $possible_t; | |||||||||||||||||||||
| 70 | # Search for exact first, then check composite ones. | ||||||||||||||||||||||
| 71 | 0 | ▶ | $possible_name = first { /^$type[0]$/i } @available_types; | ||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||
| 72 | 0 | ▶ | $possible_name = first { /$type[0]/gi } @available_types unless defined $possible_name; | ||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||
| 73 | |||||||||||||||||||||||
| 74 | 0 | ▶ | return undef unless defined $possible_name; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 75 | 0 | return join '::', 'Net::Async::Spotify::Object', $possible_name; | |||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||
| 77 | |||||||||||||||||||||||
| 78 | =head2 hash_to_string | ||||||||||||||||||||||
| 79 | |||||||||||||||||||||||
| 80 | Not so proper way to stringify a Hash. | ||||||||||||||||||||||
| 81 | Used to make Hash printable and readable to human. | ||||||||||||||||||||||
| 82 | |||||||||||||||||||||||
| 83 | =cut | ||||||||||||||||||||||
| 84 | |||||||||||||||||||||||
| 85 | 0 | ▶ | sub hash_to_string { | ||||||||||||||||||||
|
Subroutine
hash_to_string: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||
| 86 | 0 | local $Data::Dumper::Terse = 1; | |||||||||||||||||||||
| 87 | 0 | local $Data::Dumper::Indent = 0; | |||||||||||||||||||||
| 88 | 0 | ▶ | local $Data::Dumper::Sortkeys = sub { | ||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||
| 89 | [sort { | ||||||||||||||||||||||
| 90 | 0 | ▶ | if ( $b eq 'type' ) {1} | ||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||
| 91 | 0 | elsif ( $a eq 'type' ) {-1} | |||||||||||||||||||||
| 92 | 0 | elsif ( $b eq 'position' ) {1} | |||||||||||||||||||||
| 93 | 0 | elsif ( $a eq 'position' ) {-1} | |||||||||||||||||||||
| 94 | 0 | else { $a cmp $b } | |||||||||||||||||||||
| 95 | 0 | } keys %{ $_[0] }]; | |||||||||||||||||||||
| 96 | 0 | }; | |||||||||||||||||||||
| 97 | 0 | my $source = eDumper(@_); | |||||||||||||||||||||
| 98 | 0 | my $result; | |||||||||||||||||||||
| 99 | 0 | Perl::Tidy::perltidy( | |||||||||||||||||||||
| 100 | source => \$source, | ||||||||||||||||||||||
| 101 | destination => \$result, | ||||||||||||||||||||||
| 102 | argv => [qw(-pbp -nst)] | ||||||||||||||||||||||
| 103 | ); | ||||||||||||||||||||||
| 104 | 0 | return $result; | |||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||
| 106 | |||||||||||||||||||||||
| 107 | 129 | 1; | |||||||||||||||||||||