File Coverage

blib/lib/WebService/Decibel.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::Decibel;
2 1     1   2276 use JSON::XS;
  1         31151  
  1         87  
3 1     1   1069 use Cache::LRU;
  1         746  
  1         33  
4 1     1   992 use Net::DNS::Lite;
  1         99647  
  1         172  
5 1     1   563 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.02";
12              
13              
14             $Net::DNS::Lite::CACHE = Cache::LRU->new( size => 512 );
15              
16             has 'app_id' => (
17             is => 'rw',
18             isa => sub { $_[0] },
19             required => 1,
20             default => sub { $ENV{DECIBEL_APPLICATION_ID} },
21             );
22              
23             has 'app_key' => (
24             is => 'rw',
25             isa => sub { $_[0] },
26             required => 1,
27             default => sub { $ENV{DECIBEL_APPLICATION_KEY} },
28             );
29              
30             my $build_http = sub {
31             my $self = shift;
32             my $http = Furl::HTTP->new(
33             inet_aton => \&Net::DNS::Lite::inet_aton,
34             agent => 'WebService::Decibel/' . $VERSION,
35             headers => [
36             'Accept-Encoding' => 'gzip',
37             'DecibelAppID' => $self->app_id,
38             'DecibelAppKey' => $self->app_key,
39             'DecibelTimestamp' => '20140903 17:22:33',
40             ],
41             );
42             return $http;
43             };
44              
45             has 'http' => (
46             is => 'lazy',
47             required => 1,
48             default => $build_http,
49             );
50              
51             my @methods = qw( album albums artist artists disctags image recording recordings );
52             for my $method (@methods) {
53             my $code = sub {
54             my ($self, %query_param) = @_;
55             return $self->request($method, \%query_param);
56             };
57             no strict 'refs';
58             *{$method} = $code;
59             }
60              
61              
62             sub request {
63             my ( $self, $path, $query_param ) = @_;
64              
65             my $query = URI->new;
66             map { $query->query_param( $_, $query_param->{$_} ) } keys %$query_param;
67              
68             my ($minor_version, $status_code, $message, $headers, $content) =
69             $self->http->request(
70             scheme => 'http',
71             host => 'rest.decibel.net',
72             path_query => "v2/$path$query",
73             method => 'GET',
74             );
75              
76             if ( $content !~ /^\{/) {
77             confess $content;
78             } else {
79             return decode_json( $content );
80             }
81             }
82              
83              
84             1;
85             __END__