File Coverage

blib/lib/WebService/Audioscrobbler/Artist.pm
Criterion Covered Total %
statement 12 43 27.9
branch 0 16 0.0
condition 0 2 0.0
subroutine 4 8 50.0
pod 4 4 100.0
total 20 73 27.4


line stmt bran cond sub pod time code
1             package WebService::Audioscrobbler::Artist;
2 1     1   5 use warnings;
  1         2  
  1         24  
3 1     1   5 use strict;
  1         1  
  1         23  
4 1     1   12 use CLASS;
  1         2  
  1         4  
5            
6 1     1   31 use base 'WebService::Audioscrobbler::Base';
  1         1  
  1         503  
7            
8             =head1 NAME
9            
10             WebService::Audioscrobbler::Artist - An object-oriented interface to the Audioscrobbler WebService API
11            
12             =cut
13            
14             our $VERSION = '0.07';
15            
16             # postfix related accessors
17             CLASS->mk_classaccessor("base_resource_path" => "artist");
18            
19             # similar artists related accessors
20             CLASS->mk_classaccessor("similar_artists_postfix" => "similar.xml");
21             CLASS->mk_classaccessor("similar_artists_class" => "WebService::Audioscrobbler::SimilarArtist");
22            
23             # change the field used to sort stuff
24             CLASS->tracks_sort_field('reach');
25            
26             # requiring stuff
27             CLASS->similar_artists_class->require or die($@);
28             CLASS->tracks_class->require or die($@);
29             CLASS->tags_class->require or die($@);
30            
31             # object accessors
32             CLASS->mk_accessors(qw/name mbid streamable picture_url/);
33            
34             =head1 SYNOPSIS
35            
36             This module implements an object oriented abstraction of an artist within the
37             Audioscrobbler database.
38            
39             use WebService::Audioscrobbler;
40            
41             my $ws = WebService::Audioscrobbler->new;
42            
43             # get an object for artist named 'foo'
44             my $artist = $ws->artist('foo');
45            
46             # fetch artists similar to 'foo'
47             my @similar = $artist->similar_artists;
48            
49             print "Artists similar to: " . $artist->name . "\n";
50            
51             # prints each one of their names
52             for my $similar (@similar) {
53             print $similar->name . "\n";
54             }
55            
56             # retrieves tracks from 'foo'
57             my @tracks = $artist->tracks;
58            
59             # retrieves tags associated with 'foo'
60             my @tags = $artist->tags;
61            
62             This module inherits from L.
63            
64             =head1 FIELDS
65            
66             =head2 C
67            
68             The name of a given artist as provided when constructing the object.
69            
70             =head2 C
71            
72             MusicBrainz ID as provided by the Audioscrobbler database.
73            
74             =head2 C
75            
76             URI object pointing to the location of the artist's picture, if available.
77            
78             =head2 C
79            
80             Flag indicating whether the artist has streamable content available.
81            
82             =cut
83            
84             =head1 METHODS
85            
86             =cut
87            
88             =head2 C
89            
90             =head2 C
91            
92             Creates a new object using either the given C<$artist_name> or the C<\%fields>
93             hashref. The data fetcher object is a mandatory parameter and must
94             be provided either as the second parameter or inside the C<\%fields> hashref.
95            
96             =cut
97            
98             sub new {
99 0     0 1   my $class = shift;
100 0           my ($name_or_fields, $data_fetcher) = @_;
101            
102 0 0         my $self = $class->SUPER::new(
103             ref $name_or_fields eq 'HASH' ?
104             $name_or_fields : { name => $name_or_fields, data_fetcher => $data_fetcher }
105             );
106            
107 0 0         $self->croak("No data fetcher provided")
108             unless $self->data_fetcher;
109            
110 0 0         unless (defined $self->name) {
111 0 0         if (defined $self->{content}) {
112 0           $self->name($self->{content});
113             }
114             else {
115 0           $class->croak("Can't create artist without a name");
116             }
117             }
118            
119 0           return $self;
120             }
121            
122             =head2 C
123            
124             =head2 C
125            
126             =head2 C
127            
128             Retrieves similar artists from the Audioscrobbler database. $filter can be used
129             to limit artist with a low similarity index (ie. artists which have a similarity
130             index lower than $filter won't be returned).
131            
132             Returns either a list of artists or a reference to an array of artists when called
133             in list context or scalar context, respectively. The artists are returned as
134             L objects by default.
135            
136             =cut
137            
138             *related_artists = *artists = \&similar_artists;
139            
140             sub similar_artists {
141 0     0 1   my $self = shift;
142 0   0       my $filter = shift || 1;
143            
144 0           my $data = $self->fetch_data($self->similar_artists_postfix);
145            
146 0           $self->load_fields($data);
147            
148 0           my @artists;
149            
150             # check if we've got any similar artists
151 0 0         if (ref $data->{artist} eq 'ARRAY') {
152            
153 0           shift @{$data->{artist}};
  0            
154            
155 0           @artists = map {
156 0           my $artist = $_;
157 0           $self->similar_artists_class->new({
158             picture_url => URI->new($_->{image}),
159             related_to => $self,
160             data_fetcher => $self->data_fetcher,
161 0           map {$_ => $artist->{$_}} qw/name streamable mbid match/
162             })
163 0           } grep { $_->{match} >= $filter } @{$data->{artist}};
  0            
164            
165             }
166            
167 0 0         return wantarray ? @artists : \@artists;
168             }
169            
170             =head2 C
171            
172             Retrieves the artist's top tracks as available on Audioscrobbler's database.
173            
174             Returns either a list of tracks or a reference to an array of tracks when called
175             in list context or scalar context, respectively. The tracks are returned as
176             L objects by default.
177            
178             =cut
179            
180             =head2 C
181            
182             Retrieves the artist's top tags as available on Audioscrobbler's database.
183            
184             Returns either a list of tags or a reference to an array of tags when called
185             in list context or scalar context, respectively. The tags are returned as
186             L objects by default.
187            
188             =cut
189            
190             =head2 C
191            
192             Loads artist fields from the hashref C<\%data>.
193            
194             =cut
195            
196             sub load_fields {
197 0     0 1   my $self = shift;
198 0           my $data = shift;
199            
200 0           $self->streamable($data->{streamable});
201            
202 0 0         $self->picture_url(URI->new($data->{picture}))
203             unless $self->picture_url;
204            
205 0 0         $self->mbid($data->{mbid})
206             unless $self->mbid;
207             }
208            
209             =head2 C
210            
211             Returns the URL from which other URLs used for fetching artist info will be
212             derived from.
213            
214             =cut
215            
216             sub resource_path {
217 0     0 1   my $self = shift;
218 0           $self->uri_builder( $self->name );
219             }
220            
221             =head1 AUTHOR
222            
223             Nilson Santos Figueiredo Júnior, C<< >>
224            
225             =head1 COPYRIGHT & LICENSE
226            
227             Copyright 2006-2007 Nilson Santos Figueiredo Júnior, all rights reserved.
228            
229             This program is free software; you can redistribute it and/or modify it
230             under the same terms as Perl itself.
231            
232             =cut
233            
234             1; # End of WebService::Audioscrobbler::Artist