File Coverage

blib/lib/WebService/MusixMatch.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             package WebService::MusixMatch;
2 2     2   82821 use JSON::XS;
  2         28650  
  2         246  
3 2     2   1616 use Cache::LRU;
  2         1250  
  2         47  
4 2     2   1855 use Net::DNS::Lite;
  2         66946  
  2         136  
5 2     2   2946 use Furl;
  0            
  0            
6             use URI;
7             use URI::QueryParam;
8             use Carp;
9             use Moo;
10             use namespace::clean;
11             our $VERSION = "0.04";
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             my ($self, %query_param) = @_;
64             return $self->request($method, \%query_param);
65             };
66             no strict 'refs';
67             my $method_name = $method;
68             $method_name =~ s|\.|_|g;
69             *{$method_name} = $code;
70             }
71              
72              
73             sub request {
74             my ( $self, $path, $query_param ) = @_;
75              
76             my $query = URI->new;
77             $query->query_param( 'apikey', $self->api_key );
78             $query->query_param( 'format', 'json' );
79             map { $query->query_param( $_, $query_param->{$_} ) } keys %$query_param;
80              
81             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             my $data = decode_json( $content );
90             if ( $data->{message}{header}{status_code} != 200 ) {
91             confess $data->{message}{header}{status_code};
92             } else {
93             return $data;
94             }
95             }
96              
97              
98             1;
99             __END__