Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

link: album support #32

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 43 additions & 2 deletions src/link.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* Filename: link.cc
*
* Description: bindings for links subsystem
* Description: bindings for links subsystem
*
* Version: 1.0
* Created: 07/01/2013 12:37:03
Expand Down Expand Up @@ -42,6 +42,24 @@ static Handle<Value> Link_Create_From_Track(const Arguments& args) {
return scope.Close(String::New(url));
}

static Handle<Value> Link_Create_From_Album(const Arguments& args) {
HandleScope scope;

// test arguments sanity
assert(args.Length() == 1);
assert(args[0]->IsObject());

// gets sp_album pointer from given object
ObjectHandle<sp_album>* album = ObjectHandle<sp_album>::Unwrap(args[0]);

sp_link* link = sp_link_create_from_album(album->pointer);
char url[256];
// TODO handle truncated urls
sp_link_as_string(link, url, 256);

return scope.Close(String::New(url));
}

static Handle<Value> Link_Create_From_Artist(const Arguments& args) {
HandleScope scope;

Expand Down Expand Up @@ -96,6 +114,24 @@ static Handle<Value> Link_As_Track(const Arguments& args) {
return scope.Close(track->object);
}

static Handle<Value> Link_As_Album(const Arguments& args) {
HandleScope scope;

// test arguments sanity
assert(args.Length() == 1);
assert(args[0]->IsString());

String::Utf8Value url(args[0]);

sp_link* link = sp_link_create_from_string(*url);
assert(sp_link_type(link) == SP_LINKTYPE_ALBUM);

ObjectHandle<sp_album>* album = new ObjectHandle<sp_album>("sp_album");
album->pointer = sp_link_as_album(link);

return scope.Close(album->object);
}

static Handle<Value> Link_As_Artist(const Arguments& args) {
HandleScope scope;

Expand Down Expand Up @@ -132,7 +168,7 @@ static Handle<Value> Link_As_Playlist(const Arguments& args) {

ObjectHandle<sp_playlist>* playlist = new ObjectHandle<sp_playlist>("sp_playlist");
playlist->pointer = sp_playlist_create(session->pointer, link);

// Add callbacks
sp_error error = sp_playlist_add_callbacks(playlist->pointer, &nsp_playlist_callbacks, playlist);
NSP_THROW_IF_ERROR(error);
Expand Down Expand Up @@ -162,6 +198,9 @@ static Handle<Value> Link_Type(const Arguments& args) {
case SP_LINKTYPE_ARTIST:
type = "artist";
break;
case SP_LINKTYPE_ALBUM:
type = "album";
break;
case SP_LINKTYPE_TRACK:
type = "track";
break;
Expand All @@ -177,9 +216,11 @@ static Handle<Value> Link_Type(const Arguments& args) {

void nsp::init_link(Handle<Object> target) {
NODE_SET_METHOD(target, "link_create_from_track", Link_Create_From_Track);
NODE_SET_METHOD(target, "link_create_from_album", Link_Create_From_Album);
NODE_SET_METHOD(target, "link_create_from_artist", Link_Create_From_Artist);
NODE_SET_METHOD(target, "link_create_from_playlist", Link_Create_From_Playlist);
NODE_SET_METHOD(target, "link_as_track", Link_As_Track);
NODE_SET_METHOD(target, "link_as_album", Link_As_Album);
NODE_SET_METHOD(target, "link_as_artist", Link_As_Artist);
NODE_SET_METHOD(target, "link_as_playlist", Link_As_Playlist);
NODE_SET_METHOD(target, "link_type", Link_Type);
Expand Down
2 changes: 2 additions & 0 deletions test/test-065-link-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ exports.links = {
}, "Getting link type from anything else than string should throw");

var track_link = 'spotify:track:4BdSLkzKO6iMVCgw7A7JBl';
var album_link = 'spotify:album:2UGJa9DjYhXpBDKsCTyhSh';
var artist_link = 'spotify:artist:3zD5liDjbqljSRorrrcEjs';
var playlist_link = 'spotify:user:flobyiv:playlist:5ZMnMnJWGXZ9qm4gacHpQF';
test.doesNotThrow(function() {
test.equal('track', sp.getLinkType(track_link), "Link type should be 'track'");
test.equal('album', sp.getLinkType(album_link), "Link type should be 'album'");
test.equal('artist', sp.getLinkType(artist_link), "Link type should be 'artist'");
test.equal('playlist', sp.getLinkType(playlist_link), "Link type should be 'playlist'");
}, "Getting link types should not throw");
Expand Down
17 changes: 13 additions & 4 deletions test/test-066-link-from-objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ exports.links = {
});
},
"get link from track": testLink(sp.Track, 'spotify:track:4BdSLkzKO6iMVCgw7A7JBl'),
"get link from album": testLink(sp.Album, 'spotify:album:2UGJa9DjYhXpBDKsCTyhSh'),
"get link from artist": testLink(sp.Artist, 'spotify:artist:4ZCLbhEKI7019HKbk5RsUq'),
"get link from playlist": testLink(sp.Playlist, 'spotify:user:flobyiv:playlist:2t8yWR57SFWSKHtOlWr095'),
'get artist link from artist': function(test) {
Expand All @@ -40,13 +41,21 @@ exports.links = {
});
},
'get artist from link': function(test) {
var track = sp.Artist.getFromUrl('spotify:artist:3zD5liDjbqljSRorrrcEjs');
test.ok(track instanceof sp.Artist, 'the returned object should be an artist');
track.on('ready', function() {
test.equal('Guillemots', track.name, 'this should be a guillemots track');
var artist = sp.Artist.getFromUrl('spotify:artist:3zD5liDjbqljSRorrrcEjs');
test.ok(artist instanceof sp.Artist, 'the returned object should be an artist');
artist.on('ready', function() {
test.equal('Guillemots', artist.name, 'this should be the Guillemots artist');
test.done();
});
},
'get album from link': function(test) {
var album = sp.Album.getFromUrl('spotify:album:2UGJa9DjYhXpBDKsCTyhSh');
test.ok(album instanceof sp.Album, 'the returned object should be an album');
album.on('ready', function () {
test.equal('Exile (Deluxe)', album.name, 'this should be the Exile (Deluxe) album');
test.done();
});
}
};