Skip to content

Commit

Permalink
Merged
Browse files Browse the repository at this point in the history
  • Loading branch information
xhochy committed Jun 17, 2009
2 parents c22435c + 55ee5dd commit 5d22649
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 19 deletions.
45 changes: 42 additions & 3 deletions lib/scrobbler/playlist.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,51 @@
module Scrobbler
# @todo everything
class Playlist < Base
attr_reader :url
attr_reader :url, :id, :title, :date, :creator
attr_reader :description, :size, :duration, :streamable
class << self
def new_from_libxml(xml)
data = {}

xml.children.each do |child|
data[:id] = child.content.to_i if child.name == 'id'
data[:title] = child.content if child.name == 'title'

if child.name == 'image'
data[:image_small] = child.content if child['size'] == 'small'
data[:image_medium] = child.content if child['size'] == 'medium'
data[:image_large] = child.content if child['size'] == 'large'
end
data[:date] = Time.parse(child.content) if child.name == 'date'

data[:size] = child.content.to_i if child.name == 'size'
data[:description] = child.content if child.name == 'description'
data[:duration] = child.content.to_i if child.name == 'duration'

if child.name == 'streamable'
if ['1', 'true'].include?(child.content)
data[:streamable] = true
else
data[:streamable] = false
end
end
data[:creator] = child.content if child.name == 'creator'
data[:url] = child.content if child.name == 'url'
end
Playlist.new(data[:url],data)
end
end

def initialize(url)
def initialize(url,data={})
@url = url
populate_data(data)
end

def image(which=:small)
which = which.to_s
raise ArgumentError unless ['small', 'medium', 'large'].include?(which)
instance_variable_get("@image_#{which}")
end

end
end

6 changes: 3 additions & 3 deletions lib/scrobbler/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,9 @@ def past_events(format=:ics)
end

# Get a list of a user's playlists on Last.fm.
def playlists
# This needs a Playlist class which is yet not available
raise NotImplementedError
def playlists(force=false)
#(api_method, instance_name, parent, element, parameters, force=false)
get_response('user.getplaylists', :playlist, 'playlists', 'playlist', {'user'=>@username}, force)
end

# Get a list of the recent tracks listened to by this user. Indicates now
Expand Down
61 changes: 61 additions & 0 deletions test/fixtures/xml/user/playlists.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?xml version="1.0" encoding="utf-8"?>
<lfm status="ok">
<playlists user="RJ">
<playlist>
<id>5606</id>
<title>Misc gubbins</title>
<description>This is a misc test playlist with a few random tracks in it.</description>
<date>2006-11-15T13:05:48</date>
<size>10</size>
<duration>2771</duration>
<streamable>0</streamable>
<creator>http://www.last.fm/user/RJ</creator>
<url>http://www.last.fm/user/RJ/library/playlists/4bq_misc_gubbins</url>
<image size="small">http://userserve-ak.last.fm/serve/34/4218758.jpg</image>
<image size="medium">http://userserve-ak.last.fm/serve/64/4218758.jpg</image>
<image size="large">http://userserve-ak.last.fm/serve/126/4218758.jpg</image>
</playlist>
<playlist>
<id>2615079</id>
<title>Duck playlist</title>
<description>Duck and cover</description>
<date>2008-05-22T09:40:09</date>
<size>10</size>
<duration>2143</duration>
<streamable>0</streamable>
<creator>http://www.last.fm/user/RJ</creator>
<url>http://www.last.fm/user/RJ/library/playlists/1k1t3_duck_playlist</url>
<image size="small">http://userserve-ak.last.fm/serve/34/5985590.jpg</image>
<image size="medium">http://userserve-ak.last.fm/serve/64/5985590.jpg</image>
<image size="large">http://userserve-ak.last.fm/serve/126/5985590.jpg</image>
</playlist>
<playlist>
<id>2614993</id>
<title>Doglist</title>
<description>Guess what i searched for..</description>
<date>2008-05-22T09:24:23</date>
<size>18</size>
<duration>3544</duration>
<streamable>0</streamable>
<creator>http://www.last.fm/user/RJ</creator>
<url>http://www.last.fm/user/RJ/library/playlists/1k1qp_doglist</url>
<image size="small">http://userserve-ak.last.fm/serve/34/5976386.jpg</image>
<image size="medium">http://userserve-ak.last.fm/serve/64/5976386.jpg</image>
<image size="large">http://userserve-ak.last.fm/serve/126/5976386.jpg</image>
</playlist>
<playlist>
<id>2612216</id>
<title>Sexyplaylist</title>
<description>My only regret is that the search feature doesn't give me more than 10 results</description>
<date>2008-05-21T19:43:46</date>
<size>9</size>
<duration>2416</duration>
<streamable>0</streamable>
<creator>http://www.last.fm/user/RJ</creator>
<url>http://www.last.fm/user/RJ/library/playlists/1jzlk_sexyplaylist</url>
<image size="small">http://userserve-ak.last.fm/serve/34/5976429.jpg</image>
<image size="medium">http://userserve-ak.last.fm/serve/64/5976429.jpg</image>
<image size="large">http://userserve-ak.last.fm/serve/126/5976429.jpg</image>
</playlist>
</playlists>
</lfm>
6 changes: 3 additions & 3 deletions test/mocks/rest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@
FakeWeb.register_uri(:get, 'http://ws.audioscrobbler.com:80/2.0/?method=geo.getevents&api_key=foo123&page=3&force=false&location=Manchester', :file => File.join([FIXTURES_BASE, 'geo', 'events-p3.xml']))
FakeWeb.register_uri(:get, 'http://ws.audioscrobbler.com:80/2.0/?distance=15&method=geo.getevents&api_key=foo123&page=1&force=false', :file => File.join([FIXTURES_BASE, 'geo', 'events-distance-p1.xml']))
FakeWeb.register_uri(:get, 'http://ws.audioscrobbler.com:80/2.0/?long=-74.00639&method=geo.getevents&api_key=foo123&page=1&lat=40.71417&force=false', :file => File.join([FIXTURES_BASE, 'geo', 'events-lat-long.xml']))

FakeWeb.register_uri(:get, 'http://ws.audioscrobbler.com:80/2.0/?method=geo.gettopartists&api_key=foo123&page=1&force=false&location=Spain', :file => File.join([FIXTURES_BASE, 'geo', 'top_artists-p1.xml']))

FakeWeb.register_uri(:get, 'http://ws.audioscrobbler.com:80/2.0/?method=geo.gettoptracks&api_key=foo123&page=1&force=false&location=Germany', :file => File.join([FIXTURES_BASE, 'geo', 'top_tracks-p1.xml']))


#user
FakeWeb.register_uri(:get, 'http://ws.audioscrobbler.com:80/2.0/?method=user.getplaylists&api_key=foo123&user=jnunemaker', :file => File.join([FIXTURES_BASE, 'user', 'playlists.xml']))



Expand Down
48 changes: 38 additions & 10 deletions test/unit/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@
end

it 'should be able to get a list of its friends' do
@user.friends.should be_kind_of(Array)
@user.friends.should have(3).items
@user.friends.first.should be_kind_of(Scrobbler::User)
@user.friends.first.name.should eql('lobsterclaw')
@user.friends.first.realname.should eql('Laura Weiss')
@user.friends.first.image(:small).should eql('http://userserve-ak.last.fm/serve/34/1733471.jpg')
@user.friends.first.image(:medium).should eql('http://userserve-ak.last.fm/serve/64/1733471.jpg')
@user.friends.first.image(:large).should eql('http://userserve-ak.last.fm/serve/126/1733471.jpg')
@user.friends.first.url.should eql('http://www.last.fm/user/lobsterclaw')
@user.friends.should be_kind_of(Array)
@user.friends.should have(3).items
@user.friends.first.should be_kind_of(Scrobbler::User)
@user.friends.first.name.should eql('lobsterclaw')
@user.friends.first.realname.should eql('Laura Weiss')
@user.friends.first.image(:small).should eql('http://userserve-ak.last.fm/serve/34/1733471.jpg')
@user.friends.first.image(:medium).should eql('http://userserve-ak.last.fm/serve/64/1733471.jpg')
@user.friends.first.image(:large).should eql('http://userserve-ak.last.fm/serve/126/1733471.jpg')
@user.friends.first.url.should eql('http://www.last.fm/user/lobsterclaw')
end

it 'should be able to load additional information'
Expand Down Expand Up @@ -102,8 +102,36 @@
end

it 'should be able to list the attened events'

describe 'retrieving a users playlist' do

before do
@playlists = @user.playlists
@firstplaylist = @playlists.first
end

it 'should return 4 playlists' do
@playlists.size.should eql(4)
end

it 'should have the correct attributes in the first playlist' do
#@firstplaylist.id.should eql(5606)
require 'pp'
pp @firstplaylist.id
@firstplaylist.title.should eql('Misc gubbins')
@firstplaylist.description.should eql('This is a misc test playlist with a few random tracks in it.')
@firstplaylist.date.should eql(Time.mktime(2006, 11, 15, 13, 05, 48))
@firstplaylist.size.should eql(10)
@firstplaylist.duration.should eql(2771)
@firstplaylist.streamable.should be_false
@firstplaylist.creator.should eql('http://www.last.fm/user/RJ')
@firstplaylist.url.should eql('http://www.last.fm/user/RJ/library/playlists/4bq_misc_gubbins')
@firstplaylist.image(:small).should eql('http://userserve-ak.last.fm/serve/34/4218758.jpg')
@firstplaylist.image(:medium).should eql('http://userserve-ak.last.fm/serve/64/4218758.jpg')
@firstplaylist.image(:large).should eql('http://userserve-ak.last.fm/serve/126/4218758.jpg')
end
end

it 'should be able to get the playlists'

it 'should be able to fetch the recent tracks' do
@user.should have(10).recent_tracks
Expand Down

0 comments on commit 5d22649

Please sign in to comment.