diff --git a/src/link.cc b/src/link.cc index 947c0ea..93001d4 100644 --- a/src/link.cc +++ b/src/link.cc @@ -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 @@ -42,6 +42,24 @@ static Handle Link_Create_From_Track(const Arguments& args) { return scope.Close(String::New(url)); } +static Handle 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* album = ObjectHandle::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 Link_Create_From_Artist(const Arguments& args) { HandleScope scope; @@ -96,6 +114,24 @@ static Handle Link_As_Track(const Arguments& args) { return scope.Close(track->object); } +static Handle 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* album = new ObjectHandle("sp_album"); + album->pointer = sp_link_as_album(link); + + return scope.Close(album->object); +} + static Handle Link_As_Artist(const Arguments& args) { HandleScope scope; @@ -132,7 +168,7 @@ static Handle Link_As_Playlist(const Arguments& args) { ObjectHandle* playlist = new ObjectHandle("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); @@ -162,6 +198,9 @@ static Handle 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; @@ -177,9 +216,11 @@ static Handle Link_Type(const Arguments& args) { void nsp::init_link(Handle 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); diff --git a/test/test-065-link-types.js b/test/test-065-link-types.js index 9ffa5d9..b1d7fa8 100644 --- a/test/test-065-link-types.js +++ b/test/test-065-link-types.js @@ -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"); diff --git a/test/test-066-link-from-objects.js b/test/test-066-link-from-objects.js index 9e6cbdb..2caadab 100644 --- a/test/test-066-link-from-objects.js +++ b/test/test-066-link-from-objects.js @@ -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) { @@ -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(); + }); + } };