Skip to content

Commit

Permalink
Added user.getTopAlbums and user.getTopArtists along with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lfzawacki committed Jan 26, 2012
1 parent 12ec145 commit d3bf19a
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ It supports methods which require {authentication}[http://www.last.fm/api/authen
* {user.getFriends}[http://www.lastfm.jp/api/show?service=263]
* {user.getRecentTracks}[http://www.lastfm.jp/api/show?service=278]
* {user.getNeighbours}[http://www.lastfm.jp/api/show?service=264]
* {user.getTopArtists}[http://www.lastfm.jp/api/show/user.getTopArtists]
* {user.getTopAlbuns}[http://www.lastfm.jp/api/show/user.getTopAlbums]

=== Geo

Expand Down
8 changes: 8 additions & 0 deletions lib/lastfm/method_category/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ class User < Base
response.xml['user'][0]
end

regular_method :get_top_albums, [:user], [[:period, nil], [:limit, nil], [:page, nil]] do |response|
response.xml['topalbums']['album']
end

regular_method :get_top_artists, [:user], [[:period, nil], [:limit, nil], [:page, nil]] do |response|
response.xml['topartists']['artist']
end

regular_method :get_friends, [:user], [[:recenttracks, nil], [:limit, nil], [:page, nil]] do |response|
response.xml['friends']['user']
end
Expand Down
29 changes: 29 additions & 0 deletions spec/fixtures/user_get_top_albums.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<lfm status="ok">
<topalbums user="test" type="overall">
<album rank="1">
<name>The Wall</name>
<playcount>536</playcount>
<mbid>639f0d06-53bd-32a5-8f65-1db785743ab0</mbid>
<url>http://www.last.fm/music/Pink+Floyd/The+Wall</url>
<artist>
<name>Pink Floyd</name>
<mbid>83d91898-7763-47d7-b03b-b92132375c47</mbid>
<url>http://www.last.fm/music/Pink+Floyd</url>
</artist>
<image size="small">http://userserve-ak.last.fm/serve/34s/70211546.png</image>
</album>
<album rank="2">
<name>The Perfect Element, Part I</name>
<playcount>524</playcount>
<mbid>3039a1a9-a8d2-4560-81c7-b0af31f692b3</mbid>
<url>http://www.last.fm/music/Pain+Of+Salvation/The+Perfect+Element%2C+Part+I</url>
<artist>
<name>Pain of Salvation</name>
<mbid>f7c65346-9631-4220-9188-5e90baae58d5</mbid>
<url>http://www.last.fm/music/Pain+of+Salvation</url>
</artist>
<image size="small">http://userserve-ak.last.fm/serve/34s/66884232.png</image>
</album>
</topalbums>
</lfm>
29 changes: 29 additions & 0 deletions spec/fixtures/user_get_top_artists.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<lfm status="ok">
<topartists user="test" type="overall">
<artist rank="1">
<name>Pain of Salvation</name>
<playcount>1354</playcount>
<mbid>28503ab7-8bf2-4666-a7bd-2644bfc7cb1d</mbid>
<url>f7c65346-9631-4220-9188-5e90baae58d5</url>
<streamable>1</streamable>
<image size="small">http://userserve-ak.last.fm/serve/34/53572699.png</image>
</artist>
<artist rank="2">
<name>Opeth</name>
<playcount>1186</playcount>
<mbid>c14b4180-dc87-481e-b17a-64e4150f90f6</mbid>
<url>http://www.last.fm/music/Opeth</url>
<streamable>1</streamable>
<image size="small">http://userserve-ak.last.fm/serve/34/66663756.png</image>
</artist>
<artist rank="3">
<name>Nevermore</name>
<playcount>959</playcount>
<mbid>7d093650-89be-4108-842b-ba7f5367504b</mbid>
<url>http://www.last.fm/music/Nevermore</url>
<streamable>1</streamable>
<image size="small">http://userserve-ak.last.fm/serve/34/239749.jpg</image>
</artist>
</topartists>
</lfm>
46 changes: 46 additions & 0 deletions spec/lastfm_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,52 @@
end
end

describe '#get_top_artists' do
it 'should get user\'s top artists' do
@lastfm.should_receive(:request).with('user.getTopArtists', {
:user => 'test',
:period => 'overall',
:limit => nil,
:page => nil
}).and_return(make_response('user_get_top_artists'))

artists = @lastfm.user.get_top_artists('test', 'overall', nil, nil)

artists.size.should == 3
artists[0]['name'].should == "Pain of Salvation"
artists[0]['playcount'].should == '1354'

artists[1]['name'].should == "Opeth"
artists[1]['playcount'].should == '1186'

artists[2]['name'].should == "Nevermore"
artists[2]['playcount'].should == '959'
end
end

describe '#get_top_albums' do
it 'should get user\'s top albums' do
@lastfm.should_receive(:request).with('user.getTopAlbums', {
:user => 'test',
:period => 'overall',
:limit => nil,
:page => nil
}).and_return(make_response('user_get_top_albums'))

albums = @lastfm.user.get_top_albums('test', 'overall', nil, nil)

albums.size.should == 2

albums[0]['rank'].should == '1'
albums[0]['name'].should == 'The Wall'
albums[0]['artist']['name'].should == 'Pink Floyd'

albums[1]['rank'].should == '2'
albums[1]['name'].should == 'The Perfect Element, Part I'
albums[1]['artist']['name'].should == 'Pain of Salvation'
end
end

describe '#get_friends' do
it 'should get user\'s friends' do
@lastfm.should_receive(:request).with('user.getFriends', {
Expand Down

0 comments on commit d3bf19a

Please sign in to comment.