| 1 | package Net::LastFM; | ||||||||||
| 2 | 1 | $Net::LastFM::VERSION = '0.37'; | |||||||||
| 3 | 1 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use MooseX::StrictConstructor; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use Digest::MD5 qw(md5_hex); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use JSON::XS::VersionOneAndTwo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use LWP::UserAgent; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use URI::QueryParam; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 1 | has 'api_key' => ( | |||||||||
| 11 | is => 'rw', | ||||||||||
| 12 | isa => 'Str', | ||||||||||
| 13 | required => 1, | ||||||||||
| 14 | ); | ||||||||||
| 15 | |||||||||||
| 16 | 1 | has 'api_secret' => ( | |||||||||
| 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 | 0 | ▶ | my $ua = LWP::UserAgent->new; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 28 | 0 | $ua->agent( 'Net::LastFM/' . $Net::LastFM::VERSION ); | |||||||||
| 29 | 0 | $ua->env_proxy; | |||||||||
| 30 | 0 | return $ua; | |||||||||
| 31 | } | ||||||||||
| 32 | 1 | ); | |||||||||
| 33 | |||||||||||
| 34 | 1 | my $ROOT = 'http://ws.audioscrobbler.com/2.0/'; | |||||||||
| 35 | |||||||||||
| 36 | sub create_http_request { | ||||||||||
| 37 | 0 | ▶ | my ( $self, %conf ) = @_; | ||||||||
|
Subroutine
create_http_request: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 38 | |||||||||||
| 39 | 0 | $conf{api_key} = $self->api_key; | |||||||||
| 40 | 0 | my $uri = URI->new('http://ws.audioscrobbler.com/2.0/'); | |||||||||
| 41 | |||||||||||
| 42 | 0 | foreach my $key ( keys %conf ) { | |||||||||
| 43 | 0 | my $value = $conf{$key}; | |||||||||
| 44 | 0 | $uri->query_param( $key, $value ); | |||||||||
| 45 | } | ||||||||||
| 46 | 0 | $uri->query_param( 'format', 'json' ); | |||||||||
| 47 | |||||||||||
| 48 | 0 | return HTTP::Request->new( 'POST', $uri ); | |||||||||
| 49 | } | ||||||||||
| 50 | |||||||||||
| 51 | sub create_http_request_signed { | ||||||||||
| 52 | 0 | ▶ | my ( $self, %conf ) = @_; | ||||||||
|
Subroutine
create_http_request_signed: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 53 | 0 | $conf{api_key} = $self->api_key; | |||||||||
| 54 | |||||||||||
| 55 | 0 | my $to_hash = ''; | |||||||||
| 56 | 0 | foreach my $key ( sort keys %conf ) { | |||||||||
| 57 | 0 | my $value = $conf{$key}; | |||||||||
| 58 | 0 | $to_hash .= $key . $value; | |||||||||
| 59 | } | ||||||||||
| 60 | 0 | $to_hash .= $self->api_secret; | |||||||||
| 61 | 0 | $conf{api_sig} = md5_hex($to_hash); | |||||||||
| 62 | |||||||||||
| 63 | 0 | return $self->create_http_request(%conf); | |||||||||
| 64 | } | ||||||||||
| 65 | |||||||||||
| 66 | sub request { | ||||||||||
| 67 | 0 | ▶ | my ( $self, %conf ) = @_; | ||||||||
|
Subroutine
request: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 68 | 0 | my $request = $self->create_http_request(%conf); | |||||||||
| 69 | 0 | return $self->_make_request($request); | |||||||||
| 70 | } | ||||||||||
| 71 | |||||||||||
| 72 | sub request_signed { | ||||||||||
| 73 | 0 | ▶ | my ( $self, %conf ) = @_; | ||||||||
|
Subroutine
request_signed: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 74 | 0 | my $request = $self->create_http_request_signed(%conf); | |||||||||
| 75 | 0 | return $self->_make_request($request); | |||||||||
| 76 | } | ||||||||||
| 77 | |||||||||||
| 78 | sub _make_request { | ||||||||||
| 79 | 0 | ▶ | my ( $self, $request ) = @_; | ||||||||
|
Subroutine
_make_request: not called CC=2 SCAR=17.9
| |||||||||||
| 80 | 0 | my $ua = $self->ua; | |||||||||
| 81 | |||||||||||
| 82 | 0 | my $response = $ua->request($request); | |||||||||
| 83 | 0 | my $data = from_json( $response->content ); | |||||||||
| 84 | |||||||||||
| 85 | 0 | ▶ | if ( defined $data->{error} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 86 | 0 | my $code = $data->{error}; | |||||||||
| 87 | 0 | my $message = $data->{message}; | |||||||||
| 88 | 0 | confess "$code: $message"; | |||||||||
| 89 | } else { | ||||||||||
| 90 | 0 | return $data; | |||||||||
| 91 | } | ||||||||||
| 92 | } | ||||||||||
| 93 | |||||||||||
| 94 | 1 | 1; | |||||||||
| 95 | |||||||||||
| 96 | __END__ | ||||||||||
| 97 | |||||||||||
| 98 | =head1 NAME | ||||||||||
| 99 | |||||||||||
| 100 | Net::LastFM - A simple interface to the Last.fm API | ||||||||||
| 101 | |||||||||||
| 102 | =head1 SYNOPSIS | ||||||||||
| 103 | |||||||||||
| 104 | my $lastfm = Net::LastFM->new( | ||||||||||
| 105 | api_key => 'XXX', | ||||||||||
| 106 | api_secret => 'YYY', | ||||||||||
| 107 | ); | ||||||||||
| 108 | my $data = $lastfm->request_signed( | ||||||||||
| 109 | method => 'user.getRecentTracks', | ||||||||||
| 110 | user => 'lglb', | ||||||||||
| 111 | ); | ||||||||||
| 112 | |||||||||||
| 113 | =head1 DESCRIPTION | ||||||||||
| 114 | |||||||||||
| 115 | The module provides a simple interface to the Last.fm API. To use | ||||||||||
| 116 | this module, you must first sign up at L<http://www.last.fm/api> | ||||||||||
| 117 | to receive an API key and secret. | ||||||||||
| 118 | |||||||||||
| 119 | You can then make requests on the API - most of the requests | ||||||||||
| 120 | are signed. You pass in a hash of paramters and a data structure | ||||||||||
| 121 | mirroring the response is returned. | ||||||||||
| 122 | |||||||||||
| 123 | This module confesses if there is an error. | ||||||||||
| 124 | |||||||||||
| 125 | =head1 METHODS | ||||||||||
| 126 | |||||||||||
| 127 | =head2 request | ||||||||||
| 128 | |||||||||||
| 129 | This makes an unsigned request: | ||||||||||
| 130 | |||||||||||
| 131 | my $data = $lastfm->request( method => 'auth.gettoken' ); | ||||||||||
| 132 | |||||||||||
| 133 | =head2 request_signed | ||||||||||
| 134 | |||||||||||
| 135 | This makes a signed request: | ||||||||||
| 136 | |||||||||||
| 137 | my $data = $lastfm->request_signed( | ||||||||||
| 138 | method => 'user.getRecentTracks', | ||||||||||
| 139 | user => 'lglb', | ||||||||||
| 140 | ); | ||||||||||
| 141 | |||||||||||
| 142 | =head2 create_http_request | ||||||||||
| 143 | |||||||||||
| 144 | If you want to integrate this module into another HTTP framework, this | ||||||||||
| 145 | method will simple create an unsigned L<HTTP::Request> object: | ||||||||||
| 146 | |||||||||||
| 147 | my $http_request = $lastfm->create_http_request( | ||||||||||
| 148 | method => 'auth.gettoken' | ||||||||||
| 149 | ); | ||||||||||
| 150 | |||||||||||
| 151 | =head2 create_http_request_signed | ||||||||||
| 152 | |||||||||||
| 153 | If you want to integrate this module into another HTTP framework, this | ||||||||||
| 154 | method will simple create a signed L<HTTP::Request> object: | ||||||||||
| 155 | |||||||||||
| 156 | my $http_request = $lastfm->create_http_request_signed( | ||||||||||
| 157 | method => 'user.getRecentTracks', | ||||||||||
| 158 | user => 'lglb', | ||||||||||
| 159 | ); | ||||||||||
| 160 | |||||||||||
| 161 | =head1 AUTHOR | ||||||||||
| 162 | |||||||||||
| 163 | Leon Brocard <acme@astray.com> | ||||||||||
| 164 | |||||||||||
| 165 | =head1 COPYRIGHT | ||||||||||
| 166 | |||||||||||
| 167 | Copyright (C) 2008-9, Leon Brocard. | ||||||||||
| 168 | |||||||||||
| 169 | =head1 LICENSE | ||||||||||
| 170 | |||||||||||
| 171 | This module is free software; you can redistribute it or | ||||||||||
| 172 | modify it under the same terms as Perl itself. | ||||||||||