File Coverage

blib/lib/WebService/EchoNest.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             package WebService::EchoNest;
2 1     1   178099 use Moose;
  0            
  0            
3             use MooseX::StrictConstructor;
4             use JSON::XS::VersionOneAndTwo;
5             use LWP::UserAgent;
6             use URI::QueryParam;
7              
8             our $VERSION = '0.007';
9              
10             has 'api_root' => (
11             is => 'rw',
12             isa => 'Str',
13             default => 'http://developer.echonest.com/api/v4/'
14             );
15              
16             has 'api_key' => (
17             is => 'rw',
18             isa => 'Str',
19             required => 1,
20             );
21              
22             has 'ua' => (
23             is => 'rw',
24             isa => 'LWP::UserAgent',
25             required => 0,
26             default => sub {
27             my $ua = LWP::UserAgent->new;
28             $ua->agent( 'WebService::Echonest/' . $VERSION );
29             $ua->env_proxy;
30             return $ua;
31             }
32             );
33              
34             sub request {
35             my ( $self, $method, %conf ) = @_;
36             my $request = $self->create_http_request($method, %conf);
37             return $self->_make_request($request);
38             }
39              
40             sub create_http_request {
41             my ( $self, $method, %conf ) = @_;
42              
43             $conf{api_key} = $self->api_key;
44             my $uri = URI->new($self->api_root . $method);
45              
46             foreach my $key ( keys %conf ) {
47             my $value = $conf{$key};
48             $uri->query_param( $key, $value );
49             }
50             $uri->query_param( 'format', 'json' );
51              
52             return HTTP::Request->new( 'GET', $uri );
53             }
54              
55             sub _make_request {
56             my ( $self, $request ) = @_;
57             my $ua = $self->ua;
58              
59             my $response = $ua->request($request);
60             my $data = from_json( $response->content );
61              
62             my $status = $data->{response}->{status};
63             confess "Unexpected response format" if !$status;
64            
65             my $code = $status->{code};
66              
67             if ($code != 0) {
68             confess "$code: $status->{message}";
69             } else {
70             return $data;
71             }
72             }
73              
74             1;
75              
76             __END__
77              
78             =head1 NAME
79              
80             WebService::EchoNest - A simple interface to the EchoNest API
81              
82             =head1 SYNOPSIS
83              
84             my $echonest = WebService::EchoNest->new(
85             api_key => 'XXX',
86             );
87            
88             my $data = $echonest->request('artist/search',
89             name => 'Radiohead',
90             bucket => ['biographies'],
91             limit => 'true'
92             );
93              
94             =head1 DESCRIPTION
95              
96             The module provides a simple interface to the EchoNest API. To use
97             this module, you must first sign up at L<http://developer.echonest.com/>
98             to receive an API key.
99              
100             You can then make requests on the API. You pass in a method name and hash of paramters, and a data structure
101             mirroring the response is returned.
102              
103             This module confesses if there is an error.
104              
105             =head1 METHODS
106              
107             =head2 request
108              
109             This makes a request:
110              
111             my $data = $echonest->request('artist/search',
112             name => 'Black Moth Super Rainbow',
113             bucket => ['images'],
114             limit => 'true'
115             );
116              
117             =head2 create_http_request
118              
119             If you want to integrate this module into another HTTP framework, this
120             method will create an L<HTTP::Request> object:
121              
122             my $http_request = $echonest->create_http_request('artist/search',
123             name => 'Black Moth Super Rainbow',
124             bucket => ['images'],
125             limit => 'true'
126             );
127              
128             =head1 AUTHOR
129              
130             Nick Langridge <nickl@cpan.org>
131              
132             =head1 CREDITS
133              
134             This module was based on Net::LastFM by Leon Brocard.
135              
136             =head1 COPYRIGHT
137              
138             Copyright (C) 2013 Nick Langridge
139              
140             =head1 LICENSE
141              
142             This module is free software; you can redistribute it or
143             modify it under the same terms as Perl itself.