File Coverage

blib/lib/WebService/MusixMatch.pm
Criterion Covered Total %
statement 30 43 69.7
branch 0 2 0.0
condition n/a
subroutine 10 12 83.3
pod 0 1 0.0
total 40 58 68.9


line stmt bran cond sub pod time code
1             package WebService::MusixMatch;
2 2     2   17234 use JSON::XS;
  2         10284  
  2         127  
3 2     2   815 use Cache::LRU;
  2         1081  
  2         49  
4 2     2   1000 use Net::DNS::Lite;
  2         26251  
  2         119  
5 2     2   1065 use Furl;
  2         33208  
  2         62  
6 2     2   1108 use URI;
  2         7165  
  2         62  
7 2     2   1044 use URI::QueryParam;
  2         1065  
  2         46  
8 2     2   11 use Carp;
  2         2  
  2         129  
9 2     2   1001 use Moo;
  2         53329  
  2         13  
10 2     2   3497 use namespace::clean;
  2         19450  
  2         13  
11             our $VERSION = "0.06";
12              
13              
14             $Net::DNS::Lite::CACHE = Cache::LRU->new( size => 512 );
15              
16             has 'api_key' => (
17             is => 'rw',
18             isa => sub { $_[0] },
19             required => 1,
20             default => sub { $ENV{MUSIXMATCH_API_KEY} },
21             );
22              
23             has 'http' => (
24             is => 'rw',
25             required => 1,
26             default => sub {
27             my $http = Furl::HTTP->new(
28             inet_aton => \&Net::DNS::Lite::inet_aton,
29             agent => 'WebService::MusixMatch/' . $VERSION,
30             headers => [ 'Accept-Encoding' => 'gzip',],
31             );
32             return $http;
33             },
34             );
35              
36              
37             my @methods = (
38             'chart.artists.get',
39             'chart.tracks.get',
40             'track.search',
41             'track.get',
42             'track.subtitle.get',
43             'track.lyrics.get',
44             'track.snippet.get',
45             'track.lyrics.post',
46             'track.lyrics.feedback.post',
47             'matcher.lyrics.get',
48             'matcher.track.get',
49             'matcher.subtitle.get',
50             'artist.get',
51             'artist.search',
52             'artist.albums.get',
53             'artist.related.get',
54             'album.get',
55             'album.tracks.get',
56             'tracking.url.get',
57             'catalogue.dump.get',
58             );
59              
60              
61             for my $method (@methods) {
62             my $code = sub {
63 0     0     my ($self, %query_param) = @_;
64 0           return $self->request($method, \%query_param);
65             };
66 2     2   828 no strict 'refs';
  2         4  
  2         439  
67             my $method_name = $method;
68             $method_name =~ s|\.|_|g;
69             *{$method_name} = $code;
70             }
71              
72              
73             sub request {
74 0     0 0   my ( $self, $path, $query_param ) = @_;
75              
76 0           my $query = URI->new;
77 0           $query->query_param( 'apikey', $self->api_key );
78 0           $query->query_param( 'format', 'json' );
79 0           map { $query->query_param( $_, $query_param->{$_} ) } keys %$query_param;
  0            
80              
81 0           my ($minor_version, $status_code, $message, $headers, $content) =
82             $self->http->request(
83             scheme => 'http',
84             host => 'api.musixmatch.com',
85             path_query => "ws/1.1/$path$query",
86             method => 'GET',
87             );
88              
89 0           my $data = decode_json( $content );
90 0 0         if ( $data->{message}{header}{status_code} != 200 ) {
91 0           confess $data->{message}{header}{status_code};
92             } else {
93 0           return $data;
94             }
95             }
96              
97              
98             1;
99             __END__