From 6a5ce091cb2866d7f31e7db0534402778ccefd58 Mon Sep 17 00:00:00 2001 From: Ed Moore Date: Wed, 17 Sep 2014 22:13:39 +0800 Subject: [PATCH] removeUserTracks can now pass IDs and Spotify URIs --- dist/angular-spotify.min.js | 4 ++-- src/angular-spotify.js | 4 ++++ test/spec/angular-spotify.spec.js | 16 ++++++++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/dist/angular-spotify.min.js b/dist/angular-spotify.min.js index ec53922..ae3a713 100644 --- a/dist/angular-spotify.min.js +++ b/dist/angular-spotify.min.js @@ -1,2 +1,2 @@ -/*! angular-spotify 2014-09-15 */ -!function(a,b){"use strict";var c={};b.module("spotify",[]).value("settings",c).provider("Spotify",function(){c.clientId=null,c.redirectUri=null,c.scope=null,c.accessToken=null,this.setClientId=function(a){return c.clientId=a,c.clientId},this.getClientId=function(){return c.clientId},this.setRedirectUri=function(a){return c.redirectUri=a,c.redirectUri},this.getRedirectUri=function(){return c.redirectUri},this.setScope=function(a){return c.scope=a,c.scope};var d={};d.toQueryString=function(a){var b=[];for(var c in a)a.hasOwnProperty(c)&&b.push(encodeURIComponent(c)+"="+encodeURIComponent(a[c]));return b.join("&")},c.version="v1",c.apiBase="https://api.spotify.com/"+c.version,this.$get=["$q","$http","$window",function(e,f,g){function h(){this.clientId=c.clientId,this.redirectUri=c.redirectUri,this.apiBase=c.apiBase,this.scope=c.scope,this.accessToken=null,this.toQueryString=d.toQueryString}return h.prototype.api=function(a,b,c,d,g){var h=e.defer();return f({url:this.apiBase+a,method:b?b:"GET",params:c,data:d,headers:g}).success(function(a){h.resolve(a)}).error(function(a){h.reject(a)}),h.promise},h.prototype.search=function(a,b,c){return c=c||{},c.q=a,c.type=b,this.api("/search","GET",c)},h.prototype.getAlbum=function(a){return a=-1===a.indexOf("spotify:")?a:a.split(":")[2],this.api("/albums/"+a)},h.prototype.getAlbums=function(a){return a=b.isString(a)?a.split(","):a,b.forEach(a,function(b,c){a[c]=b.indexOf("spotify:")>-1?b.split(":")[2]:b}),this.api("/albums","GET",{ids:a?a.toString():""})},h.prototype.getAlbumTracks=function(a,b){return a=-1===a.indexOf("spotify:")?a:a.split(":")[2],this.api("/albums/"+a+"/tracks","GET",b)},h.prototype.getArtist=function(a){return a=-1===a.indexOf("spotify:")?a:a.split(":")[2],this.api("/artists/"+a)},h.prototype.getArtists=function(a){return a=b.isString(a)?a.split(","):a,b.forEach(a,function(b,c){a[c]=b.indexOf("spotify:")>-1?b.split(":")[2]:b}),this.api("/artists/","GET",{ids:a?a.toString():""})},h.prototype.getArtistAlbums=function(a,b){return a=-1===a.indexOf("spotify:")?a:a.split(":")[2],this.api("/artists/"+a+"/albums","GET",b)},h.prototype.getArtistTopTracks=function(a,b){return a=-1===a.indexOf("spotify:")?a:a.split(":")[2],this.api("/artists/"+a+"/top-tracks","GET",{country:b})},h.prototype.getRelatedArtists=function(a){return a=-1===a.indexOf("spotify:")?a:a.split(":")[2],this.api("/artists/"+a+"/related-artists")},h.prototype.getTrack=function(a){return a=-1===a.indexOf("spotify:")?a:a.split(":")[2],this.api("/tracks/"+a)},h.prototype.getTracks=function(a){return a=b.isString(a)?a.split(","):a,b.forEach(a,function(b,c){a[c]=b.indexOf("spotify:")>-1?b.split(":")[2]:b}),this.api("/tracks/","GET",{ids:a?a.toString():""})},h.prototype.getUserPlaylists=function(a,b){return this.api("/users/"+a+"/playlists","GET",b,null,{Authorization:"Bearer "+this.authToken})},h.prototype.getPlaylist=function(a,b,c){return this.api("/users/"+a+"/playlists/"+b,"GET",c,null,{Authorization:"Bearer "+this.authToken})},h.prototype.getPlaylistTracks=function(a,b,c){return this.api("/users/"+a+"/playlists/"+b+"/tracks","GET",c,null,{Authorization:"Bearer "+this.authToken})},h.prototype.createPlaylist=function(a,b){return this.api("/users/"+a+"/playlists","POST",null,b,{Authorization:"Bearer "+this.authToken,"Content-Type":"application/json"})},h.prototype.addPlaylistTracks=function(a,c,d,e){return d=b.isArray(d)?d:d.split(","),b.forEach(d,function(a,b){d[b]=-1===a.indexOf("spotify:")?"spotify:track:"+a:a}),this.api("/users/"+a+"/playlists/"+c+"/tracks","POST",{uris:d.toString(),position:e?e.position:null},null,{Authorization:"Bearer "+this.authToken,"Content-Type":"application/json"})},h.prototype.removePlaylistTracks=function(a,c,d){d=b.isArray(d)?d:d.split(",");var e;return b.forEach(d,function(a,b){e=d[b],d[b]={uri:-1===e.indexOf("spotify:")?"spotify:track:"+e:e}}),this.api("/users/"+a+"/playlists/"+c+"/tracks","DELETE",null,{tracks:d},{Authorization:"Bearer "+this.authToken,"Content-Type":"application/json"})},h.prototype.replacePlaylistTracks=function(a,c,d){d=b.isArray(d)?d:d.split(",");var e;return b.forEach(d,function(a,b){e=d[b],d[b]=-1===e.indexOf("spotify:")?"spotify:track:"+e:e}),this.api("/users/"+a+"/playlists/"+c+"/tracks","PUT",{uris:d.toString()},null,{Authorization:"Bearer "+this.authToken,"Content-Type":"application/json"})},h.prototype.updatePlaylistDetails=function(a,b,c){return this.api("/users/"+a+"/playlists/"+b,"PUT",null,c,{Authorization:"Bearer "+this.authToken,"Content-Type":"application/json"})},h.prototype.getUser=function(a){return this.api("/users/"+a)},h.prototype.getCurrentUser=function(){return this.api("/me","GET",null,null,{Authorization:"Bearer "+this.authToken})},h.prototype.getSavedUserTracks=function(a){return this.api("/me/tracks","GET",a,null,{Authorization:"Bearer "+this.authToken})},h.prototype.userTracksContains=function(a){return a=b.isString(a)?a.split(","):a,b.forEach(a,function(b,c){a[c]=b.indexOf("spotify:")>-1?b.split(":")[2]:b}),this.api("/me/tracks/contains","GET",{ids:a.toString()},null,{Authorization:"Bearer "+this.authToken})},h.prototype.saveUserTracks=function(a){return a=b.isString(a)?a.split(","):a,b.forEach(a,function(b,c){a[c]=b.indexOf("spotify:")>-1?b.split(":")[2]:b}),this.api("/me/tracks","PUT",{ids:a.toString()},null,{Authorization:"Bearer "+this.authToken})},h.prototype.removeUserTracks=function(a){return this.api("/me/tracks","DELETE",{ids:a.toString()},null,{Authorization:"Bearer "+this.authToken,"Content-Type":"application/json"})},h.prototype.setAuthToken=function(a){return this.authToken=a,this.authToken},h.prototype.login=function(){function b(a){l&&l.close(),d.setAuthToken(a.data),c.resolve(a.data)}var c=e.defer(),d=this,f=400,h=500,i=screen.width/2-f/2,j=screen.height/2-h/2,k={client_id:this.clientId,redirect_uri:this.redirectUri,scope:this.scope||"",response_type:"token"},l=a.open("https://accounts.spotify.com/authorize?"+this.toQueryString(k),"Spotify","menubar=no,location=no,resizable=no,scrollbars=no,status=no,width="+f+",height="+h+",top="+j+",left="+i);return g.addEventListener("message",b,!1),c.promise},new h}]})}(window,angular); \ No newline at end of file +/*! angular-spotify 2014-09-17 */ +!function(a,b){"use strict";var c={};b.module("spotify",[]).value("settings",c).provider("Spotify",function(){c.clientId=null,c.redirectUri=null,c.scope=null,c.accessToken=null,this.setClientId=function(a){return c.clientId=a,c.clientId},this.getClientId=function(){return c.clientId},this.setRedirectUri=function(a){return c.redirectUri=a,c.redirectUri},this.getRedirectUri=function(){return c.redirectUri},this.setScope=function(a){return c.scope=a,c.scope};var d={};d.toQueryString=function(a){var b=[];for(var c in a)a.hasOwnProperty(c)&&b.push(encodeURIComponent(c)+"="+encodeURIComponent(a[c]));return b.join("&")},c.version="v1",c.apiBase="https://api.spotify.com/"+c.version,this.$get=["$q","$http","$window",function(e,f,g){function h(){this.clientId=c.clientId,this.redirectUri=c.redirectUri,this.apiBase=c.apiBase,this.scope=c.scope,this.accessToken=null,this.toQueryString=d.toQueryString}return h.prototype.api=function(a,b,c,d,g){var h=e.defer();return f({url:this.apiBase+a,method:b?b:"GET",params:c,data:d,headers:g}).success(function(a){h.resolve(a)}).error(function(a){h.reject(a)}),h.promise},h.prototype.search=function(a,b,c){return c=c||{},c.q=a,c.type=b,this.api("/search","GET",c)},h.prototype.getAlbum=function(a){return a=-1===a.indexOf("spotify:")?a:a.split(":")[2],this.api("/albums/"+a)},h.prototype.getAlbums=function(a){return a=b.isString(a)?a.split(","):a,b.forEach(a,function(b,c){a[c]=b.indexOf("spotify:")>-1?b.split(":")[2]:b}),this.api("/albums","GET",{ids:a?a.toString():""})},h.prototype.getAlbumTracks=function(a,b){return a=-1===a.indexOf("spotify:")?a:a.split(":")[2],this.api("/albums/"+a+"/tracks","GET",b)},h.prototype.getArtist=function(a){return a=-1===a.indexOf("spotify:")?a:a.split(":")[2],this.api("/artists/"+a)},h.prototype.getArtists=function(a){return a=b.isString(a)?a.split(","):a,b.forEach(a,function(b,c){a[c]=b.indexOf("spotify:")>-1?b.split(":")[2]:b}),this.api("/artists/","GET",{ids:a?a.toString():""})},h.prototype.getArtistAlbums=function(a,b){return a=-1===a.indexOf("spotify:")?a:a.split(":")[2],this.api("/artists/"+a+"/albums","GET",b)},h.prototype.getArtistTopTracks=function(a,b){return a=-1===a.indexOf("spotify:")?a:a.split(":")[2],this.api("/artists/"+a+"/top-tracks","GET",{country:b})},h.prototype.getRelatedArtists=function(a){return a=-1===a.indexOf("spotify:")?a:a.split(":")[2],this.api("/artists/"+a+"/related-artists")},h.prototype.getTrack=function(a){return a=-1===a.indexOf("spotify:")?a:a.split(":")[2],this.api("/tracks/"+a)},h.prototype.getTracks=function(a){return a=b.isString(a)?a.split(","):a,b.forEach(a,function(b,c){a[c]=b.indexOf("spotify:")>-1?b.split(":")[2]:b}),this.api("/tracks/","GET",{ids:a?a.toString():""})},h.prototype.getUserPlaylists=function(a,b){return this.api("/users/"+a+"/playlists","GET",b,null,{Authorization:"Bearer "+this.authToken})},h.prototype.getPlaylist=function(a,b,c){return this.api("/users/"+a+"/playlists/"+b,"GET",c,null,{Authorization:"Bearer "+this.authToken})},h.prototype.getPlaylistTracks=function(a,b,c){return this.api("/users/"+a+"/playlists/"+b+"/tracks","GET",c,null,{Authorization:"Bearer "+this.authToken})},h.prototype.createPlaylist=function(a,b){return this.api("/users/"+a+"/playlists","POST",null,b,{Authorization:"Bearer "+this.authToken,"Content-Type":"application/json"})},h.prototype.addPlaylistTracks=function(a,c,d,e){return d=b.isArray(d)?d:d.split(","),b.forEach(d,function(a,b){d[b]=-1===a.indexOf("spotify:")?"spotify:track:"+a:a}),this.api("/users/"+a+"/playlists/"+c+"/tracks","POST",{uris:d.toString(),position:e?e.position:null},null,{Authorization:"Bearer "+this.authToken,"Content-Type":"application/json"})},h.prototype.removePlaylistTracks=function(a,c,d){d=b.isArray(d)?d:d.split(",");var e;return b.forEach(d,function(a,b){e=d[b],d[b]={uri:-1===e.indexOf("spotify:")?"spotify:track:"+e:e}}),this.api("/users/"+a+"/playlists/"+c+"/tracks","DELETE",null,{tracks:d},{Authorization:"Bearer "+this.authToken,"Content-Type":"application/json"})},h.prototype.replacePlaylistTracks=function(a,c,d){d=b.isArray(d)?d:d.split(",");var e;return b.forEach(d,function(a,b){e=d[b],d[b]=-1===e.indexOf("spotify:")?"spotify:track:"+e:e}),this.api("/users/"+a+"/playlists/"+c+"/tracks","PUT",{uris:d.toString()},null,{Authorization:"Bearer "+this.authToken,"Content-Type":"application/json"})},h.prototype.updatePlaylistDetails=function(a,b,c){return this.api("/users/"+a+"/playlists/"+b,"PUT",null,c,{Authorization:"Bearer "+this.authToken,"Content-Type":"application/json"})},h.prototype.getUser=function(a){return this.api("/users/"+a)},h.prototype.getCurrentUser=function(){return this.api("/me","GET",null,null,{Authorization:"Bearer "+this.authToken})},h.prototype.getSavedUserTracks=function(a){return this.api("/me/tracks","GET",a,null,{Authorization:"Bearer "+this.authToken})},h.prototype.userTracksContains=function(a){return a=b.isString(a)?a.split(","):a,b.forEach(a,function(b,c){a[c]=b.indexOf("spotify:")>-1?b.split(":")[2]:b}),this.api("/me/tracks/contains","GET",{ids:a.toString()},null,{Authorization:"Bearer "+this.authToken})},h.prototype.saveUserTracks=function(a){return a=b.isString(a)?a.split(","):a,b.forEach(a,function(b,c){a[c]=b.indexOf("spotify:")>-1?b.split(":")[2]:b}),this.api("/me/tracks","PUT",{ids:a.toString()},null,{Authorization:"Bearer "+this.authToken})},h.prototype.removeUserTracks=function(a){return a=b.isString(a)?a.split(","):a,b.forEach(a,function(b,c){a[c]=b.indexOf("spotify:")>-1?b.split(":")[2]:b}),this.api("/me/tracks","DELETE",{ids:a.toString()},null,{Authorization:"Bearer "+this.authToken,"Content-Type":"application/json"})},h.prototype.setAuthToken=function(a){return this.authToken=a,this.authToken},h.prototype.login=function(){function b(a){l&&l.close(),d.setAuthToken(a.data),c.resolve(a.data)}var c=e.defer(),d=this,f=400,h=500,i=screen.width/2-f/2,j=screen.height/2-h/2,k={client_id:this.clientId,redirect_uri:this.redirectUri,scope:this.scope||"",response_type:"token"},l=a.open("https://accounts.spotify.com/authorize?"+this.toQueryString(k),"Spotify","menubar=no,location=no,resizable=no,scrollbars=no,status=no,width="+f+",height="+h+",top="+j+",left="+i);return g.addEventListener("message",b,!1),c.promise},new h}]})}(window,angular); \ No newline at end of file diff --git a/src/angular-spotify.js b/src/angular-spotify.js index b44fa4a..05911cf 100644 --- a/src/angular-spotify.js +++ b/src/angular-spotify.js @@ -344,6 +344,10 @@ }; NgSpotify.prototype.removeUserTracks = function (tracks) { + tracks = angular.isString(tracks) ? tracks.split(',') : tracks; + angular.forEach(tracks, function (value, index) { + tracks[index] = value.indexOf('spotify:') > -1 ? value.split(':')[2] : value; + }); return this.api('/me/tracks', 'DELETE', { ids: tracks.toString() }, null, { diff --git a/test/spec/angular-spotify.spec.js b/test/spec/angular-spotify.spec.js index e365744..59d08be 100644 --- a/test/spec/angular-spotify.spec.js +++ b/test/spec/angular-spotify.spec.js @@ -1349,6 +1349,22 @@ describe('angular-spotify', function () { }); }); + it('should be able to pass Spotify URIs', function () { + spyOn(Spotify, 'api'); + + Spotify.setAuthToken('TESTING'); + + Spotify.removeUserTracks(['spotify:track:0udZHhCi7p1YzMlvI4fXoK','spotify:track:3SF5puV5eb6bgRSxBeMOk9']); + + expect(Spotify.api).toHaveBeenCalled(); + expect(Spotify.api).toHaveBeenCalledWith('/me/tracks', 'DELETE', { + ids: '0udZHhCi7p1YzMlvI4fXoK,3SF5puV5eb6bgRSxBeMOk9' + }, null, { + 'Authorization': 'Bearer TESTING', + 'Content-Type': 'application/json' + }); + }); + }); });