From 1a0497acc65d0a72397e2cc730d715120a7a038e Mon Sep 17 00:00:00 2001 From: Ed Moore Date: Wed, 13 Apr 2016 15:37:36 +0800 Subject: [PATCH 01/13] Updated API --- src/angular-spotify.js | 275 +++++++--- test/spec/angular-spotify.spec.js | 883 ++++++++++++++++-------------- 2 files changed, 659 insertions(+), 499 deletions(-) diff --git a/src/angular-spotify.js b/src/angular-spotify.js index cda6dc4..f06b455 100644 --- a/src/angular-spotify.js +++ b/src/angular-spotify.js @@ -79,15 +79,17 @@ } NgSpotify.prototype = { - api: function (endpoint, method, params, data, headers) { + api: function (endpoint, options) { var deferred = $q.defer(); + options = options || {}; + options.method = options.method || 'GET'; $http({ url: this.apiBase + endpoint, - method: method ? method : 'GET', - params: params, - data: data, - headers: headers, + method: options.method, + params: options.params, + data: options.data, + headers: options.headers, withCredentials: false }) .success(function (data) { @@ -132,8 +134,10 @@ angular.forEach(albums, function (value, index) { albums[index] = value.indexOf('spotify:') > -1 ? value.split(':')[2] : value; }); - return this.api('/albums', 'GET', { - ids: albums ? albums.toString() : '' + return this.api('/albums', { + params: { + ids: albums ? albums.toString() : '' + } }); }, @@ -143,8 +147,8 @@ */ getAlbumTracks: function (album, options) { album = album.indexOf('spotify:') === -1 ? album : album.split(':')[2]; - - return this.api('/albums/' + album + '/tracks', 'GET', options); + var o = options && { params: options }; + return this.api('/albums/' + album + '/tracks', o); }, @@ -169,16 +173,18 @@ angular.forEach(artists, function (value, index) { artists[index] = value.indexOf('spotify:') > -1 ? value.split(':')[2] : value; }); - return this.api('/artists/', 'GET', { - ids: artists ? artists.toString() : '' + return this.api('/artists/', { + params: { + ids: artists ? artists.toString() : '' + } }); }, //Artist Albums getArtistAlbums: function (artist, options) { artist = artist.indexOf('spotify:') === -1 ? artist : artist.split(':')[2]; - - return this.api('/artists/' + artist + '/albums', 'GET', options); + var o = options && { params: options }; + return this.api('/artists/' + artist + '/albums', o); }, /** @@ -188,8 +194,10 @@ getArtistTopTracks: function (artist, country) { artist = artist.indexOf('spotify:') === -1 ? artist : artist.split(':')[2]; - return this.api('/artists/' + artist + '/top-tracks', 'GET', { - country: country + return this.api('/artists/' + artist + '/top-tracks', { + params: { + country: country + } }); }, @@ -204,31 +212,48 @@ ====================== Browse ===================== */ getFeaturedPlaylists: function (options) { - return this.api('/browse/featured-playlists', 'GET', options, null, this._auth()); + return this.api('/browse/featured-playlists', { + params: options, + headers: this._auth() + }); }, getNewReleases: function (options) { - return this.api('/browse/new-releases', 'GET', options, null, this._auth()); + return this.api('/browse/new-releases', { + params: options, + headers: this._auth() + }); }, getCategories: function (options) { - return this.api('/browse/categories', 'GET', options, null, this._auth()); + var o = { headers: this._auth() }; + if (options) { o.params = options; } + return this.api('/browse/categories', o); }, getCategory: function (category_id, options) { - return this.api('/browse/categories/' + category_id, 'GET', options, null, this._auth()); + var o = { headers: this._auth() }; + if (options) { o.params = options; } + return this.api('/browse/categories/' + category_id, o); }, getCategoryPlaylists: function (category_id, options) { - return this.api('/browse/categories/' + category_id + '/playlists', 'GET', options, null, this._auth()); + var o = { headers: this._auth() }; + if (options) { o.params = options; } + return this.api('/browse/categories/' + category_id + '/playlists', o); }, getRecommendations: function (options) { - return this.api('/recommendations', 'GET', options, null, this._auth()); + return this.api('/recommendations', { + params: options, + headers: this._auth() + }); }, getAvailableGenreSeeds: function () { - return this.api('/recommendations/available-genre-seeds', 'GET', null, null, this._auth()); + return this.api('/recommendations/available-genre-seeds', { + headers: this._auth() + }); }, @@ -238,35 +263,59 @@ following: function (type, options) { options = options || {}; options.type = type; - return this.api('/me/following', 'GET', options, null, this._auth()); + return this.api('/me/following', { + params: options, + headers: this._auth() + }); }, follow: function (type, ids) { - return this.api('/me/following', 'PUT', { type: type, ids: ids }, null, this._auth()); + return this.api('/me/following', { + method: 'PUT', + params: { type: type, ids: ids }, + headers: this._auth() + }); }, unfollow: function (type, ids) { - return this.api('/me/following', 'DELETE', { type: type, ids: ids }, null, this._auth()); + return this.api('/me/following', { + method: 'DELETE', + params: { type: type, ids: ids }, + headers: this._auth() + }); }, userFollowingContains: function (type, ids) { - return this.api('/me/following/contains', 'GET', { type: type, ids: ids }, null, this._auth()); + return this.api('/me/following/contains', { + params: { type: type, ids: ids }, + headers: this._auth() + }); }, followPlaylist: function (userId, playlistId, isPublic) { - return this.api('/users/' + userId + '/playlists/' + playlistId + '/followers', 'PUT', null, { - public: isPublic || null - }, this._auth(true)); + return this.api('/users/' + userId + '/playlists/' + playlistId + '/followers', { + method: 'PUT', + data: { + public: isPublic || null + }, + headers: this._auth(true) + }); }, unfollowPlaylist: function (userId, playlistId) { - return this.api('/users/' + userId + '/playlists/' + playlistId + '/followers', 'DELETE', null, null, this._auth()); + return this.api('/users/' + userId + '/playlists/' + playlistId + '/followers', { + method: 'DELETE', + headers: this._auth() + }); }, playlistFollowingContains: function(userId, playlistId, ids) { - return this.api('/users/' + userId + '/playlists/' + playlistId + '/followers/contains', 'GET', { - ids: ids.toString() - }, null, this._auth()); + return this.api('/users/' + userId + '/playlists/' + playlistId + '/followers/contains', { + params: { + ids: ids.toString() + }, + headers: this._auth() + }); }, @@ -274,7 +323,9 @@ ====================== Library ===================== */ getSavedUserTracks: function (options) { - return this.api('/me/tracks', 'GET', options, null, this._auth()); + var o = { headers: this._auth() }; + if (options) { o.params = options; } + return this.api('/me/tracks', o); }, userTracksContains: function (tracks) { @@ -282,9 +333,12 @@ angular.forEach(tracks, function (value, index) { tracks[index] = value.indexOf('spotify:') > -1 ? value.split(':')[2] : value; }); - return this.api('/me/tracks/contains', 'GET', { - ids: tracks.toString() - }, null, this._auth()); + return this.api('/me/tracks/contains', { + params: { + ids: tracks.toString() + }, + headers: this._auth() + }); }, saveUserTracks: function (tracks) { @@ -292,9 +346,13 @@ angular.forEach(tracks, function (value, index) { tracks[index] = value.indexOf('spotify:') > -1 ? value.split(':')[2] : value; }); - return this.api('/me/tracks', 'PUT', { - ids: tracks.toString() - }, null, this._auth()); + return this.api('/me/tracks', { + method: 'PUT', + params: { + ids: tracks.toString() + }, + headers: this._auth() + }); }, removeUserTracks: function (tracks) { @@ -302,9 +360,13 @@ 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, this._auth(true)); + return this.api('/me/tracks', { + method: 'DELETE', + params: { + ids: tracks.toString() + }, + headers: this._auth(true) + }); }, saveUserAlbums: function (albums) { @@ -312,13 +374,19 @@ angular.forEach(albums, function (value, index) { albums[index] = value.indexOf('spotify:') > -1 ? value.split(':')[2] : value; }); - return this.api('/me/albums', 'PUT', { - ids: albums.toString() - }, null, this._auth()); + return this.api('/me/albums', { + method: 'PUT', + params: { + ids: albums.toString() + }, + headers: this._auth() + }); }, getSavedUserAlbums: function (options) { - return this.api('/me/albums', 'GET', options, null, this._auth()); + var o = { headers: this._auth() }; + if (options) { o.params = options; } + return this.api('/me/albums', o); }, removeUserAlbums: function (albums) { @@ -326,9 +394,13 @@ angular.forEach(albums, function (value, index) { albums[index] = value.indexOf('spotify:') > -1 ? value.split(':')[2] : value; }); - return this.api('/me/albums', 'DELETE', { - ids: albums.toString() - }, null, this._auth(true)); + return this.api('/me/albums', { + method: 'DELETE', + params: { + ids: albums.toString() + }, + headers: this._auth(true) + }); }, userAlbumsContains: function (albums) { @@ -336,9 +408,12 @@ angular.forEach(albums, function (value, index) { albums[index] = value.indexOf('spotify:') > -1 ? value.split(':')[2] : value; }); - return this.api('/me/albums/contains', 'GET', { - ids: albums.toString() - }, null, this._auth()); + return this.api('/me/albums/contains', { + params: { + ids: albums.toString() + }, + headers: this._auth() + }); }, @@ -346,13 +421,15 @@ ====================== Personalization ===================== */ getUserTopArtists: function (options) { - options = options || {}; - return this.api('/me/top/artists', 'GET', options, null, this._auth()); + var o = { headers: this._auth() }; + if (options) { o.params = options; } + return this.api('/me/top/artists', o); }, getUserTopTracks: function (options) { - options = options || {}; - return this.api('/me/top/tracks', 'GET', options, null, this._auth()); + var o = { headers: this._auth() }; + if (options) { o.params = options; } + return this.api('/me/top/tracks', o); }, @@ -360,21 +437,29 @@ ====================== Playlists ===================== */ getUserPlaylists: function (userId, options) { - return this.api('/users/' + userId + '/playlists', 'GET', options, null, { - 'Authorization': 'Bearer ' + this.authToken - }); + var o = { headers: this._auth() }; + if (options) { o.params = options; } + return this.api('/users/' + userId + '/playlists', o); }, getPlaylist: function (userId, playlistId, options) { - return this.api('/users/' + userId + '/playlists/' + playlistId, 'GET', options, null, this._auth()); + var o = { headers: this._auth() }; + if (options) { o.params = options; } + return this.api('/users/' + userId + '/playlists/' + playlistId, o); }, getPlaylistTracks: function (userId, playlistId, options) { - return this.api('/users/' + userId + '/playlists/' + playlistId + '/tracks', 'GET', options, null, this._auth()); + var o = { headers: this._auth() }; + if (options) { o.params = options; } + return this.api('/users/' + userId + '/playlists/' + playlistId + '/tracks', o); }, createPlaylist: function (userId, options) { - return this.api('/users/' + userId + '/playlists', 'POST', null, options, this._auth(true)); + return this.api('/users/' + userId + '/playlists', { + method: 'POST', + data: options, + headers: this._auth(true) + }); }, addPlaylistTracks: function (userId, playlistId, tracks, options) { @@ -382,10 +467,14 @@ angular.forEach(tracks, function (value, index) { tracks[index] = value.indexOf('spotify:') === -1 ? 'spotify:track:' + value : value; }); - return this.api('/users/' + userId + '/playlists/' + playlistId + '/tracks', 'POST', { - uris: tracks.toString(), - position: options ? options.position : null - }, null, this._auth(true)); + return this.api('/users/' + userId + '/playlists/' + playlistId + '/tracks', { + method: 'POST', + params: { + uris: tracks.toString(), + position: options ? options.position : null + }, + headers: this._auth(true) + }); }, removePlaylistTracks: function (userId, playlistId, tracks) { @@ -397,13 +486,21 @@ uri: track.indexOf('spotify:') === -1 ? 'spotify:track:' + track : track }; }); - return this.api('/users/' + userId + '/playlists/' + playlistId + '/tracks', 'DELETE', null, { - tracks: tracks - }, this._auth(true)); + return this.api('/users/' + userId + '/playlists/' + playlistId + '/tracks', { + method: 'DELETE', + data: { + tracks: tracks + }, + headers: this._auth(true) + }); }, reorderPlaylistTracks: function (userId, playlistId, options) { - return this.api('/users/' + userId + '/playlists/' + playlistId + '/tracks', 'PUT', null, options, this._auth(true)); + return this.api('/users/' + userId + '/playlists/' + playlistId + '/tracks', { + method: 'PUT', + data: options, + headers: this._auth(true) + }); }, replacePlaylistTracks: function (userId, playlistId, tracks) { @@ -413,13 +510,21 @@ track = tracks[index]; tracks[index] = track.indexOf('spotify:') === -1 ? 'spotify:track:' + track : track; }); - return this.api('/users/' + userId + '/playlists/' + playlistId + '/tracks', 'PUT', { - uris: tracks.toString() - }, null, this._auth(true)); + return this.api('/users/' + userId + '/playlists/' + playlistId + '/tracks', { + method: 'PUT', + params: { + uris: tracks.toString() + }, + headers: this._auth(true) + }); }, updatePlaylistDetails: function (userId, playlistId, options) { - return this.api('/users/' + userId + '/playlists/' + playlistId, 'PUT', null, options, this._auth(true)); + return this.api('/users/' + userId + '/playlists/' + playlistId, { + method: 'PUT', + data: options, + headers: this._auth(true) + }); }, /** @@ -431,7 +536,9 @@ }, getCurrentUser: function () { - return this.api('/me', 'GET', null, null, this._auth()); + return this.api('/me', { + headers: this._auth() + }); }, @@ -446,7 +553,9 @@ options.q = q; options.type = type; - return this.api('/search', 'GET', options); + return this.api('/search', { + params: options + }); }, @@ -464,8 +573,10 @@ angular.forEach(tracks, function (value, index) { tracks[index] = value.indexOf('spotify:') > -1 ? value.split(':')[2] : value; }); - return this.api('/tracks/', 'GET', { - ids: tracks ? tracks.toString() : '' + return this.api('/tracks/', { + params: { + ids: tracks ? tracks.toString() : '' + } }); }, @@ -479,8 +590,10 @@ angular.forEach(tracks, function (value, index) { tracks[index] = value.indexOf('spotify:') > -1 ? value.split(':')[2] : value; }); - return this.api('/audio-features/', 'GET', { - ids: tracks ? tracks.toString() : '' + return this.api('/audio-features/', { + params: { + ids: tracks ? tracks.toString() : '' + } }); }, diff --git a/test/spec/angular-spotify.spec.js b/test/spec/angular-spotify.spec.js index 941159b..b1b7c20 100644 --- a/test/spec/angular-spotify.spec.js +++ b/test/spec/angular-spotify.spec.js @@ -85,130 +85,10 @@ describe('angular-spotify', function () { Spotify = _Spotify_; })); - it('should be defined', function () { - expect(Spotify).toBeDefined(); - }); - it('should be an object', function () { expect(typeof Spotify).toBe('object'); }); - it('should have a method api()', function () { - expect(Spotify.api).toBeDefined(); - }); - - it('should have a method search()', function () { - expect(Spotify.search).toBeDefined(); - }); - - it('should have a method getAlbum()', function () { - expect(Spotify.getAlbum).toBeDefined(); - }); - - it('should have a method getAlbums()', function () { - expect(Spotify.getAlbums).toBeDefined(); - }); - - it('should have a method getAlbumTracks()', function () { - expect(Spotify.getAlbumTracks).toBeDefined(); - }); - - it('should have a method getArtist()', function () { - expect(Spotify.getArtist).toBeDefined(); - }); - - it('should have a method getArtists()', function () { - expect(Spotify.getArtists).toBeDefined(); - }); - - it('should have a method getArtistAlbums()', function () { - expect(Spotify.getArtistAlbums).toBeDefined(); - }); - - it('should have a method getArtistTopTracks()', function () { - expect(Spotify.getArtistTopTracks).toBeDefined(); - }); - - it('should have a method getRelatedArtists()', function () { - expect(Spotify.getRelatedArtists).toBeDefined(); - }); - - it('should have a method getTrack()', function () { - expect(Spotify.getTrack).toBeDefined(); - }); - - it('should have a method getTracks()', function () { - expect(Spotify.getTracks).toBeDefined(); - }); - - it('should have a method getUserPlaylists()', function () { - expect(Spotify.getUserPlaylists).toBeDefined(); - }); - - it('should have a method getPlaylist()', function () { - expect(Spotify.getPlaylist).toBeDefined(); - }); - - it('should have a method getPlaylistTracks()', function () { - expect(Spotify.getPlaylistTracks).toBeDefined(); - }); - - it('should have a method createPlaylist()', function () { - expect(Spotify.createPlaylist).toBeDefined(); - }); - - it('should have a method addPlaylistTracks()', function () { - expect(Spotify.addPlaylistTracks).toBeDefined(); - }); - - it('should have a method removePlaylistTracks()', function () { - expect(Spotify.removePlaylistTracks).toBeDefined(); - }); - - it('should have a method reorderPlaylistTracks()', function () { - expect(Spotify.reorderPlaylistTracks).toBeDefined(); - }); - - it('should have a method replacePlaylistTracks()', function () { - expect(Spotify.replacePlaylistTracks).toBeDefined(); - }); - - it('should have a method updatePlaylistDetails()', function () { - expect(Spotify.updatePlaylistDetails).toBeDefined(); - }); - - it('should have a method getUser()', function () { - expect(Spotify.getUser).toBeDefined(); - }); - - it('should have a method getCurrentUser()', function () { - expect(Spotify.getCurrentUser).toBeDefined(); - }); - - it('should have a method getSavedUserTracks()', function () { - expect(Spotify.getSavedUserTracks).toBeDefined(); - }); - - it('should have a method userTracksContains()', function () { - expect(Spotify.userTracksContains).toBeDefined(); - }); - - it('should have a method saveUserTracks()', function () { - expect(Spotify.saveUserTracks).toBeDefined(); - }); - - it('should have a method removeUserTracks()', function () { - expect(Spotify.removeUserTracks).toBeDefined(); - }); - - it('should have a method setAuthToken()', function () { - expect(Spotify.setAuthToken).toBeDefined(); - }); - - it('should have a method login()', function () { - expect(Spotify.login).toBeDefined(); - }); - it('should set the AuthToken', function () { expect(Spotify.setAuthToken('ABCDEFGHIJKLMNOP')).toBe('ABCDEFGHIJKLMNOP'); }); @@ -217,55 +97,6 @@ describe('angular-spotify', function () { expect(Spotify.toQueryString({a: 't', b: 4, c: 'q'})).toBe('a=t&b=4&c=q'); }); - it('should have a method getFeaturedPlaylists()', function () { - expect(Spotify.getFeaturedPlaylists).toBeDefined(); - }); - - it('should have a method getNewReleases()', function () { - expect(Spotify.getNewReleases).toBeDefined(); - }); - - it('should have a method following()', function () { - expect(Spotify.following).toBeDefined(); - }); - - it('should have a method follow()', function () { - expect(Spotify.follow).toBeDefined(); - }); - - it('should have a method unfollow()', function () { - expect(Spotify.unfollow).toBeDefined(); - }); - - it('should have a method userFollowingContains()', function () { - expect(Spotify.userFollowingContains).toBeDefined(); - }); - - it('should have a method followPlaylist()', function () { - expect(Spotify.followPlaylist).toBeDefined(); - }); - - it('should have a method unfollowPlaylist()', function () { - expect(Spotify.unfollowPlaylist).toBeDefined(); - }); - - it('should have a method playlistFollowingContains()', function () { - expect(Spotify.playlistFollowingContains).toBeDefined(); - }); - - it('should have a method getCategories()', function () { - expect(Spotify.getCategories).toBeDefined(); - }); - - it('should have a method getCategory()', function () { - expect(Spotify.getCategory).toBeDefined(); - }); - - it('should have a method getCategoryPlaylists()', function () { - expect(Spotify.getCategoryPlaylists).toBeDefined(); - }); - - describe('Spotify.api', function () { var $httpBackend; var Spotify; @@ -288,9 +119,11 @@ describe('angular-spotify', function () { ); var result; - Spotify.api('/search', 'GET', { - q: 'Nirvana', - type: 'artist' + Spotify.api('/search', { + params: { + q: 'Nirvana', + type: 'artist' + } }).then(function (data) { result = data; }); @@ -307,9 +140,12 @@ describe('angular-spotify', function () { }).respond({}); var result; - Spotify.api('/users/wizzler/playlists', 'POST', null, { - name: 'TESTING', - public: false + Spotify.api('/users/wizzler/playlists', { + method: 'POST', + data: { + name: 'TESTING', + public: false + } }).then(function (data) { result = data; }); @@ -328,11 +164,15 @@ describe('angular-spotify', function () { }).respond({}); var result; - Spotify.api('/users/wizzler/playlists', 'POST', null, { - name: 'TESTING', - public: false - }, { - 'Authorization': 'Bearer TESTING' + Spotify.api('/users/wizzler/playlists', { + method: 'POST', + data: { + name: 'TESTING', + public: false + }, + headers: { + 'Authorization': 'Bearer TESTING' + } }).then(function (data) { result = data; }); @@ -363,9 +203,11 @@ describe('angular-spotify', function () { Spotify.search('Nirvana', 'artist'); - expect(Spotify.api).toHaveBeenCalledWith('/search', 'GET', { - q: 'Nirvana', - type: 'artist' + expect(Spotify.api).toHaveBeenCalledWith('/search', { + params: { + q: 'Nirvana', + type: 'artist' + } }); }); @@ -491,8 +333,10 @@ describe('angular-spotify', function () { Spotify.getAlbums('spotify:album:41MnTivkwTO3UUJ8DrqEJJ,spotify:album:6JWc4iAiJ9FjyK0B59ABb4,spotify:album:6UXCm6bOO4gFlDQZV5yL37'); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/albums', 'GET', { - ids: '41MnTivkwTO3UUJ8DrqEJJ,6JWc4iAiJ9FjyK0B59ABb4,6UXCm6bOO4gFlDQZV5yL37' + expect(Spotify.api).toHaveBeenCalledWith('/albums', { + params: { + ids: '41MnTivkwTO3UUJ8DrqEJJ,6JWc4iAiJ9FjyK0B59ABb4,6UXCm6bOO4gFlDQZV5yL37' + } }); }); @@ -543,7 +387,19 @@ describe('angular-spotify', function () { Spotify.getAlbumTracks('spotify:album:0sNOF9WDwhWunNAHPD3Baj'); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/albums/0sNOF9WDwhWunNAHPD3Baj/tracks', 'GET', undefined); + expect(Spotify.api).toHaveBeenCalledWith('/albums/0sNOF9WDwhWunNAHPD3Baj/tracks', undefined); + }); + + it('should resolve with options', function () { + spyOn(Spotify, 'api'); + Spotify.getAlbumTracks('spotify:album:0sNOF9WDwhWunNAHPD3Baj', { limit: 20 }); + + expect(Spotify.api).toHaveBeenCalled(); + expect(Spotify.api).toHaveBeenCalledWith('/albums/0sNOF9WDwhWunNAHPD3Baj/tracks', { + params: { + limit: 20 + } + }); }); it('should resolve to an object of album tracks', function () { @@ -667,8 +523,10 @@ describe('angular-spotify', function () { Spotify.getArtists('spotify:artist:0oSGxfWSnnOXhD2fKuz2Gy,spotify:artist:3dBVyJ7JuOMt4GE9607Qin'); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/artists/', 'GET', { - ids: '0oSGxfWSnnOXhD2fKuz2Gy,3dBVyJ7JuOMt4GE9607Qin' + expect(Spotify.api).toHaveBeenCalledWith('/artists/', { + params: { + ids: '0oSGxfWSnnOXhD2fKuz2Gy,3dBVyJ7JuOMt4GE9607Qin' + } }); }); @@ -705,7 +563,19 @@ describe('angular-spotify', function () { Spotify.getArtistAlbums('spotify:artist:0LcJLqbBmaGUft1e9Mm8HV'); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/artists/0LcJLqbBmaGUft1e9Mm8HV/albums', 'GET', undefined); + expect(Spotify.api).toHaveBeenCalledWith('/artists/0LcJLqbBmaGUft1e9Mm8HV/albums', undefined); + }); + + it('should resolve with options', function () { + spyOn(Spotify, 'api'); + + Spotify.getArtistAlbums('spotify:artist:0LcJLqbBmaGUft1e9Mm8HV', { limit: 20 }); + expect(Spotify.api).toHaveBeenCalled(); + expect(Spotify.api).toHaveBeenCalledWith('/artists/0LcJLqbBmaGUft1e9Mm8HV/albums', { + params: { + limit: 20 + } + }); }); it('should resolve to an array of artist albums', function () { @@ -756,8 +626,10 @@ describe('angular-spotify', function () { Spotify.getArtistTopTracks('spotify:artist:0LcJLqbBmaGUft1e9Mm8HV', 'AU'); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/artists/0LcJLqbBmaGUft1e9Mm8HV/top-tracks', 'GET', { - country: 'AU' + expect(Spotify.api).toHaveBeenCalledWith('/artists/0LcJLqbBmaGUft1e9Mm8HV/top-tracks', { + params: { + country: 'AU' + } }); }); @@ -964,8 +836,10 @@ describe('angular-spotify', function () { Spotify.getTracks('spotify:track:0eGsygTp906u18L0Oimnem,spotify:track:1lDWb6b6ieDQ2xT7ewTC3G'); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/tracks/', 'GET', { - ids: '0eGsygTp906u18L0Oimnem,1lDWb6b6ieDQ2xT7ewTC3G' + expect(Spotify.api).toHaveBeenCalledWith('/tracks/', { + params: { + ids: '0eGsygTp906u18L0Oimnem,1lDWb6b6ieDQ2xT7ewTC3G' + } }); }); @@ -1072,8 +946,10 @@ describe('angular-spotify', function () { Spotify.getTracksAudioFeatures('spotify:track:0eGsygTp906u18L0Oimnem,spotify:track:1lDWb6b6ieDQ2xT7ewTC3G'); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/audio-features/', 'GET', { - ids: '0eGsygTp906u18L0Oimnem,1lDWb6b6ieDQ2xT7ewTC3G' + expect(Spotify.api).toHaveBeenCalledWith('/audio-features/', { + params: { + ids: '0eGsygTp906u18L0Oimnem,1lDWb6b6ieDQ2xT7ewTC3G' + } }); }); @@ -1114,8 +990,10 @@ describe('angular-spotify', function () { Spotify.getUserPlaylists('wizzler'); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/users/wizzler/playlists', 'GET', undefined, null, { - 'Authorization': 'Bearer TESTING' + expect(Spotify.api).toHaveBeenCalledWith('/users/wizzler/playlists', { + headers: { + 'Authorization': 'Bearer TESTING' + } }); }); @@ -1131,8 +1009,10 @@ describe('angular-spotify', function () { Spotify.getPlaylist('triple.j.abc', '73ppZmbaAS2aW9hmDTTDcb'); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/users/triple.j.abc/playlists/73ppZmbaAS2aW9hmDTTDcb', 'GET', undefined, null, { - 'Authorization': 'Bearer TESTING' + expect(Spotify.api).toHaveBeenCalledWith('/users/triple.j.abc/playlists/73ppZmbaAS2aW9hmDTTDcb', { + headers: { + 'Authorization': 'Bearer TESTING' + } }); }); @@ -1148,8 +1028,10 @@ describe('angular-spotify', function () { Spotify.getPlaylistTracks('triple.j.abc', '73ppZmbaAS2aW9hmDTTDcb'); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/users/triple.j.abc/playlists/73ppZmbaAS2aW9hmDTTDcb/tracks', 'GET', undefined, null, { - 'Authorization': 'Bearer TESTING' + expect(Spotify.api).toHaveBeenCalledWith('/users/triple.j.abc/playlists/73ppZmbaAS2aW9hmDTTDcb/tracks', { + headers: { + 'Authorization': 'Bearer TESTING' + } }); }); @@ -1167,11 +1049,15 @@ describe('angular-spotify', function () { }); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/users/1176458919/playlists', 'POST', null, { - name: 'Awesome Mix Vol. 1' - }, { - 'Authorization': 'Bearer TESTING', - 'Content-Type': 'application/json' + expect(Spotify.api).toHaveBeenCalledWith('/users/1176458919/playlists', { + method: 'POST', + data:{ + name: 'Awesome Mix Vol. 1' + }, + headers: { + 'Authorization': 'Bearer TESTING', + 'Content-Type': 'application/json' + } }); }); @@ -1191,12 +1077,16 @@ describe('angular-spotify', function () { ]); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/users/triple.j.abc/playlists/73ppZmbaAS2aW9hmDTTDcb/tracks', 'POST', { - uris: 'spotify:track:5LwukQO2fCx35GUUN6d6NW,spotify:track:4w8CsAnzn0lXJxYlAuCtCW,spotify:track:2Foc5Q5nqNiosCNqttzHof', - position: null - }, null, { - 'Authorization': 'Bearer TESTING', - 'Content-Type': 'application/json' + expect(Spotify.api).toHaveBeenCalledWith('/users/triple.j.abc/playlists/73ppZmbaAS2aW9hmDTTDcb/tracks', { + method: 'POST', + params: { + uris: 'spotify:track:5LwukQO2fCx35GUUN6d6NW,spotify:track:4w8CsAnzn0lXJxYlAuCtCW,spotify:track:2Foc5Q5nqNiosCNqttzHof', + position: null + }, + headers: { + 'Authorization': 'Bearer TESTING', + 'Content-Type': 'application/json' + } }); }); @@ -1212,12 +1102,16 @@ describe('angular-spotify', function () { ]); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/users/triple.j.abc/playlists/73ppZmbaAS2aW9hmDTTDcb/tracks', 'POST', { - uris: 'spotify:track:5LwukQO2fCx35GUUN6d6NW,spotify:track:4w8CsAnzn0lXJxYlAuCtCW,spotify:track:2Foc5Q5nqNiosCNqttzHof', - position: null - }, null, { - 'Authorization': 'Bearer TESTING', - 'Content-Type': 'application/json' + expect(Spotify.api).toHaveBeenCalledWith('/users/triple.j.abc/playlists/73ppZmbaAS2aW9hmDTTDcb/tracks', { + method: 'POST', + params: { + uris: 'spotify:track:5LwukQO2fCx35GUUN6d6NW,spotify:track:4w8CsAnzn0lXJxYlAuCtCW,spotify:track:2Foc5Q5nqNiosCNqttzHof', + position: null + }, + headers: { + 'Authorization': 'Bearer TESTING', + 'Content-Type': 'application/json' + } }); }); @@ -1236,15 +1130,19 @@ describe('angular-spotify', function () { ]); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/users/triple.j.abc/playlists/73ppZmbaAS2aW9hmDTTDcb/tracks', 'DELETE', null, { - tracks: [ - {uri: 'spotify:track:5LwukQO2fCx35GUUN6d6NW'}, - {uri: 'spotify:track:4w8CsAnzn0lXJxYlAuCtCW'}, - {uri: 'spotify:track:2Foc5Q5nqNiosCNqttzHof'} - ] - }, { - 'Authorization': 'Bearer TESTING', - 'Content-Type': 'application/json' + expect(Spotify.api).toHaveBeenCalledWith('/users/triple.j.abc/playlists/73ppZmbaAS2aW9hmDTTDcb/tracks', { + method: 'DELETE', + data: { + tracks: [ + {uri: 'spotify:track:5LwukQO2fCx35GUUN6d6NW'}, + {uri: 'spotify:track:4w8CsAnzn0lXJxYlAuCtCW'}, + {uri: 'spotify:track:2Foc5Q5nqNiosCNqttzHof'} + ] + }, + headers: { + 'Authorization': 'Bearer TESTING', + 'Content-Type': 'application/json' + } }); }); @@ -1260,15 +1158,19 @@ describe('angular-spotify', function () { ]); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/users/triple.j.abc/playlists/73ppZmbaAS2aW9hmDTTDcb/tracks', 'DELETE', null, { - tracks: [ - {uri: 'spotify:track:5LwukQO2fCx35GUUN6d6NW'}, - {uri: 'spotify:track:4w8CsAnzn0lXJxYlAuCtCW'}, - {uri: 'spotify:track:2Foc5Q5nqNiosCNqttzHof'} - ] - }, { - 'Authorization': 'Bearer TESTING', - 'Content-Type': 'application/json' + expect(Spotify.api).toHaveBeenCalledWith('/users/triple.j.abc/playlists/73ppZmbaAS2aW9hmDTTDcb/tracks', { + method: 'DELETE', + data: { + tracks: [ + {uri: 'spotify:track:5LwukQO2fCx35GUUN6d6NW'}, + {uri: 'spotify:track:4w8CsAnzn0lXJxYlAuCtCW'}, + {uri: 'spotify:track:2Foc5Q5nqNiosCNqttzHof'} + ] + }, + headers: { + 'Authorization': 'Bearer TESTING', + 'Content-Type': 'application/json' + } }); }); }); @@ -1286,13 +1188,17 @@ describe('angular-spotify', function () { }); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/users/triple.j.abc/playlists/73ppZmbaAS2aW9hmDTTDcb/tracks', 'PUT', null, { - range_start: 2, - range_length: 10, - insert_before: 15 - },{ - 'Authorization': 'Bearer TESTING', - 'Content-Type': 'application/json' + expect(Spotify.api).toHaveBeenCalledWith('/users/triple.j.abc/playlists/73ppZmbaAS2aW9hmDTTDcb/tracks', { + method: 'PUT', + data: { + range_start: 2, + range_length: 10, + insert_before: 15 + }, + headers: { + 'Authorization': 'Bearer TESTING', + 'Content-Type': 'application/json' + } }); }); }); @@ -1310,11 +1216,15 @@ describe('angular-spotify', function () { ]); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/users/triple.j.abc/playlists/73ppZmbaAS2aW9hmDTTDcb/tracks', 'PUT', { - uris: 'spotify:track:5LwukQO2fCx35GUUN6d6NW,spotify:track:4w8CsAnzn0lXJxYlAuCtCW,spotify:track:2Foc5Q5nqNiosCNqttzHof' - }, null, { - 'Authorization': 'Bearer TESTING', - 'Content-Type': 'application/json' + expect(Spotify.api).toHaveBeenCalledWith('/users/triple.j.abc/playlists/73ppZmbaAS2aW9hmDTTDcb/tracks', { + method: 'PUT', + params: { + uris: 'spotify:track:5LwukQO2fCx35GUUN6d6NW,spotify:track:4w8CsAnzn0lXJxYlAuCtCW,spotify:track:2Foc5Q5nqNiosCNqttzHof' + }, + headers: { + 'Authorization': 'Bearer TESTING', + 'Content-Type': 'application/json' + } }); }); @@ -1330,11 +1240,15 @@ describe('angular-spotify', function () { ]); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/users/triple.j.abc/playlists/73ppZmbaAS2aW9hmDTTDcb/tracks', 'PUT', { - uris: 'spotify:track:5LwukQO2fCx35GUUN6d6NW,spotify:track:4w8CsAnzn0lXJxYlAuCtCW,spotify:track:2Foc5Q5nqNiosCNqttzHof' - }, null, { - 'Authorization': 'Bearer TESTING', - 'Content-Type': 'application/json' + expect(Spotify.api).toHaveBeenCalledWith('/users/triple.j.abc/playlists/73ppZmbaAS2aW9hmDTTDcb/tracks', { + method: 'PUT', + params: { + uris: 'spotify:track:5LwukQO2fCx35GUUN6d6NW,spotify:track:4w8CsAnzn0lXJxYlAuCtCW,spotify:track:2Foc5Q5nqNiosCNqttzHof' + }, + headers: { + 'Authorization': 'Bearer TESTING', + 'Content-Type': 'application/json' + } }); }); }); @@ -1351,11 +1265,15 @@ describe('angular-spotify', function () { }); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/users/1176458919/playlists/3ygKiRcD8ed3i2g8P7jlXr', 'PUT', null, { - name: 'Awesome Mix Vol. 2' - }, { - 'Authorization': 'Bearer TESTING', - 'Content-Type': 'application/json' + expect(Spotify.api).toHaveBeenCalledWith('/users/1176458919/playlists/3ygKiRcD8ed3i2g8P7jlXr', { + method: 'PUT', + data: { + name: 'Awesome Mix Vol. 2' + }, + headers: { + 'Authorization': 'Bearer TESTING', + 'Content-Type': 'application/json' + } }); }); @@ -1422,8 +1340,10 @@ describe('angular-spotify', function () { Spotify.getCurrentUser(); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/me', 'GET', null, null, { - 'Authorization': 'Bearer TESTING' + expect(Spotify.api).toHaveBeenCalledWith('/me', { + headers: { + 'Authorization': 'Bearer TESTING' + } }); }); @@ -1443,8 +1363,10 @@ describe('angular-spotify', function () { Spotify.getSavedUserTracks(); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/me/tracks', 'GET', undefined, null, { - 'Authorization': 'Bearer TESTING' + expect(Spotify.api).toHaveBeenCalledWith('/me/tracks', { + headers: { + 'Authorization': 'Bearer TESTING' + } }); }); @@ -1460,10 +1382,13 @@ describe('angular-spotify', function () { Spotify.userTracksContains(['0udZHhCi7p1YzMlvI4fXoK','3SF5puV5eb6bgRSxBeMOk9']); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/me/tracks/contains', 'GET', { - ids: '0udZHhCi7p1YzMlvI4fXoK,3SF5puV5eb6bgRSxBeMOk9' - }, null, { - 'Authorization': 'Bearer TESTING' + expect(Spotify.api).toHaveBeenCalledWith('/me/tracks/contains', { + params: { + ids: '0udZHhCi7p1YzMlvI4fXoK,3SF5puV5eb6bgRSxBeMOk9' + }, + headers: { + 'Authorization': 'Bearer TESTING' + } }); }); @@ -1475,10 +1400,13 @@ describe('angular-spotify', function () { Spotify.userTracksContains(['spotify:track:0udZHhCi7p1YzMlvI4fXoK','spotify:track:3SF5puV5eb6bgRSxBeMOk9']); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/me/tracks/contains', 'GET', { - ids: '0udZHhCi7p1YzMlvI4fXoK,3SF5puV5eb6bgRSxBeMOk9' - }, null, { - 'Authorization': 'Bearer TESTING' + expect(Spotify.api).toHaveBeenCalledWith('/me/tracks/contains', { + params: { + ids: '0udZHhCi7p1YzMlvI4fXoK,3SF5puV5eb6bgRSxBeMOk9' + }, + headers: { + 'Authorization': 'Bearer TESTING' + } }); }); @@ -1494,10 +1422,14 @@ describe('angular-spotify', function () { Spotify.saveUserTracks(['4iV5W9uYEdYUVa79Axb7Rh','1301WleyT98MSxVHPZCA6M']); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/me/tracks', 'PUT', { - ids: '4iV5W9uYEdYUVa79Axb7Rh,1301WleyT98MSxVHPZCA6M' - }, null, { - 'Authorization': 'Bearer TESTING' + expect(Spotify.api).toHaveBeenCalledWith('/me/tracks', { + method: 'PUT', + params: { + ids: '4iV5W9uYEdYUVa79Axb7Rh,1301WleyT98MSxVHPZCA6M' + }, + headers: { + 'Authorization': 'Bearer TESTING' + } }); }); @@ -1509,10 +1441,14 @@ describe('angular-spotify', function () { Spotify.saveUserTracks(['spotify:track:0udZHhCi7p1YzMlvI4fXoK','spotify:track:3SF5puV5eb6bgRSxBeMOk9']); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/me/tracks', 'PUT', { - ids: '0udZHhCi7p1YzMlvI4fXoK,3SF5puV5eb6bgRSxBeMOk9' - }, null, { - 'Authorization': 'Bearer TESTING' + expect(Spotify.api).toHaveBeenCalledWith('/me/tracks', { + method: 'PUT', + params: { + ids: '0udZHhCi7p1YzMlvI4fXoK,3SF5puV5eb6bgRSxBeMOk9' + }, + headers: { + 'Authorization': 'Bearer TESTING' + } }); }); @@ -1528,11 +1464,15 @@ describe('angular-spotify', function () { Spotify.removeUserTracks(['4iV5W9uYEdYUVa79Axb7Rh','1301WleyT98MSxVHPZCA6M']); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/me/tracks', 'DELETE', { - ids: '4iV5W9uYEdYUVa79Axb7Rh,1301WleyT98MSxVHPZCA6M' - }, null, { - 'Authorization': 'Bearer TESTING', - 'Content-Type': 'application/json' + expect(Spotify.api).toHaveBeenCalledWith('/me/tracks', { + method: 'DELETE', + params: { + ids: '4iV5W9uYEdYUVa79Axb7Rh,1301WleyT98MSxVHPZCA6M' + }, + headers: { + 'Authorization': 'Bearer TESTING', + 'Content-Type': 'application/json' + } }); }); @@ -1544,11 +1484,15 @@ describe('angular-spotify', function () { 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' + expect(Spotify.api).toHaveBeenCalledWith('/me/tracks', { + method: 'DELETE', + params: { + ids: '0udZHhCi7p1YzMlvI4fXoK,3SF5puV5eb6bgRSxBeMOk9' + }, + headers: { + 'Authorization': 'Bearer TESTING', + 'Content-Type': 'application/json' + } }); }); @@ -1564,8 +1508,10 @@ describe('angular-spotify', function () { Spotify.getSavedUserAlbums(); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/me/albums', 'GET', undefined, null, { - 'Authorization': 'Bearer TESTING' + expect(Spotify.api).toHaveBeenCalledWith('/me/albums', { + headers: { + 'Authorization': 'Bearer TESTING' + } }); }); @@ -1581,10 +1527,14 @@ describe('angular-spotify', function () { Spotify.saveUserAlbums(['4iV5W9uYEdYUVa79Axb7Rh','1301WleyT98MSxVHPZCA6M']); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/me/albums', 'PUT', { - ids: '4iV5W9uYEdYUVa79Axb7Rh,1301WleyT98MSxVHPZCA6M' - }, null, { - 'Authorization': 'Bearer TESTING' + expect(Spotify.api).toHaveBeenCalledWith('/me/albums', { + method: 'PUT', + params: { + ids: '4iV5W9uYEdYUVa79Axb7Rh,1301WleyT98MSxVHPZCA6M' + }, + headers: { + 'Authorization': 'Bearer TESTING' + } }); }); @@ -1596,10 +1546,14 @@ describe('angular-spotify', function () { Spotify.saveUserAlbums(['spotify:album:4iV5W9uYEdYUVa79Axb7Rh','spotify:album:1301WleyT98MSxVHPZCA6M']); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/me/albums', 'PUT', { - ids: '4iV5W9uYEdYUVa79Axb7Rh,1301WleyT98MSxVHPZCA6M' - }, null, { - 'Authorization': 'Bearer TESTING' + expect(Spotify.api).toHaveBeenCalledWith('/me/albums', { + method: 'PUT', + params: { + ids: '4iV5W9uYEdYUVa79Axb7Rh,1301WleyT98MSxVHPZCA6M' + }, + headers: { + 'Authorization': 'Bearer TESTING' + } }); }); @@ -1615,11 +1569,15 @@ describe('angular-spotify', function () { Spotify.removeUserAlbums(['4iV5W9uYEdYUVa79Axb7Rh','1301WleyT98MSxVHPZCA6M']); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/me/albums', 'DELETE', { - ids: '4iV5W9uYEdYUVa79Axb7Rh,1301WleyT98MSxVHPZCA6M' - }, null, { - 'Authorization': 'Bearer TESTING', - 'Content-Type': 'application/json' + expect(Spotify.api).toHaveBeenCalledWith('/me/albums', { + method: 'DELETE', + params: { + ids: '4iV5W9uYEdYUVa79Axb7Rh,1301WleyT98MSxVHPZCA6M' + }, + headers: { + 'Authorization': 'Bearer TESTING', + 'Content-Type': 'application/json' + } }); }); @@ -1631,11 +1589,15 @@ describe('angular-spotify', function () { Spotify.removeUserAlbums(['spotify:album:4iV5W9uYEdYUVa79Axb7Rh','spotify:album:1301WleyT98MSxVHPZCA6M']); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/me/albums', 'DELETE', { - ids: '4iV5W9uYEdYUVa79Axb7Rh,1301WleyT98MSxVHPZCA6M' - }, null, { - 'Authorization': 'Bearer TESTING', - 'Content-Type': 'application/json' + expect(Spotify.api).toHaveBeenCalledWith('/me/albums', { + method: 'DELETE', + params: { + ids: '4iV5W9uYEdYUVa79Axb7Rh,1301WleyT98MSxVHPZCA6M' + }, + headers: { + 'Authorization': 'Bearer TESTING', + 'Content-Type': 'application/json' + } }); }); @@ -1651,10 +1613,13 @@ describe('angular-spotify', function () { Spotify.userAlbumsContains(['4iV5W9uYEdYUVa79Axb7Rh','1301WleyT98MSxVHPZCA6M']); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/me/albums/contains', 'GET', { - ids: '4iV5W9uYEdYUVa79Axb7Rh,1301WleyT98MSxVHPZCA6M' - }, null, { - 'Authorization': 'Bearer TESTING' + expect(Spotify.api).toHaveBeenCalledWith('/me/albums/contains', { + params: { + ids: '4iV5W9uYEdYUVa79Axb7Rh,1301WleyT98MSxVHPZCA6M' + }, + headers: { + 'Authorization': 'Bearer TESTING' + } }); }); @@ -1666,10 +1631,13 @@ describe('angular-spotify', function () { Spotify.userAlbumsContains(['spotify:album:4iV5W9uYEdYUVa79Axb7Rh','spotify:album:1301WleyT98MSxVHPZCA6M']); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/me/albums/contains', 'GET', { - ids: '4iV5W9uYEdYUVa79Axb7Rh,1301WleyT98MSxVHPZCA6M' - }, null, { - 'Authorization': 'Bearer TESTING' + expect(Spotify.api).toHaveBeenCalledWith('/me/albums/contains', { + params: { + ids: '4iV5W9uYEdYUVa79Axb7Rh,1301WleyT98MSxVHPZCA6M' + }, + headers: { + 'Authorization': 'Bearer TESTING' + } }); }); @@ -1687,8 +1655,10 @@ describe('angular-spotify', function () { Spotify.getUserTopArtists(); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/me/top/artists', 'GET', {}, null, { - 'Authorization': 'Bearer TESTING' + expect(Spotify.api).toHaveBeenCalledWith('/me/top/artists', { + headers: { + 'Authorization': 'Bearer TESTING' + } }); }); @@ -1703,11 +1673,14 @@ describe('angular-spotify', function () { }); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/me/top/artists', 'GET', { - limit: 50, - offset: 50 - }, null, { - 'Authorization': 'Bearer TESTING' + expect(Spotify.api).toHaveBeenCalledWith('/me/top/artists', { + params: { + limit: 50, + offset: 50 + }, + headers: { + 'Authorization': 'Bearer TESTING' + } }); }); }); @@ -1721,8 +1694,10 @@ describe('angular-spotify', function () { Spotify.getUserTopTracks(); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/me/top/tracks', 'GET', {}, null, { - 'Authorization': 'Bearer TESTING' + expect(Spotify.api).toHaveBeenCalledWith('/me/top/tracks', { + headers: { + 'Authorization': 'Bearer TESTING' + } }); }); @@ -1737,11 +1712,14 @@ describe('angular-spotify', function () { }); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/me/top/tracks', 'GET', { - limit: 50, - offset: 50 - }, null, { - 'Authorization': 'Bearer TESTING' + expect(Spotify.api).toHaveBeenCalledWith('/me/top/tracks', { + params: { + limit: 50, + offset: 50 + }, + headers: { + 'Authorization': 'Bearer TESTING' + } }); }); }); @@ -1767,10 +1745,14 @@ describe('angular-spotify', function () { Spotify.getFeaturedPlaylists({ country: 'NL', locale: 'nl_NL' }); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/browse/featured-playlists', 'GET', { - country: 'NL', locale: 'nl_NL' - }, null, { - 'Authorization': 'Bearer TESTING' + expect(Spotify.api).toHaveBeenCalledWith('/browse/featured-playlists', { + params: { + country: 'NL', + locale: 'nl_NL' + }, + headers: { + 'Authorization': 'Bearer TESTING' + } }); }); @@ -1801,10 +1783,13 @@ describe('angular-spotify', function () { Spotify.getNewReleases({ country: 'NL' }); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/browse/new-releases', 'GET', { - country: 'NL' - }, null, { - 'Authorization': 'Bearer TESTING' + expect(Spotify.api).toHaveBeenCalledWith('/browse/new-releases', { + params: { + country: 'NL' + }, + headers: { + 'Authorization': 'Bearer TESTING' + } }); }); @@ -1833,8 +1818,10 @@ describe('angular-spotify', function () { Spotify.getCategories(); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/browse/categories', 'GET', undefined, null, { - 'Authorization': 'Bearer TESTING' + expect(Spotify.api).toHaveBeenCalledWith('/browse/categories', { + headers: { + 'Authorization': 'Bearer TESTING' + } }); }); @@ -1849,11 +1836,14 @@ describe('angular-spotify', function () { }); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/browse/categories', 'GET', { - country: 'SG', - limit: 20 - }, null, { - 'Authorization': 'Bearer TESTING' + expect(Spotify.api).toHaveBeenCalledWith('/browse/categories', { + params: { + country: 'SG', + limit: 20 + }, + headers: { + 'Authorization': 'Bearer TESTING' + } }); }); }); @@ -1867,8 +1857,10 @@ describe('angular-spotify', function () { Spotify.getCategory('party'); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/browse/categories/party', 'GET', undefined, null, { - 'Authorization': 'Bearer TESTING' + expect(Spotify.api).toHaveBeenCalledWith('/browse/categories/party', { + headers: { + 'Authorization': 'Bearer TESTING' + } }); }); @@ -1882,10 +1874,13 @@ describe('angular-spotify', function () { }); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/browse/categories/party', 'GET', { - country: 'SG' - }, null, { - 'Authorization': 'Bearer TESTING' + expect(Spotify.api).toHaveBeenCalledWith('/browse/categories/party', { + params: { + country: 'SG' + }, + headers: { + 'Authorization': 'Bearer TESTING' + } }); }); }); @@ -1899,8 +1894,10 @@ describe('angular-spotify', function () { Spotify.getCategoryPlaylists('party'); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/browse/categories/party/playlists', 'GET', undefined, null, { - 'Authorization': 'Bearer TESTING' + expect(Spotify.api).toHaveBeenCalledWith('/browse/categories/party/playlists', { + headers: { + 'Authorization': 'Bearer TESTING' + } }); }); @@ -1915,11 +1912,14 @@ describe('angular-spotify', function () { }); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/browse/categories/party/playlists', 'GET', { - country: 'SG', - limit: 20 - }, null, { - 'Authorization': 'Bearer TESTING' + expect(Spotify.api).toHaveBeenCalledWith('/browse/categories/party/playlists', { + params: { + country: 'SG', + limit: 20 + }, + headers: { + 'Authorization': 'Bearer TESTING' + } }); }); }); @@ -1935,10 +1935,13 @@ describe('angular-spotify', function () { }); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/recommendations', 'GET', { - seed_artists: '4NHQUGzhtTLFvgF5SZesLK' - }, null, { - 'Authorization': 'Bearer TESTING' + expect(Spotify.api).toHaveBeenCalledWith('/recommendations', { + params: { + seed_artists: '4NHQUGzhtTLFvgF5SZesLK' + }, + headers: { + 'Authorization': 'Bearer TESTING' + } }); }); }); @@ -1952,8 +1955,10 @@ describe('angular-spotify', function () { Spotify.getAvailableGenreSeeds(); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/recommendations/available-genre-seeds', 'GET', null, null, { - 'Authorization': 'Bearer TESTING' + expect(Spotify.api).toHaveBeenCalledWith('/recommendations/available-genre-seeds', { + headers: { + 'Authorization': 'Bearer TESTING' + } }); }); }); @@ -1977,10 +1982,13 @@ describe('angular-spotify', function () { Spotify.following('artist'); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/me/following', 'GET', { - type: 'artist' - }, null, { - 'Authorization': 'Bearer TESTING' + expect(Spotify.api).toHaveBeenCalledWith('/me/following', { + params: { + type: 'artist' + }, + headers: { + 'Authorization': 'Bearer TESTING' + } }); }); @@ -1992,11 +2000,14 @@ describe('angular-spotify', function () { Spotify.following('artist', { limit: 30 }); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/me/following', 'GET', { - type: 'artist', - limit: 30 - }, null, { - 'Authorization': 'Bearer TESTING' + expect(Spotify.api).toHaveBeenCalledWith('/me/following', { + params: { + type: 'artist', + limit: 30 + }, + headers: { + 'Authorization': 'Bearer TESTING' + } }); }); }); @@ -2010,11 +2021,15 @@ describe('angular-spotify', function () { Spotify.follow('user', 'exampleuser01'); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/me/following', 'PUT', { - type: 'user', - ids: 'exampleuser01' - }, null, { - 'Authorization': 'Bearer TESTING' + expect(Spotify.api).toHaveBeenCalledWith('/me/following', { + method: 'PUT', + params: { + type: 'user', + ids: 'exampleuser01' + }, + headers: { + 'Authorization': 'Bearer TESTING' + } }); }); @@ -2026,11 +2041,15 @@ describe('angular-spotify', function () { Spotify.follow('artist', '74ASZWbe4lXaubB36ztrGX,08td7MxkoHQkXnWAYD8d6Q'); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/me/following', 'PUT', { - type: 'artist', - ids: '74ASZWbe4lXaubB36ztrGX,08td7MxkoHQkXnWAYD8d6Q' - }, null, { - 'Authorization': 'Bearer TESTING' + expect(Spotify.api).toHaveBeenCalledWith('/me/following', { + method: 'PUT', + params: { + type: 'artist', + ids: '74ASZWbe4lXaubB36ztrGX,08td7MxkoHQkXnWAYD8d6Q' + }, + headers: { + 'Authorization': 'Bearer TESTING' + } }); }); }); @@ -2044,11 +2063,15 @@ describe('angular-spotify', function () { Spotify.unfollow('user', 'exampleuser01'); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/me/following', 'DELETE', { - type: 'user', - ids: 'exampleuser01' - }, null, { - 'Authorization': 'Bearer TESTING' + expect(Spotify.api).toHaveBeenCalledWith('/me/following', { + method: 'DELETE', + params: { + type: 'user', + ids: 'exampleuser01' + }, + headers: { + 'Authorization': 'Bearer TESTING' + } }); }); @@ -2060,11 +2083,15 @@ describe('angular-spotify', function () { Spotify.unfollow('artist', '74ASZWbe4lXaubB36ztrGX,08td7MxkoHQkXnWAYD8d6Q'); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/me/following', 'DELETE', { - type: 'artist', - ids: '74ASZWbe4lXaubB36ztrGX,08td7MxkoHQkXnWAYD8d6Q' - }, null, { - 'Authorization': 'Bearer TESTING' + expect(Spotify.api).toHaveBeenCalledWith('/me/following', { + method: 'DELETE', + params: { + type: 'artist', + ids: '74ASZWbe4lXaubB36ztrGX,08td7MxkoHQkXnWAYD8d6Q' + }, + headers: { + 'Authorization': 'Bearer TESTING' + } }); }); }); @@ -2078,11 +2105,14 @@ describe('angular-spotify', function () { Spotify.userFollowingContains('user', 'exampleuser01'); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/me/following/contains', 'GET', { - type: 'user', - ids: 'exampleuser01' - }, null, { - 'Authorization': 'Bearer TESTING' + expect(Spotify.api).toHaveBeenCalledWith('/me/following/contains', { + params: { + type: 'user', + ids: 'exampleuser01' + }, + headers: { + 'Authorization': 'Bearer TESTING' + } }); }); }); @@ -2096,11 +2126,15 @@ describe('angular-spotify', function () { Spotify.followPlaylist('jmperezperez', '2v3iNvBX8Ay1Gt2uXtUKUT'); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/users/jmperezperez/playlists/2v3iNvBX8Ay1Gt2uXtUKUT/followers', 'PUT', null, { - public: null - }, { - 'Authorization': 'Bearer TESTING', - 'Content-Type': 'application/json' + expect(Spotify.api).toHaveBeenCalledWith('/users/jmperezperez/playlists/2v3iNvBX8Ay1Gt2uXtUKUT/followers', { + method: 'PUT', + data: { + public: null + }, + headers: { + 'Authorization': 'Bearer TESTING', + 'Content-Type': 'application/json' + } }); }); @@ -2112,11 +2146,15 @@ describe('angular-spotify', function () { Spotify.followPlaylist('jmperezperez', '2v3iNvBX8Ay1Gt2uXtUKUT', true); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/users/jmperezperez/playlists/2v3iNvBX8Ay1Gt2uXtUKUT/followers', 'PUT', null, { - public: true - }, { - 'Authorization': 'Bearer TESTING', - 'Content-Type': 'application/json' + expect(Spotify.api).toHaveBeenCalledWith('/users/jmperezperez/playlists/2v3iNvBX8Ay1Gt2uXtUKUT/followers', { + method: 'PUT', + data: { + public: true + }, + headers: { + 'Authorization': 'Bearer TESTING', + 'Content-Type': 'application/json' + } }); }); }); @@ -2130,8 +2168,11 @@ describe('angular-spotify', function () { Spotify.unfollowPlaylist('jmperezperez', '2v3iNvBX8Ay1Gt2uXtUKUT'); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/users/jmperezperez/playlists/2v3iNvBX8Ay1Gt2uXtUKUT/followers', 'DELETE', null, null, { - 'Authorization': 'Bearer TESTING' + expect(Spotify.api).toHaveBeenCalledWith('/users/jmperezperez/playlists/2v3iNvBX8Ay1Gt2uXtUKUT/followers', { + method: 'DELETE', + headers: { + 'Authorization': 'Bearer TESTING' + } }); }); }); @@ -2145,10 +2186,13 @@ describe('angular-spotify', function () { Spotify.playlistFollowingContains('jmperezperez', '2v3iNvBX8Ay1Gt2uXtUKUT', 'possan,elogain'); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/users/jmperezperez/playlists/2v3iNvBX8Ay1Gt2uXtUKUT/followers/contains', 'GET', { - ids: 'possan,elogain' - }, null, { - 'Authorization': 'Bearer TESTING' + expect(Spotify.api).toHaveBeenCalledWith('/users/jmperezperez/playlists/2v3iNvBX8Ay1Gt2uXtUKUT/followers/contains', { + params: { + ids: 'possan,elogain' + }, + headers: { + 'Authorization': 'Bearer TESTING' + } }); }); @@ -2160,10 +2204,13 @@ describe('angular-spotify', function () { Spotify.playlistFollowingContains('jmperezperez', '2v3iNvBX8Ay1Gt2uXtUKUT', ['possan','elogain']); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/users/jmperezperez/playlists/2v3iNvBX8Ay1Gt2uXtUKUT/followers/contains', 'GET', { - ids: 'possan,elogain' - }, null, { - 'Authorization': 'Bearer TESTING' + expect(Spotify.api).toHaveBeenCalledWith('/users/jmperezperez/playlists/2v3iNvBX8Ay1Gt2uXtUKUT/followers/contains', { + params: { + ids: 'possan,elogain' + }, + headers: { + 'Authorization': 'Bearer TESTING' + } }); }); }); From c5e4be91664eb6c7d7e661a8df124055427ed87a Mon Sep 17 00:00:00 2001 From: Ed Moore Date: Wed, 13 Apr 2016 16:29:53 +0800 Subject: [PATCH 02/13] Immutable as possible --- src/angular-spotify.js | 169 +++++++++++++++++------------------------ 1 file changed, 71 insertions(+), 98 deletions(-) diff --git a/src/angular-spotify.js b/src/angular-spotify.js index f06b455..a7389eb 100644 --- a/src/angular-spotify.js +++ b/src/angular-spotify.js @@ -1,6 +1,18 @@ (function (window, angular, undefined) { 'use strict'; + function getSpotifyId (s) { + return s.indexOf('spotify:') === -1 ? s : s.split(':')[2]; + } + + function stringToArray (s) { + if (!s) { return; } + var arr = angular.isString(s) ? s.split(',') : s; + return arr.map(function (value) { + return getSpotifyId(value); + }); + } + angular .module('spotify', []) .provider('Spotify', function () { @@ -120,9 +132,8 @@ * Pass in album id or spotify uri */ getAlbum: function (album) { - album = album.indexOf('spotify:') === -1 ? album : album.split(':')[2]; - - return this.api('/albums/' + album); + var a = getSpotifyId(album); + return this.api('/albums/' + a); }, /** @@ -130,13 +141,10 @@ * Pass in comma separated string or array of album ids */ getAlbums: function (albums) { - albums = angular.isString(albums) ? albums.split(',') : albums; - angular.forEach(albums, function (value, index) { - albums[index] = value.indexOf('spotify:') > -1 ? value.split(':')[2] : value; - }); + var a = stringToArray(albums); return this.api('/albums', { params: { - ids: albums ? albums.toString() : '' + ids: a ? a.toString() : '' } }); }, @@ -146,9 +154,9 @@ * Pass in album id or spotify uri */ getAlbumTracks: function (album, options) { - album = album.indexOf('spotify:') === -1 ? album : album.split(':')[2]; + var a = getSpotifyId(album); var o = options && { params: options }; - return this.api('/albums/' + album + '/tracks', o); + return this.api('/albums/' + a + '/tracks', o); }, @@ -160,31 +168,27 @@ * Get an Artist */ getArtist: function (artist) { - artist = artist.indexOf('spotify:') === -1 ? artist : artist.split(':')[2]; - - return this.api('/artists/' + artist); + var a = getSpotifyId(artist); + return this.api('/artists/' + a); }, /** * Get multiple artists */ getArtists: function (artists) { - artists = angular.isString(artists) ? artists.split(',') : artists; - angular.forEach(artists, function (value, index) { - artists[index] = value.indexOf('spotify:') > -1 ? value.split(':')[2] : value; - }); + var a = stringToArray(artists); return this.api('/artists/', { params: { - ids: artists ? artists.toString() : '' + ids: a ? a.toString() : '' } }); }, //Artist Albums getArtistAlbums: function (artist, options) { - artist = artist.indexOf('spotify:') === -1 ? artist : artist.split(':')[2]; + var a = getSpotifyId(artist); var o = options && { params: options }; - return this.api('/artists/' + artist + '/albums', o); + return this.api('/artists/' + a + '/albums', o); }, /** @@ -192,9 +196,8 @@ * The country: an ISO 3166-1 alpha-2 country code. */ getArtistTopTracks: function (artist, country) { - artist = artist.indexOf('spotify:') === -1 ? artist : artist.split(':')[2]; - - return this.api('/artists/' + artist + '/top-tracks', { + var a = getSpotifyId(artist); + return this.api('/artists/' + a + '/top-tracks', { params: { country: country } @@ -202,9 +205,8 @@ }, getRelatedArtists: function (artist) { - artist = artist.indexOf('spotify:') === -1 ? artist : artist.split(':')[2]; - - return this.api('/artists/' + artist + '/related-artists'); + var a = getSpotifyId(artist); + return this.api('/artists/' + a + '/related-artists'); }, @@ -261,10 +263,10 @@ ====================== Following ===================== */ following: function (type, options) { - options = options || {}; - options.type = type; + var params = options || {}; + params.type = type; return this.api('/me/following', { - params: options, + params: params, headers: this._auth() }); }, @@ -329,55 +331,43 @@ }, userTracksContains: 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; - }); + var trks = stringToArray(tracks); return this.api('/me/tracks/contains', { params: { - ids: tracks.toString() + ids: trks.toString() }, headers: this._auth() }); }, saveUserTracks: 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; - }); + var trks = stringToArray(tracks); return this.api('/me/tracks', { method: 'PUT', params: { - ids: tracks.toString() + ids: trks.toString() }, headers: this._auth() }); }, 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; - }); + var trks = stringToArray(tracks); return this.api('/me/tracks', { method: 'DELETE', params: { - ids: tracks.toString() + ids: trks.toString() }, headers: this._auth(true) }); }, saveUserAlbums: function (albums) { - albums = angular.isString(albums) ? albums.split(',') : albums; - angular.forEach(albums, function (value, index) { - albums[index] = value.indexOf('spotify:') > -1 ? value.split(':')[2] : value; - }); + var albs = stringToArray(albums); return this.api('/me/albums', { method: 'PUT', params: { - ids: albums.toString() + ids: albs.toString() }, headers: this._auth() }); @@ -390,27 +380,21 @@ }, removeUserAlbums: function (albums) { - albums = angular.isString(albums) ? albums.split(',') : albums; - angular.forEach(albums, function (value, index) { - albums[index] = value.indexOf('spotify:') > -1 ? value.split(':')[2] : value; - }); + var albs = stringToArray(albums); return this.api('/me/albums', { method: 'DELETE', params: { - ids: albums.toString() + ids: albs.toString() }, headers: this._auth(true) }); }, userAlbumsContains: function (albums) { - albums = angular.isString(albums) ? albums.split(',') : albums; - angular.forEach(albums, function (value, index) { - albums[index] = value.indexOf('spotify:') > -1 ? value.split(':')[2] : value; - }); + var albs = stringToArray(albums); return this.api('/me/albums/contains', { params: { - ids: albums.toString() + ids: albs.toString() }, headers: this._auth() }); @@ -463,14 +447,14 @@ }, addPlaylistTracks: function (userId, playlistId, tracks, options) { - tracks = angular.isArray(tracks) ? tracks : tracks.split(','); - angular.forEach(tracks, function (value, index) { - tracks[index] = value.indexOf('spotify:') === -1 ? 'spotify:track:' + value : value; + var arr = angular.isArray(tracks) ? tracks : tracks.split(','); + var trks = arr.map(function (value) { + return value.indexOf('spotify:') === -1 ? 'spotify:track:' + value : value; }); return this.api('/users/' + userId + '/playlists/' + playlistId + '/tracks', { method: 'POST', params: { - uris: tracks.toString(), + uris: trks.toString(), position: options ? options.position : null }, headers: this._auth(true) @@ -478,18 +462,16 @@ }, removePlaylistTracks: function (userId, playlistId, tracks) { - tracks = angular.isArray(tracks) ? tracks : tracks.split(','); - var track; - angular.forEach(tracks, function (value, index) { - track = tracks[index]; - tracks[index] = { + var arr = angular.isArray(tracks) ? tracks : tracks.split(','); + var trks = arr.map(function (track) { + return { uri: track.indexOf('spotify:') === -1 ? 'spotify:track:' + track : track }; }); return this.api('/users/' + userId + '/playlists/' + playlistId + '/tracks', { method: 'DELETE', data: { - tracks: tracks + tracks: trks }, headers: this._auth(true) }); @@ -504,16 +486,14 @@ }, replacePlaylistTracks: function (userId, playlistId, tracks) { - tracks = angular.isArray(tracks) ? tracks : tracks.split(','); - var track; - angular.forEach(tracks, function (value, index) { - track = tracks[index]; - tracks[index] = track.indexOf('spotify:') === -1 ? 'spotify:track:' + track : track; + var arr = tracks = angular.isArray(tracks) ? tracks : tracks.split(','); + var trks = arr.map(function (track) { + return track.indexOf('spotify:') === -1 ? 'spotify:track:' + track : track; }); return this.api('/users/' + userId + '/playlists/' + playlistId + '/tracks', { method: 'PUT', params: { - uris: tracks.toString() + uris: trks.toString() }, headers: this._auth(true) }); @@ -549,12 +529,12 @@ * type = artist, album or track */ search: function (q, type, options) { - options = options || {}; - options.q = q; - options.type = type; + var params = options || {}; + params.q = q; + params.type = type; return this.api('/search', { - params: options + params: params }); }, @@ -563,36 +543,29 @@ ====================== Tracks ===================== */ getTrack: function (track) { - track = track.indexOf('spotify:') === -1 ? track : track.split(':')[2]; - - return this.api('/tracks/' + track); + var t = getSpotifyId(track); + return this.api('/tracks/' + t); }, getTracks: 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; - }); + var trks = stringToArray(tracks); return this.api('/tracks/', { params: { - ids: tracks ? tracks.toString() : '' + ids: trks ? trks.toString() : '' } }); }, getTrackAudioFeatures: function (track) { - track = track.indexOf('spotify:') === -1 ? track : track.split(':')[2]; - return this.api('/audio-features/' + track); + var t = getSpotifyId(track); + return this.api('/audio-features/' + t); }, getTracksAudioFeatures: 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; - }); + var trks = stringToArray(tracks); return this.api('/audio-features/', { params: { - ids: tracks ? tracks.toString() : '' + ids: trks ? trks.toString() : '' } }); }, @@ -610,10 +583,10 @@ var deferred = $q.defer(); var that = this; - var w = 400, - h = 500, - left = (screen.width / 2) - (w / 2), - top = (screen.height / 2) - (h / 2); + var w = 400; + var h = 500; + var left = (screen.width / 2) - (w / 2); + var top = (screen.height / 2) - (h / 2); var params = { client_id: this.clientId, From 8f4cd9df365e5f28316ed28f76de5057e842a058 Mon Sep 17 00:00:00 2001 From: Ed Moore Date: Fri, 15 Apr 2016 11:46:43 +0800 Subject: [PATCH 03/13] Only set auth in api method --- README.md | 88 ++++---- src/angular-spotify.js | 169 ++++++++------- test/spec/angular-spotify.spec.js | 344 ++++++++---------------------- 3 files changed, 215 insertions(+), 386 deletions(-) diff --git a/README.md b/README.md index ead0de7..12f301f 100644 --- a/README.md +++ b/README.md @@ -175,11 +175,11 @@ Spotify #### Get an Artist’s Related Artists Get Spotify catalog information about artists similar to a given artist. Similarity is based on analysis of the Spotify community’s listening history. ```js -Spotify.getRelatedArtists('Artist Id or Spotify Artist URI'); +Spotify.getArtistRelatedArtists('Artist Id or Spotify Artist URI'); ``` Example: ```js -Spotify.getRelatedArtists('1vCWHaC5f2uS3yhpwWbIA6').then(function (data) { +Spotify.getArtistRelatedArtists('1vCWHaC5f2uS3yhpwWbIA6').then(function (data) { console.log(data); }); ``` @@ -329,13 +329,13 @@ These endpoints allow you manage the list of artists and users that a logged in Get the current user’s followed artists. ```js -Spotify.following('type', options) +Spotify.getFollowed('type', options) ``` - type: Required. currently only ```artist``` is supported. ```js -Spotify.following('artists', { limit: 10 }).then(function (artists) { +Spotify.getFollowed('artists', { limit: 10 }).then(function (artists) { console.log(artists); }) ``` @@ -371,14 +371,14 @@ Spotify.unfollow('user', 'exampleuser01').then(function () { #### Check if Current User Follows Check to see if the current user is following one or more artists or other Spotify users. ```js -Spotify.userFollowingContains('type', 'ids'); +Spotify.isFollowing('type', 'ids'); ``` - type: Required. either ```artist``` or ```user``` - ids: Required. comma-separated list. Example: ```js -Spotify.userFollowingContains('user', 'exampleuser01').then(function (data) { +Spotify.isFollowing('user', 'exampleuser01').then(function (data) { console.log(data); }); ``` @@ -424,11 +424,11 @@ Spotify Checking if the user is privately following a playlist is only possible for the current user when that user has granted access to the ```playlist-read-private``` scope. ```js Spotify - .playlistFollowingContains('owner_id', 'playlist_id', 'comma separated string or array of user ids'); + .areFollowingPlaylist('owner_id', 'playlist_id', 'comma separated string or array of user ids'); ``` Example: ```js - Spotify.playlistFollowingContains('jmperezperez', '2v3iNvBX8Ay1Gt2uXtUKUT', 'possan,elogain').then(function (data) { + Spotify.areFollowingPlaylist('jmperezperez', '2v3iNvBX8Ay1Gt2uXtUKUT', 'possan,elogain').then(function (data) { console.log(data); }); ``` @@ -438,7 +438,7 @@ Spotify #### Get Current User’s Saved Tracks Get a list of the songs saved in the current Spotify user’s “Your Music” library. Requires the ```user-library-read``` scope. ```js -Spotify.getSavedUserTracks(options); +Spotify.getMySavedTracks(options); ``` ##### Options Object (Optional) @@ -446,7 +446,7 @@ Spotify.getSavedUserTracks(options); - offset - Optional. The index of the first object to return. Default: 0 (i.e., the first object). Use with limit to get the next set of objects. ```js -Spotify.getSavedUserTracks().then(function (data) { +Spotify.getMySavedTracks().then(function (data) { console.log(data); }); ``` @@ -456,12 +456,12 @@ Spotify.getSavedUserTracks().then(function (data) { Check if one or more tracks is already saved in the current Spotify user’s “Your Music” library. Requires the ```user-library-read``` scope. ```js -Spotify.userTracksContains('comma separated string or array of spotify track ids'); +Spotify.containsMySavedTracks('comma separated string or array of spotify track ids'); ``` Example: ```js Spotify - .userTracksContains('0udZHhCi7p1YzMlvI4fXoK,3SF5puV5eb6bgRSxBeMOk9') + .containsMySavedTracks('0udZHhCi7p1YzMlvI4fXoK,3SF5puV5eb6bgRSxBeMOk9') .then(function (data) { console.log(data); }); @@ -471,12 +471,12 @@ Spotify #### Save Tracks for Current User Save one or more tracks to the current user’s “Your Music” library. Requires the ```user-library-modify``` scope. ```js -Spotify.saveUserTracks('comma separated string or array of spotify track ids'); +Spotify.addToMySavedTracks('comma separated string or array of spotify track ids'); ``` Example: ```js Spotify - .saveUserTracks('0udZHhCi7p1YzMlvI4fXoK,3SF5puV5eb6bgRSxBeMOk9') + .addToMySavedTracks('0udZHhCi7p1YzMlvI4fXoK,3SF5puV5eb6bgRSxBeMOk9') .then(function (data) { console.log(data); }); @@ -486,12 +486,12 @@ Spotify #### Remove Tracks for Current User Remove one or more tracks from the current user’s “Your Music” library. Requires the ```user-library-modify``` scope. ```js -Spotify.removeUserTracks('comma separated string or array of spotify track ids'); +Spotify.removeFromMySavedTracks('comma separated string or array of spotify track ids'); ``` Example: ```js Spotify - .removeUserTracks('0udZHhCi7p1YzMlvI4fXoK,3SF5puV5eb6bgRSxBeMOk9') + .removeFromMySavedTracks('0udZHhCi7p1YzMlvI4fXoK,3SF5puV5eb6bgRSxBeMOk9') .then(function (data) { console.log(data); }); @@ -501,12 +501,12 @@ Spotify #### Save Albums for Current User Save one or more albums to the current user’s “Your Music” library. Requires the ```user-library-modify``` scope. ```js -Spotify.saveUserAlbums('comma separated string or array of spotify album ids'); +Spotify.addToMySavedAlbums('comma separated string or array of spotify album ids'); ``` Example: ```js Spotify - .saveUserAlbums('4iV5W9uYEdYUVa79Axb7Rh,1301WleyT98MSxVHPZCA6M') + .addToMySavedAlbums('4iV5W9uYEdYUVa79Axb7Rh,1301WleyT98MSxVHPZCA6M') .then(function (data) { console.log(data); }); @@ -515,7 +515,7 @@ Spotify #### Get Current User’s Saved Albums Get a list of the albums saved in the current Spotify user’s “Your Music” library. Requires the ```user-library-read``` scope. ```js -Spotify.getSavedUserAlbums(options); +Spotify.getMySavedAlbums(options); ``` ##### Options Object (Optional) @@ -524,7 +524,7 @@ Spotify.getSavedUserAlbums(options); - market - Optional. An ISO 3166-1 alpha-2 country code. Provide this parameter if you want to apply Track Relinking. ```js -Spotify.getSavedUserAlbums().then(function (data) { +Spotify.getMySavedAlbums().then(function (data) { console.log(data); }); ``` @@ -532,12 +532,12 @@ Spotify.getSavedUserAlbums().then(function (data) { #### Remove Albums for Current User Remove one or more albums from the current user’s “Your Music” library. Requires the ```user-library-modify``` scope. ```js -Spotify.removeUserAlbums('comma separated string or array of spotify album ids'); +Spotify.removeFromMySavedAlbums('comma separated string or array of spotify album ids'); ``` Example: ```js Spotify - .removeUserAlbums('4iV5W9uYEdYUVa79Axb7Rh,1301WleyT98MSxVHPZCA6M') + .removeFromMySavedAlbums('4iV5W9uYEdYUVa79Axb7Rh,1301WleyT98MSxVHPZCA6M') .then(function (data) { console.log(data); }); @@ -548,12 +548,12 @@ Spotify Check if one or more albums is already saved in the current Spotify user’s “Your Music” library. Requires the ```user-library-read``` scope. ```js -Spotify.userAlbumsContains('comma separated string or array of spotify album ids'); +Spotify.containsMySavedAlbums('comma separated string or array of spotify album ids'); ``` Example: ```js Spotify - .userAlbumsContains('4iV5W9uYEdYUVa79Axb7Rh,1301WleyT98MSxVHPZCA6M') + .containsMySavedAlbums('4iV5W9uYEdYUVa79Axb7Rh,1301WleyT98MSxVHPZCA6M') .then(function (data) { console.log(data); }); @@ -566,7 +566,7 @@ Endpoints for retrieving information about the user’s listening habits. #### Get a User’s Top Artists Get the current user’s top artists based on calculated affinity. ```js -Spotify.getUserTopArtists(options); +Spotify.getMyTopArtists(options); ``` ##### Options Object (Optional) @@ -576,7 +576,7 @@ Spotify.getUserTopArtists(options); Example: ```js -Spotify.getUserTopArtists({ limit: 50 }).then(function (data) { +Spotify.getMyTopArtists({ limit: 50 }).then(function (data) { console.log(data); }); ``` @@ -584,7 +584,7 @@ Spotify.getUserTopArtists({ limit: 50 }).then(function (data) { #### Get a User’s Top Tracks Get the current user’s top tracks based on calculated affinity. ```js -Spotify.getUserTopTracks(options); +Spotify.getMyTopTracks(options); ``` ##### Options Object (Optional) @@ -594,7 +594,7 @@ Spotify.getUserTopTracks(options); Example: ```js -Spotify.getUserTopTracks({ limit: 50 }).then(function (data) { +Spotify.getMyTopTracks({ limit: 50 }).then(function (data) { console.log(data); }); ``` @@ -674,7 +674,7 @@ Spotify #### Add Tracks to a Playlist Add one or more tracks to a user’s playlist. Adding tracks to a public playlist requires the ```playlist-modify-public``` scope. Adding tracks to a private playlist requires the ```playlist-modify-private``` scope. ```js -Spotify.addPlaylistTracks('user_id', 'playlist_id', 'comma separated string or array of spotify track uris'); +Spotify.addTracksToPlaylist('user_id', 'playlist_id', 'comma separated string or array of spotify track uris'); ``` ##### Options Object (Optional) - position - integer - Optional. The position to insert the tracks, a zero-based index. For example, to insert the tracks in the first position: position=0; to insert the tracks in the third position: position=2. If omitted, the tracks will be appended to the playlist. Tracks are added in the order they are listed in the query string or request body. @@ -683,7 +683,7 @@ Spotify.addPlaylistTracks('user_id', 'playlist_id', 'comma separated string or a Example: ```js Spotify - .addPlaylistTracks('1176458919', '2TkWjGCu8jurholsfdWtG4', 'spotify:track:4iV5W9uYEdYUVa79Axb7Rh, spotify:track:1301WleyT98MSxVHPZCA6M') + .addTracksToPlaylist('1176458919', '2TkWjGCu8jurholsfdWtG4', 'spotify:track:4iV5W9uYEdYUVa79Axb7Rh, spotify:track:1301WleyT98MSxVHPZCA6M') .then(function (data) { console.log('tracks added to playlist'); }); @@ -693,12 +693,12 @@ Spotify #### Remove Tracks from a Playlist Remove one or more tracks from a user’s playlist. Removing tracks from a public playlist requires the ```playlist-modify-public``` scope. Removing tracks from a private playlist requires the ```playlist-modify-private``` scope. ```js -Spotify.removePlaylistTracks('user_id', 'playlist_id', 'comma separated string or array of spotify track ids or uris'); +Spotify.removeTracksFromPlaylist('user_id', 'playlist_id', 'comma separated string or array of spotify track ids or uris'); ``` Example: ```js Spotify - .removePlaylistTracks('1176458919', '2TkWjGCu8jurholsfdWtG4', 'spotify:track:4iV5W9uYEdYUVa79Axb7Rh, spotify:track:1301WleyT98MSxVHPZCA6M') + .removeTracksFromPlaylist('1176458919', '2TkWjGCu8jurholsfdWtG4', 'spotify:track:4iV5W9uYEdYUVa79Axb7Rh, spotify:track:1301WleyT98MSxVHPZCA6M') .then(function (data) { console.log('tracks removed from playlist'); }); @@ -707,7 +707,7 @@ Spotify #### Reorder a Playlist's Tracks Reorder a track or a group of tracks in a playlist. ```js -Spotify.reorderPlaylistTracks('user_id', 'playlist_id', options); +Spotify.reorderTracksInPlaylist('user_id', 'playlist_id', options); ``` ##### Options Object (Required) - range_start - integer - Required. The position of the first track to be reordered. @@ -718,7 +718,7 @@ Spotify.reorderPlaylistTracks('user_id', 'playlist_id', options); Example: ```js -Spotify.reorderPlaylistTracks('1176458919', '2TkWjGCu8jurholsfdWtG4', { +Spotify.reorderTracksInPlaylist('1176458919', '2TkWjGCu8jurholsfdWtG4', { range_start: 8, range_length: 5, insert_before: 0 @@ -731,12 +731,12 @@ Spotify.reorderPlaylistTracks('1176458919', '2TkWjGCu8jurholsfdWtG4', { #### Replace a Playlist’s Tracks Replace all the tracks in a playlist, overwriting its existing tracks. This powerful request can be useful for replacing tracks, re-ordering existing tracks, or clearing the playlist. Replacing tracks in a public playlist requires the ```playlist-modify-public``` scope. Replacing tracks in a private playlist requires the ```playlist-modify-private``` scope. ```js -Spotify.replacePlaylistTracks('user_id', 'playlist_id', 'comma separated string or array of spotify track ids or uris'); +Spotify.replaceTracksInPlaylist('user_id', 'playlist_id', 'comma separated string or array of spotify track ids or uris'); ``` Example: ```js Spotify - .replacePlaylistTracks('1176458919', '2TkWjGCu8jurholsfdWtG4', 'spotify:track:4iV5W9uYEdYUVa79Axb7Rh, spotify:track:1301WleyT98MSxVHPZCA6M') + .replaceTracksInPlaylist('1176458919', '2TkWjGCu8jurholsfdWtG4', 'spotify:track:4iV5W9uYEdYUVa79Axb7Rh, spotify:track:1301WleyT98MSxVHPZCA6M') .then(function (data) { console.log('tracks removed from playlist'); }); @@ -746,7 +746,7 @@ Spotify #### Change a Playlist’s Details Change a playlist’s name and public/private state. (The user must, of course, own the playlist.) Changing a public playlist requires the ```playlist-modify-public``` scope. Changing a private playlist requires the ```playlist-modify-private``` scope. ```js -Spotify.updatePlaylistDetails('user_id', 'playlist_id', options); +Spotify.changePlaylistDetails('user_id', 'playlist_id', options); ``` ##### Options Object (Optional) - name - string - Optional. The new name for the playlist, for example "My New Playlist Title". @@ -756,7 +756,7 @@ Spotify.updatePlaylistDetails('user_id', 'playlist_id', options); Example: ```js Spotify - .updatePlaylistDetails('1176458919', '2TkWjGCu8jurholsfdWtG4', { name: 'Updated Playlist Title' }) + .changePlaylistDetails('1176458919', '2TkWjGCu8jurholsfdWtG4', { name: 'Updated Playlist Title' }) .then(function (data) { console.log('Updated playlist details'); }); @@ -782,11 +782,11 @@ Spotify.getUser('wizzler').then(function (data) { #### Get Current User’s Profile Get detailed profile information about the current user (including the current user’s username). ```js -Spotify.getCurrentUser(); +Spotify.getMe(); ``` Example: ```js -Spotify.getCurrentUser().then(function (data) { +Spotify.getMe().then(function (data) { console.log(data); }); ``` @@ -842,11 +842,11 @@ Spotify.getTracks('0eGsygTp906u18L0Oimnem,1lDWb6b6ieDQ2xT7ewTC3G').then(function Get audio feature information for a single track identified by its unique Spotify ID. ```js -Spotify.getTrackAudioFeatures('Track Id or Spotify Track URI'); +Spotify.getAudioFeaturesForTrack('Track Id or Spotify Track URI'); ``` Example: ```js -Spotify.getTrackAudioFeatures('0eGsygTp906u18L0Oimnem').then(function (data) { +Spotify.getAudioFeaturesForTrack('0eGsygTp906u18L0Oimnem').then(function (data) { console.log(data); }); ``` @@ -855,11 +855,11 @@ Spotify.getTrackAudioFeatures('0eGsygTp906u18L0Oimnem').then(function (data) { Get audio features for multiple tracks based on their Spotify IDs. ```js -Spotify.getTracksAudioFeatures('Comma separated list or array of Track Ids'); +Spotify.getAudioFeaturesForTracks('Comma separated list or array of Track Ids'); ``` Example: ```js -Spotify.getTracksAudioFeatures('0eGsygTp906u18L0Oimnem,1lDWb6b6ieDQ2xT7ewTC3G').then(function (data) { +Spotify.getAudioFeaturesForTracks('0eGsygTp906u18L0Oimnem,1lDWb6b6ieDQ2xT7ewTC3G').then(function (data) { console.log(data); }); ``` diff --git a/src/angular-spotify.js b/src/angular-spotify.js index a7389eb..49ffe26 100644 --- a/src/angular-spotify.js +++ b/src/angular-spotify.js @@ -95,6 +95,10 @@ var deferred = $q.defer(); options = options || {}; options.method = options.method || 'GET'; + options.headers = options.headers || {}; + if (this.authToken) { + options.headers.Authorization = 'Bearer ' + this.authToken; + } $http({ url: this.apiBase + endpoint, @@ -113,16 +117,6 @@ return deferred.promise; }, - _auth: function (isJson) { - var auth = { - 'Authorization': 'Bearer ' + this.authToken - }; - if (isJson) { - auth['Content-Type'] = 'application/json'; - } - return auth; - }, - /** ====================== Albums ===================== */ @@ -204,7 +198,7 @@ }); }, - getRelatedArtists: function (artist) { + getArtistRelatedArtists: function (artist) { var a = getSpotifyId(artist); return this.api('/artists/' + a + '/related-artists'); }, @@ -215,82 +209,73 @@ */ getFeaturedPlaylists: function (options) { return this.api('/browse/featured-playlists', { - params: options, - headers: this._auth() + params: options }); }, getNewReleases: function (options) { return this.api('/browse/new-releases', { - params: options, - headers: this._auth() + params: options }); }, getCategories: function (options) { - var o = { headers: this._auth() }; + var o = {}; if (options) { o.params = options; } return this.api('/browse/categories', o); }, getCategory: function (category_id, options) { - var o = { headers: this._auth() }; + var o = {}; if (options) { o.params = options; } return this.api('/browse/categories/' + category_id, o); }, getCategoryPlaylists: function (category_id, options) { - var o = { headers: this._auth() }; + var o = {}; if (options) { o.params = options; } return this.api('/browse/categories/' + category_id + '/playlists', o); }, getRecommendations: function (options) { return this.api('/recommendations', { - params: options, - headers: this._auth() + params: options }); }, getAvailableGenreSeeds: function () { - return this.api('/recommendations/available-genre-seeds', { - headers: this._auth() - }); + return this.api('/recommendations/available-genre-seeds'); }, /** ====================== Following ===================== */ - following: function (type, options) { + getFollowed: function (type, options) { var params = options || {}; params.type = type; return this.api('/me/following', { - params: params, - headers: this._auth() + params: params }); }, follow: function (type, ids) { return this.api('/me/following', { method: 'PUT', - params: { type: type, ids: ids }, - headers: this._auth() + params: { type: type, ids: ids } }); }, unfollow: function (type, ids) { return this.api('/me/following', { method: 'DELETE', - params: { type: type, ids: ids }, - headers: this._auth() + params: { type: type, ids: ids } }); }, - userFollowingContains: function (type, ids) { + isFollowing: function (type, ids) { return this.api('/me/following/contains', { - params: { type: type, ids: ids }, - headers: this._auth() + params: { type: type, ids: ids } }); }, @@ -300,23 +285,23 @@ data: { public: isPublic || null }, - headers: this._auth(true) + headers: { + 'Content-Type': 'application/json' + } }); }, unfollowPlaylist: function (userId, playlistId) { return this.api('/users/' + userId + '/playlists/' + playlistId + '/followers', { - method: 'DELETE', - headers: this._auth() + method: 'DELETE' }); }, - playlistFollowingContains: function(userId, playlistId, ids) { + areFollowingPlaylist: function(userId, playlistId, ids) { return this.api('/users/' + userId + '/playlists/' + playlistId + '/followers/contains', { params: { ids: ids.toString() - }, - headers: this._auth() + } }); }, @@ -324,79 +309,79 @@ /** ====================== Library ===================== */ - getSavedUserTracks: function (options) { - var o = { headers: this._auth() }; + getMySavedTracks: function (options) { + var o = {}; if (options) { o.params = options; } return this.api('/me/tracks', o); }, - userTracksContains: function (tracks) { + containsMySavedTracks: function (tracks) { var trks = stringToArray(tracks); return this.api('/me/tracks/contains', { params: { ids: trks.toString() - }, - headers: this._auth() + } }); }, - saveUserTracks: function (tracks) { + addToMySavedTracks: function (tracks) { var trks = stringToArray(tracks); return this.api('/me/tracks', { method: 'PUT', params: { ids: trks.toString() - }, - headers: this._auth() + } }); }, - removeUserTracks: function (tracks) { + removeFromMySavedTracks: function (tracks) { var trks = stringToArray(tracks); return this.api('/me/tracks', { method: 'DELETE', params: { ids: trks.toString() }, - headers: this._auth(true) + headers: { + 'Content-Type': 'application/json' + } }); }, - saveUserAlbums: function (albums) { + addToMySavedAlbums: function (albums) { var albs = stringToArray(albums); return this.api('/me/albums', { method: 'PUT', params: { ids: albs.toString() - }, - headers: this._auth() + } }); }, - getSavedUserAlbums: function (options) { - var o = { headers: this._auth() }; + getMySavedAlbums: function (options) { + var o = {}; if (options) { o.params = options; } return this.api('/me/albums', o); }, - removeUserAlbums: function (albums) { + removeFromMySavedAlbums: function (albums) { var albs = stringToArray(albums); return this.api('/me/albums', { method: 'DELETE', params: { ids: albs.toString() }, - headers: this._auth(true) + headers: { + 'Content-Type': 'application/json' + } }); }, - userAlbumsContains: function (albums) { + containsMySavedAlbums: function (albums) { var albs = stringToArray(albums); return this.api('/me/albums/contains', { params: { ids: albs.toString() - }, - headers: this._auth() + } }); }, @@ -404,14 +389,14 @@ /** ====================== Personalization ===================== */ - getUserTopArtists: function (options) { - var o = { headers: this._auth() }; + getMyTopArtists: function (options) { + var o = {}; if (options) { o.params = options; } return this.api('/me/top/artists', o); }, - getUserTopTracks: function (options) { - var o = { headers: this._auth() }; + getMyTopTracks: function (options) { + var o = {}; if (options) { o.params = options; } return this.api('/me/top/tracks', o); }, @@ -421,19 +406,19 @@ ====================== Playlists ===================== */ getUserPlaylists: function (userId, options) { - var o = { headers: this._auth() }; + var o = {}; if (options) { o.params = options; } return this.api('/users/' + userId + '/playlists', o); }, getPlaylist: function (userId, playlistId, options) { - var o = { headers: this._auth() }; + var o = {}; if (options) { o.params = options; } return this.api('/users/' + userId + '/playlists/' + playlistId, o); }, getPlaylistTracks: function (userId, playlistId, options) { - var o = { headers: this._auth() }; + var o = {}; if (options) { o.params = options; } return this.api('/users/' + userId + '/playlists/' + playlistId + '/tracks', o); }, @@ -442,11 +427,13 @@ return this.api('/users/' + userId + '/playlists', { method: 'POST', data: options, - headers: this._auth(true) + headers: { + 'Content-Type': 'application/json' + } }); }, - addPlaylistTracks: function (userId, playlistId, tracks, options) { + addTracksToPlaylist: function (userId, playlistId, tracks, options) { var arr = angular.isArray(tracks) ? tracks : tracks.split(','); var trks = arr.map(function (value) { return value.indexOf('spotify:') === -1 ? 'spotify:track:' + value : value; @@ -457,11 +444,13 @@ uris: trks.toString(), position: options ? options.position : null }, - headers: this._auth(true) + headers: { + 'Content-Type': 'application/json' + } }); }, - removePlaylistTracks: function (userId, playlistId, tracks) { + removeTracksFromPlaylist: function (userId, playlistId, tracks) { var arr = angular.isArray(tracks) ? tracks : tracks.split(','); var trks = arr.map(function (track) { return { @@ -473,19 +462,23 @@ data: { tracks: trks }, - headers: this._auth(true) + headers: { + 'Content-Type': 'application/json' + } }); }, - reorderPlaylistTracks: function (userId, playlistId, options) { + reorderTracksInPlaylist: function (userId, playlistId, options) { return this.api('/users/' + userId + '/playlists/' + playlistId + '/tracks', { method: 'PUT', data: options, - headers: this._auth(true) + headers: { + 'Content-Type': 'application/json' + } }); }, - replacePlaylistTracks: function (userId, playlistId, tracks) { + replaceTracksInPlaylist: function (userId, playlistId, tracks) { var arr = tracks = angular.isArray(tracks) ? tracks : tracks.split(','); var trks = arr.map(function (track) { return track.indexOf('spotify:') === -1 ? 'spotify:track:' + track : track; @@ -495,15 +488,19 @@ params: { uris: trks.toString() }, - headers: this._auth(true) + headers: { + 'Content-Type': 'application/json' + } }); }, - updatePlaylistDetails: function (userId, playlistId, options) { + changePlaylistDetails: function (userId, playlistId, options) { return this.api('/users/' + userId + '/playlists/' + playlistId, { method: 'PUT', data: options, - headers: this._auth(true) + headers: { + 'Content-Type': 'application/json' + } }); }, @@ -515,10 +512,8 @@ return this.api('/users/' + userId); }, - getCurrentUser: function () { - return this.api('/me', { - headers: this._auth() - }); + getMe: function () { + return this.api('/me'); }, @@ -528,10 +523,10 @@ * q = search query * type = artist, album or track */ - search: function (q, type, options) { + search: function (q, types, options) { var params = options || {}; params.q = q; - params.type = type; + params.type = angular.isArray(types) ? types.join(',') : types; return this.api('/search', { params: params @@ -556,12 +551,12 @@ }); }, - getTrackAudioFeatures: function (track) { + getAudioFeaturesForTrack: function (track) { var t = getSpotifyId(track); return this.api('/audio-features/' + t); }, - getTracksAudioFeatures: function (tracks) { + getAudioFeaturesForTracks: function (tracks) { var trks = stringToArray(tracks); return this.api('/audio-features/', { params: { @@ -579,6 +574,10 @@ return this.authToken; }, + getAuthToken: function () { + return this.authToken; + }, + login: function () { var deferred = $q.defer(); var that = this; diff --git a/test/spec/angular-spotify.spec.js b/test/spec/angular-spotify.spec.js index b1b7c20..0337118 100644 --- a/test/spec/angular-spotify.spec.js +++ b/test/spec/angular-spotify.spec.js @@ -14,38 +14,6 @@ describe('angular-spotify', function () { inject(function () {}); }); - it('should be defined', function () { - expect(spotifyProvider).toBeDefined(); - }); - - it('should have a method setClientId()', function () { - expect(spotifyProvider.setClientId).toBeDefined(); - }); - - it('should have a method getClientId()', function () { - expect(spotifyProvider.getClientId).toBeDefined(); - }); - - it('should have a method setRedirectUri()', function () { - expect(spotifyProvider.setRedirectUri).toBeDefined(); - }); - - it('should have a method getRedirectUri()', function () { - expect(spotifyProvider.getRedirectUri).toBeDefined(); - }); - - it('should have a method setScope()', function () { - expect(spotifyProvider.setScope).toBeDefined(); - }); - - it('should have a method setScope()', function () { - expect(spotifyProvider.setScope).toBeDefined(); - }); - - it('should have a method setAuthToken', function () { - expect(spotifyProvider.setAuthToken).toBeDefined(); - }); - it('should set the client id', function () { expect(spotifyProvider.setClientId('ABCDEFGHIJKLMNOP')).toBe('ABCDEFGHIJKLMNOP'); }); @@ -93,8 +61,13 @@ describe('angular-spotify', function () { expect(Spotify.setAuthToken('ABCDEFGHIJKLMNOP')).toBe('ABCDEFGHIJKLMNOP'); }); + it('should get the AuthToken', function () { + Spotify.setAuthToken('ABCDEFGHIJKLMNOP'); + expect(Spotify.getAuthToken()).toBe('ABCDEFGHIJKLMNOP'); + }); + it('should turn an object into a query string', function () { - expect(Spotify.toQueryString({a: 't', b: 4, c: 'q'})).toBe('a=t&b=4&c=q'); + expect(Spotify.toQueryString({ a: 't', b: 4, c: 'q' })).toBe('a=t&b=4&c=q'); }); describe('Spotify.api', function () { @@ -105,7 +78,7 @@ describe('angular-spotify', function () { beforeEach(inject(function(_Spotify_, _$httpBackend_) { Spotify = _Spotify_; $httpBackend = _$httpBackend_; - jasmine.getJSONFixtures().fixturesPath='base/test/mock'; + jasmine.getJSONFixtures().fixturesPath = 'base/test/mock'; })); afterEach(function(){ @@ -164,14 +137,12 @@ describe('angular-spotify', function () { }).respond({}); var result; + Spotify.setAuthToken('TESTING'); Spotify.api('/users/wizzler/playlists', { method: 'POST', data: { name: 'TESTING', public: false - }, - headers: { - 'Authorization': 'Bearer TESTING' } }).then(function (data) { result = data; @@ -693,15 +664,15 @@ describe('angular-spotify', function () { }); }); - describe('Spotify.getRelatedArtists', function() { + describe('Spotify.getArtistRelatedArtists', function() { it('should make an ajax call to https://api.spotify.com/v1/artists/{id}/related-artists', function () { - expect(Spotify.getRelatedArtists('0LcJLqbBmaGUft1e9Mm8HV')).toBeDefined(); + expect(Spotify.getArtistRelatedArtists('0LcJLqbBmaGUft1e9Mm8HV')).toBeDefined(); }); it('should convert spotify uri to just an id', function () { spyOn(Spotify, 'api'); - Spotify.getRelatedArtists('spotify:artist:0LcJLqbBmaGUft1e9Mm8HV'); + Spotify.getArtistRelatedArtists('spotify:artist:0LcJLqbBmaGUft1e9Mm8HV'); expect(Spotify.api).toHaveBeenCalled(); expect(Spotify.api).toHaveBeenCalledWith('/artists/0LcJLqbBmaGUft1e9Mm8HV/related-artists'); @@ -712,7 +683,7 @@ describe('angular-spotify', function () { var result; Spotify - .getRelatedArtists('0LcJLqbBmaGUft1e9Mm8HV') + .getArtistRelatedArtists('0LcJLqbBmaGUft1e9Mm8HV') .then(function (data) { result = data; }); @@ -732,7 +703,7 @@ describe('angular-spotify', function () { var result; Spotify - .getRelatedArtists('ABCDEFGHIJKLMNOP') + .getArtistRelatedArtists('ABCDEFGHIJKLMNOP') .then(function () { }, function (reason) { result = reason; @@ -866,15 +837,15 @@ describe('angular-spotify', function () { }); }); - describe('Spotify.getTrackAudioFeatures', function () { + describe('Spotify.getAudioFeaturesForTrack', function () { it('should make an ajax call to https://api.spotify.com/v1/audio-features/{id}', function () { - expect(Spotify.getTrackAudioFeatures('0eGsygTp906u18L0Oimnem')).toBeDefined(); + expect(Spotify.getAudioFeaturesForTrack('0eGsygTp906u18L0Oimnem')).toBeDefined(); }); it('should convert spotify uri to just an id', function () { spyOn(Spotify, 'api'); - Spotify.getTrackAudioFeatures('spotify:artist:0eGsygTp906u18L0Oimnem'); + Spotify.getAudioFeaturesForTrack('spotify:artist:0eGsygTp906u18L0Oimnem'); expect(Spotify.api).toHaveBeenCalled(); expect(Spotify.api).toHaveBeenCalledWith('/audio-features/0eGsygTp906u18L0Oimnem'); @@ -887,7 +858,7 @@ describe('angular-spotify', function () { var result; Spotify - .getTrackAudioFeatures('0eGsygTp906u18L0Oimnem') + .getAudioFeaturesForTrack('0eGsygTp906u18L0Oimnem') .then(function (data) { result = data; }); @@ -907,7 +878,7 @@ describe('angular-spotify', function () { var result; Spotify - .getTrackAudioFeatures('ABCDEFGHIJKLMNOP') + .getAudioFeaturesForTrack('ABCDEFGHIJKLMNOP') .then(function () { }, function (reason) { result = reason; @@ -920,9 +891,9 @@ describe('angular-spotify', function () { }); }); - describe('Spotify.getTracksAudioFeatures', function() { + describe('Spotify.getAudioFeaturesForTracks', function() { it('should make an ajax call to https://api.spotify.com/v1/audio-features?ids={id}', function () { - expect(Spotify.getTracksAudioFeatures('0eGsygTp906u18L0Oimnem,1lDWb6b6ieDQ2xT7ewTC3G')).toBeDefined(); + expect(Spotify.getAudioFeaturesForTracks('0eGsygTp906u18L0Oimnem,1lDWb6b6ieDQ2xT7ewTC3G')).toBeDefined(); }); it('should resolve to an array of tracks', function () { @@ -930,7 +901,7 @@ describe('angular-spotify', function () { var result; Spotify - .getTracksAudioFeatures('0eGsygTp906u18L0Oimnem,1lDWb6b6ieDQ2xT7ewTC3G') + .getAudioFeaturesForTracks('0eGsygTp906u18L0Oimnem,1lDWb6b6ieDQ2xT7ewTC3G') .then(function (data) { result = data; }); @@ -943,7 +914,7 @@ describe('angular-spotify', function () { it('should convert spotify uris to Ids', function () { spyOn(Spotify, 'api'); - Spotify.getTracksAudioFeatures('spotify:track:0eGsygTp906u18L0Oimnem,spotify:track:1lDWb6b6ieDQ2xT7ewTC3G'); + Spotify.getAudioFeaturesForTracks('spotify:track:0eGsygTp906u18L0Oimnem,spotify:track:1lDWb6b6ieDQ2xT7ewTC3G'); expect(Spotify.api).toHaveBeenCalled(); expect(Spotify.api).toHaveBeenCalledWith('/audio-features/', { @@ -963,7 +934,7 @@ describe('angular-spotify', function () { var result; Spotify - .getTracksAudioFeatures() + .getAudioFeaturesForTracks() .then(function () { }, function (reason) { result = reason; @@ -990,11 +961,7 @@ describe('angular-spotify', function () { Spotify.getUserPlaylists('wizzler'); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/users/wizzler/playlists', { - headers: { - 'Authorization': 'Bearer TESTING' - } - }); + expect(Spotify.api).toHaveBeenCalledWith('/users/wizzler/playlists', {}); }); }); @@ -1009,11 +976,7 @@ describe('angular-spotify', function () { Spotify.getPlaylist('triple.j.abc', '73ppZmbaAS2aW9hmDTTDcb'); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/users/triple.j.abc/playlists/73ppZmbaAS2aW9hmDTTDcb', { - headers: { - 'Authorization': 'Bearer TESTING' - } - }); + expect(Spotify.api).toHaveBeenCalledWith('/users/triple.j.abc/playlists/73ppZmbaAS2aW9hmDTTDcb', {}); }); }); @@ -1028,11 +991,7 @@ describe('angular-spotify', function () { Spotify.getPlaylistTracks('triple.j.abc', '73ppZmbaAS2aW9hmDTTDcb'); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/users/triple.j.abc/playlists/73ppZmbaAS2aW9hmDTTDcb/tracks', { - headers: { - 'Authorization': 'Bearer TESTING' - } - }); + expect(Spotify.api).toHaveBeenCalledWith('/users/triple.j.abc/playlists/73ppZmbaAS2aW9hmDTTDcb/tracks', {}); }); }); @@ -1055,7 +1014,6 @@ describe('angular-spotify', function () { name: 'Awesome Mix Vol. 1' }, headers: { - 'Authorization': 'Bearer TESTING', 'Content-Type': 'application/json' } }); @@ -1063,14 +1021,14 @@ describe('angular-spotify', function () { }); - describe('Spotify.addPlaylistTracks', function() { + describe('Spotify.addTracksToPlaylist', function() { it('should call the correct url', function () { spyOn(Spotify, 'api'); Spotify.setAuthToken('TESTING'); - Spotify.addPlaylistTracks('triple.j.abc', '73ppZmbaAS2aW9hmDTTDcb', [ + Spotify.addTracksToPlaylist('triple.j.abc', '73ppZmbaAS2aW9hmDTTDcb', [ 'spotify:track:5LwukQO2fCx35GUUN6d6NW', 'spotify:track:4w8CsAnzn0lXJxYlAuCtCW', 'spotify:track:2Foc5Q5nqNiosCNqttzHof' @@ -1084,7 +1042,6 @@ describe('angular-spotify', function () { position: null }, headers: { - 'Authorization': 'Bearer TESTING', 'Content-Type': 'application/json' } }); @@ -1095,7 +1052,7 @@ describe('angular-spotify', function () { Spotify.setAuthToken('TESTING'); - Spotify.addPlaylistTracks('triple.j.abc', '73ppZmbaAS2aW9hmDTTDcb', [ + Spotify.addTracksToPlaylist('triple.j.abc', '73ppZmbaAS2aW9hmDTTDcb', [ '5LwukQO2fCx35GUUN6d6NW', '4w8CsAnzn0lXJxYlAuCtCW', '2Foc5Q5nqNiosCNqttzHof' @@ -1109,7 +1066,6 @@ describe('angular-spotify', function () { position: null }, headers: { - 'Authorization': 'Bearer TESTING', 'Content-Type': 'application/json' } }); @@ -1117,13 +1073,13 @@ describe('angular-spotify', function () { }); - describe('Spotify.removePlaylistTracks', function() { + describe('Spotify.removeTracksFromPlaylist', function() { it('should call the correct url', function () { spyOn(Spotify, 'api'); Spotify.setAuthToken('TESTING'); - Spotify.removePlaylistTracks('triple.j.abc', '73ppZmbaAS2aW9hmDTTDcb', [ + Spotify.removeTracksFromPlaylist('triple.j.abc', '73ppZmbaAS2aW9hmDTTDcb', [ 'spotify:track:5LwukQO2fCx35GUUN6d6NW', 'spotify:track:4w8CsAnzn0lXJxYlAuCtCW', 'spotify:track:2Foc5Q5nqNiosCNqttzHof' @@ -1140,7 +1096,6 @@ describe('angular-spotify', function () { ] }, headers: { - 'Authorization': 'Bearer TESTING', 'Content-Type': 'application/json' } }); @@ -1151,7 +1106,7 @@ describe('angular-spotify', function () { Spotify.setAuthToken('TESTING'); - Spotify.removePlaylistTracks('triple.j.abc', '73ppZmbaAS2aW9hmDTTDcb', [ + Spotify.removeTracksFromPlaylist('triple.j.abc', '73ppZmbaAS2aW9hmDTTDcb', [ '5LwukQO2fCx35GUUN6d6NW', '4w8CsAnzn0lXJxYlAuCtCW', '2Foc5Q5nqNiosCNqttzHof' @@ -1168,20 +1123,19 @@ describe('angular-spotify', function () { ] }, headers: { - 'Authorization': 'Bearer TESTING', 'Content-Type': 'application/json' } }); }); }); - describe('Spotify.reorderPlaylistTracks', function () { + describe('Spotify.reorderTracksInPlaylist', function () { it('should call the correct url', function () { spyOn(Spotify, 'api'); Spotify.setAuthToken('TESTING'); - Spotify.reorderPlaylistTracks('triple.j.abc', '73ppZmbaAS2aW9hmDTTDcb', { + Spotify.reorderTracksInPlaylist('triple.j.abc', '73ppZmbaAS2aW9hmDTTDcb', { range_start: 2, range_length: 10, insert_before: 15 @@ -1196,20 +1150,19 @@ describe('angular-spotify', function () { insert_before: 15 }, headers: { - 'Authorization': 'Bearer TESTING', 'Content-Type': 'application/json' } }); }); }); - describe('Spotify.replacePlaylistTracks', function() { + describe('Spotify.replaceTracksInPlaylist', function() { it('should call the correct url', function () { spyOn(Spotify, 'api'); Spotify.setAuthToken('TESTING'); - Spotify.replacePlaylistTracks('triple.j.abc', '73ppZmbaAS2aW9hmDTTDcb', [ + Spotify.replaceTracksInPlaylist('triple.j.abc', '73ppZmbaAS2aW9hmDTTDcb', [ 'spotify:track:5LwukQO2fCx35GUUN6d6NW', 'spotify:track:4w8CsAnzn0lXJxYlAuCtCW', 'spotify:track:2Foc5Q5nqNiosCNqttzHof' @@ -1222,7 +1175,6 @@ describe('angular-spotify', function () { uris: 'spotify:track:5LwukQO2fCx35GUUN6d6NW,spotify:track:4w8CsAnzn0lXJxYlAuCtCW,spotify:track:2Foc5Q5nqNiosCNqttzHof' }, headers: { - 'Authorization': 'Bearer TESTING', 'Content-Type': 'application/json' } }); @@ -1233,7 +1185,7 @@ describe('angular-spotify', function () { Spotify.setAuthToken('TESTING'); - Spotify.replacePlaylistTracks('triple.j.abc', '73ppZmbaAS2aW9hmDTTDcb', [ + Spotify.replaceTracksInPlaylist('triple.j.abc', '73ppZmbaAS2aW9hmDTTDcb', [ '5LwukQO2fCx35GUUN6d6NW', '4w8CsAnzn0lXJxYlAuCtCW', '2Foc5Q5nqNiosCNqttzHof' @@ -1246,21 +1198,20 @@ describe('angular-spotify', function () { uris: 'spotify:track:5LwukQO2fCx35GUUN6d6NW,spotify:track:4w8CsAnzn0lXJxYlAuCtCW,spotify:track:2Foc5Q5nqNiosCNqttzHof' }, headers: { - 'Authorization': 'Bearer TESTING', 'Content-Type': 'application/json' } }); }); }); - describe('Spotify.updatePlaylistDetails', function () { + describe('Spotify.changePlaylistDetails', function () { it('should call the correct URL', function () { spyOn(Spotify, 'api'); Spotify.setAuthToken('TESTING'); - Spotify.updatePlaylistDetails('1176458919', '3ygKiRcD8ed3i2g8P7jlXr', { + Spotify.changePlaylistDetails('1176458919', '3ygKiRcD8ed3i2g8P7jlXr', { name: 'Awesome Mix Vol. 2' }); @@ -1271,7 +1222,6 @@ describe('angular-spotify', function () { name: 'Awesome Mix Vol. 2' }, headers: { - 'Authorization': 'Bearer TESTING', 'Content-Type': 'application/json' } }); @@ -1330,21 +1280,17 @@ describe('angular-spotify', function () { }); }); - describe('Spotify.getCurrentUser', function () { + describe('Spotify.getMe', function () { it('should call the correct URL', function () { spyOn(Spotify, 'api'); Spotify.setAuthToken('TESTING'); - Spotify.getCurrentUser(); + Spotify.getMe(); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/me', { - headers: { - 'Authorization': 'Bearer TESTING' - } - }); + expect(Spotify.api).toHaveBeenCalledWith('/me'); }); }); @@ -1353,41 +1299,34 @@ describe('angular-spotify', function () { describe('User Library', function () { - describe('Spotify.getSavedUserTracks', function () { + describe('Spotify.getMySavedTracks', function () { it('should call the correct URL', function () { spyOn(Spotify, 'api'); Spotify.setAuthToken('TESTING'); - Spotify.getSavedUserTracks(); + Spotify.getMySavedTracks(); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/me/tracks', { - headers: { - 'Authorization': 'Bearer TESTING' - } - }); + expect(Spotify.api).toHaveBeenCalledWith('/me/tracks', {}); }); }); - describe('Spotify.userTracksContains', function () { + describe('Spotify.containsMySavedTracks', function () { it('should call the correct URL', function () { spyOn(Spotify, 'api'); Spotify.setAuthToken('TESTING'); - Spotify.userTracksContains(['0udZHhCi7p1YzMlvI4fXoK','3SF5puV5eb6bgRSxBeMOk9']); + Spotify.containsMySavedTracks(['0udZHhCi7p1YzMlvI4fXoK','3SF5puV5eb6bgRSxBeMOk9']); expect(Spotify.api).toHaveBeenCalled(); expect(Spotify.api).toHaveBeenCalledWith('/me/tracks/contains', { params: { ids: '0udZHhCi7p1YzMlvI4fXoK,3SF5puV5eb6bgRSxBeMOk9' - }, - headers: { - 'Authorization': 'Bearer TESTING' } }); }); @@ -1397,38 +1336,32 @@ describe('angular-spotify', function () { Spotify.setAuthToken('TESTING'); - Spotify.userTracksContains(['spotify:track:0udZHhCi7p1YzMlvI4fXoK','spotify:track:3SF5puV5eb6bgRSxBeMOk9']); + Spotify.containsMySavedTracks(['spotify:track:0udZHhCi7p1YzMlvI4fXoK','spotify:track:3SF5puV5eb6bgRSxBeMOk9']); expect(Spotify.api).toHaveBeenCalled(); expect(Spotify.api).toHaveBeenCalledWith('/me/tracks/contains', { params: { ids: '0udZHhCi7p1YzMlvI4fXoK,3SF5puV5eb6bgRSxBeMOk9' - }, - headers: { - 'Authorization': 'Bearer TESTING' } }); }); }); - describe('Spotify.saveUserTracks', function () { + describe('Spotify.addToMySavedTracks', function () { it('should call the correct URL', function () { spyOn(Spotify, 'api'); Spotify.setAuthToken('TESTING'); - Spotify.saveUserTracks(['4iV5W9uYEdYUVa79Axb7Rh','1301WleyT98MSxVHPZCA6M']); + Spotify.addToMySavedTracks(['4iV5W9uYEdYUVa79Axb7Rh','1301WleyT98MSxVHPZCA6M']); expect(Spotify.api).toHaveBeenCalled(); expect(Spotify.api).toHaveBeenCalledWith('/me/tracks', { method: 'PUT', params: { ids: '4iV5W9uYEdYUVa79Axb7Rh,1301WleyT98MSxVHPZCA6M' - }, - headers: { - 'Authorization': 'Bearer TESTING' } }); }); @@ -1438,30 +1371,27 @@ describe('angular-spotify', function () { Spotify.setAuthToken('TESTING'); - Spotify.saveUserTracks(['spotify:track:0udZHhCi7p1YzMlvI4fXoK','spotify:track:3SF5puV5eb6bgRSxBeMOk9']); + Spotify.addToMySavedTracks(['spotify:track:0udZHhCi7p1YzMlvI4fXoK','spotify:track:3SF5puV5eb6bgRSxBeMOk9']); expect(Spotify.api).toHaveBeenCalled(); expect(Spotify.api).toHaveBeenCalledWith('/me/tracks', { method: 'PUT', params: { ids: '0udZHhCi7p1YzMlvI4fXoK,3SF5puV5eb6bgRSxBeMOk9' - }, - headers: { - 'Authorization': 'Bearer TESTING' } }); }); }); - describe('Spotify.removeUserTracks', function () { + describe('Spotify.removeFromMySavedTracks', function () { it('should call the correct URL', function () { spyOn(Spotify, 'api'); Spotify.setAuthToken('TESTING'); - Spotify.removeUserTracks(['4iV5W9uYEdYUVa79Axb7Rh','1301WleyT98MSxVHPZCA6M']); + Spotify.removeFromMySavedTracks(['4iV5W9uYEdYUVa79Axb7Rh','1301WleyT98MSxVHPZCA6M']); expect(Spotify.api).toHaveBeenCalled(); expect(Spotify.api).toHaveBeenCalledWith('/me/tracks', { @@ -1470,7 +1400,6 @@ describe('angular-spotify', function () { ids: '4iV5W9uYEdYUVa79Axb7Rh,1301WleyT98MSxVHPZCA6M' }, headers: { - 'Authorization': 'Bearer TESTING', 'Content-Type': 'application/json' } }); @@ -1481,7 +1410,7 @@ describe('angular-spotify', function () { Spotify.setAuthToken('TESTING'); - Spotify.removeUserTracks(['spotify:track:0udZHhCi7p1YzMlvI4fXoK','spotify:track:3SF5puV5eb6bgRSxBeMOk9']); + Spotify.removeFromMySavedTracks(['spotify:track:0udZHhCi7p1YzMlvI4fXoK','spotify:track:3SF5puV5eb6bgRSxBeMOk9']); expect(Spotify.api).toHaveBeenCalled(); expect(Spotify.api).toHaveBeenCalledWith('/me/tracks', { @@ -1490,7 +1419,6 @@ describe('angular-spotify', function () { ids: '0udZHhCi7p1YzMlvI4fXoK,3SF5puV5eb6bgRSxBeMOk9' }, headers: { - 'Authorization': 'Bearer TESTING', 'Content-Type': 'application/json' } }); @@ -1498,42 +1426,35 @@ describe('angular-spotify', function () { }); - describe('Spotify.getSavedUserAlbums', function () { + describe('Spotify.getMySavedAlbums', function () { it('should call the correct URL', function () { spyOn(Spotify, 'api'); Spotify.setAuthToken('TESTING'); - Spotify.getSavedUserAlbums(); + Spotify.getMySavedAlbums(); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/me/albums', { - headers: { - 'Authorization': 'Bearer TESTING' - } - }); + expect(Spotify.api).toHaveBeenCalledWith('/me/albums', {}); }); }); - describe('Spotify.saveUserAlbums', function () { + describe('Spotify.addToMySavedAlbums', function () { it('should call the correct URL', function () { spyOn(Spotify, 'api'); Spotify.setAuthToken('TESTING'); - Spotify.saveUserAlbums(['4iV5W9uYEdYUVa79Axb7Rh','1301WleyT98MSxVHPZCA6M']); + Spotify.addToMySavedAlbums(['4iV5W9uYEdYUVa79Axb7Rh','1301WleyT98MSxVHPZCA6M']); expect(Spotify.api).toHaveBeenCalled(); expect(Spotify.api).toHaveBeenCalledWith('/me/albums', { method: 'PUT', params: { ids: '4iV5W9uYEdYUVa79Axb7Rh,1301WleyT98MSxVHPZCA6M' - }, - headers: { - 'Authorization': 'Bearer TESTING' } }); }); @@ -1543,30 +1464,27 @@ describe('angular-spotify', function () { Spotify.setAuthToken('TESTING'); - Spotify.saveUserAlbums(['spotify:album:4iV5W9uYEdYUVa79Axb7Rh','spotify:album:1301WleyT98MSxVHPZCA6M']); + Spotify.addToMySavedAlbums(['spotify:album:4iV5W9uYEdYUVa79Axb7Rh','spotify:album:1301WleyT98MSxVHPZCA6M']); expect(Spotify.api).toHaveBeenCalled(); expect(Spotify.api).toHaveBeenCalledWith('/me/albums', { method: 'PUT', params: { ids: '4iV5W9uYEdYUVa79Axb7Rh,1301WleyT98MSxVHPZCA6M' - }, - headers: { - 'Authorization': 'Bearer TESTING' } }); }); }); - describe('Spotify.removeUserAlbums', function () { + describe('Spotify.removeFromMySavedAlbums', function () { it('should call the correct URL', function () { spyOn(Spotify, 'api'); Spotify.setAuthToken('TESTING'); - Spotify.removeUserAlbums(['4iV5W9uYEdYUVa79Axb7Rh','1301WleyT98MSxVHPZCA6M']); + Spotify.removeFromMySavedAlbums(['4iV5W9uYEdYUVa79Axb7Rh','1301WleyT98MSxVHPZCA6M']); expect(Spotify.api).toHaveBeenCalled(); expect(Spotify.api).toHaveBeenCalledWith('/me/albums', { @@ -1575,7 +1493,6 @@ describe('angular-spotify', function () { ids: '4iV5W9uYEdYUVa79Axb7Rh,1301WleyT98MSxVHPZCA6M' }, headers: { - 'Authorization': 'Bearer TESTING', 'Content-Type': 'application/json' } }); @@ -1586,7 +1503,7 @@ describe('angular-spotify', function () { Spotify.setAuthToken('TESTING'); - Spotify.removeUserAlbums(['spotify:album:4iV5W9uYEdYUVa79Axb7Rh','spotify:album:1301WleyT98MSxVHPZCA6M']); + Spotify.removeFromMySavedAlbums(['spotify:album:4iV5W9uYEdYUVa79Axb7Rh','spotify:album:1301WleyT98MSxVHPZCA6M']); expect(Spotify.api).toHaveBeenCalled(); expect(Spotify.api).toHaveBeenCalledWith('/me/albums', { @@ -1595,7 +1512,6 @@ describe('angular-spotify', function () { ids: '4iV5W9uYEdYUVa79Axb7Rh,1301WleyT98MSxVHPZCA6M' }, headers: { - 'Authorization': 'Bearer TESTING', 'Content-Type': 'application/json' } }); @@ -1603,22 +1519,19 @@ describe('angular-spotify', function () { }); - describe('Spotify.userAlbumsContains', function () { + describe('Spotify.containsMySavedAlbums', function () { it('should call the correct URL', function () { spyOn(Spotify, 'api'); Spotify.setAuthToken('TESTING'); - Spotify.userAlbumsContains(['4iV5W9uYEdYUVa79Axb7Rh','1301WleyT98MSxVHPZCA6M']); + Spotify.containsMySavedAlbums(['4iV5W9uYEdYUVa79Axb7Rh','1301WleyT98MSxVHPZCA6M']); expect(Spotify.api).toHaveBeenCalled(); expect(Spotify.api).toHaveBeenCalledWith('/me/albums/contains', { params: { ids: '4iV5W9uYEdYUVa79Axb7Rh,1301WleyT98MSxVHPZCA6M' - }, - headers: { - 'Authorization': 'Bearer TESTING' } }); }); @@ -1628,15 +1541,12 @@ describe('angular-spotify', function () { Spotify.setAuthToken('TESTING'); - Spotify.userAlbumsContains(['spotify:album:4iV5W9uYEdYUVa79Axb7Rh','spotify:album:1301WleyT98MSxVHPZCA6M']); + Spotify.containsMySavedAlbums(['spotify:album:4iV5W9uYEdYUVa79Axb7Rh','spotify:album:1301WleyT98MSxVHPZCA6M']); expect(Spotify.api).toHaveBeenCalled(); expect(Spotify.api).toHaveBeenCalledWith('/me/albums/contains', { params: { ids: '4iV5W9uYEdYUVa79Axb7Rh,1301WleyT98MSxVHPZCA6M' - }, - headers: { - 'Authorization': 'Bearer TESTING' } }); }); @@ -1646,20 +1556,16 @@ describe('angular-spotify', function () { }); describe('Personalization', function () { - describe('Spotify.getUserTopArtists', function () { + describe('Spotify.getMyTopArtists', function () { it('should call the correct URL with authentication', function () { spyOn(Spotify, 'api'); Spotify.setAuthToken('TESTING'); - Spotify.getUserTopArtists(); + Spotify.getMyTopArtists(); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/me/top/artists', { - headers: { - 'Authorization': 'Bearer TESTING' - } - }); + expect(Spotify.api).toHaveBeenCalledWith('/me/top/artists', {}); }); it('should call the correct URL with authentication and options', function () { @@ -1667,7 +1573,7 @@ describe('angular-spotify', function () { Spotify.setAuthToken('TESTING'); - Spotify.getUserTopArtists({ + Spotify.getMyTopArtists({ limit: 50, offset: 50 }); @@ -1677,28 +1583,21 @@ describe('angular-spotify', function () { params: { limit: 50, offset: 50 - }, - headers: { - 'Authorization': 'Bearer TESTING' } }); }); }); - describe('Spotify.getUserTopTracks', function () { + describe('Spotify.getMyTopTracks', function () { it('should call the correct URL with authentication', function () { spyOn(Spotify, 'api'); Spotify.setAuthToken('TESTING'); - Spotify.getUserTopTracks(); + Spotify.getMyTopTracks(); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/me/top/tracks', { - headers: { - 'Authorization': 'Bearer TESTING' - } - }); + expect(Spotify.api).toHaveBeenCalledWith('/me/top/tracks', {}); }); it('should call the correct URL with authentication and options', function () { @@ -1706,7 +1605,7 @@ describe('angular-spotify', function () { Spotify.setAuthToken('TESTING'); - Spotify.getUserTopTracks({ + Spotify.getMyTopTracks({ limit: 50, offset: 50 }); @@ -1716,9 +1615,6 @@ describe('angular-spotify', function () { params: { limit: 50, offset: 50 - }, - headers: { - 'Authorization': 'Bearer TESTING' } }); }); @@ -1749,9 +1645,6 @@ describe('angular-spotify', function () { params: { country: 'NL', locale: 'nl_NL' - }, - headers: { - 'Authorization': 'Bearer TESTING' } }); }); @@ -1786,9 +1679,6 @@ describe('angular-spotify', function () { expect(Spotify.api).toHaveBeenCalledWith('/browse/new-releases', { params: { country: 'NL' - }, - headers: { - 'Authorization': 'Bearer TESTING' } }); }); @@ -1818,11 +1708,7 @@ describe('angular-spotify', function () { Spotify.getCategories(); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/browse/categories', { - headers: { - 'Authorization': 'Bearer TESTING' - } - }); + expect(Spotify.api).toHaveBeenCalledWith('/browse/categories', {}); }); it('should call the correct URL with authentication and options', function () { @@ -1840,9 +1726,6 @@ describe('angular-spotify', function () { params: { country: 'SG', limit: 20 - }, - headers: { - 'Authorization': 'Bearer TESTING' } }); }); @@ -1857,11 +1740,7 @@ describe('angular-spotify', function () { Spotify.getCategory('party'); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/browse/categories/party', { - headers: { - 'Authorization': 'Bearer TESTING' - } - }); + expect(Spotify.api).toHaveBeenCalledWith('/browse/categories/party', {}); }); it('should call the correct URL with authentication and options', function () { @@ -1877,9 +1756,6 @@ describe('angular-spotify', function () { expect(Spotify.api).toHaveBeenCalledWith('/browse/categories/party', { params: { country: 'SG' - }, - headers: { - 'Authorization': 'Bearer TESTING' } }); }); @@ -1894,11 +1770,7 @@ describe('angular-spotify', function () { Spotify.getCategoryPlaylists('party'); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/browse/categories/party/playlists', { - headers: { - 'Authorization': 'Bearer TESTING' - } - }); + expect(Spotify.api).toHaveBeenCalledWith('/browse/categories/party/playlists', {}); }); it('should call the correct URL with authentication with options', function () { @@ -1916,9 +1788,6 @@ describe('angular-spotify', function () { params: { country: 'SG', limit: 20 - }, - headers: { - 'Authorization': 'Bearer TESTING' } }); }); @@ -1938,9 +1807,6 @@ describe('angular-spotify', function () { expect(Spotify.api).toHaveBeenCalledWith('/recommendations', { params: { seed_artists: '4NHQUGzhtTLFvgF5SZesLK' - }, - headers: { - 'Authorization': 'Bearer TESTING' } }); }); @@ -1955,11 +1821,7 @@ describe('angular-spotify', function () { Spotify.getAvailableGenreSeeds(); expect(Spotify.api).toHaveBeenCalled(); - expect(Spotify.api).toHaveBeenCalledWith('/recommendations/available-genre-seeds', { - headers: { - 'Authorization': 'Bearer TESTING' - } - }); + expect(Spotify.api).toHaveBeenCalledWith('/recommendations/available-genre-seeds'); }); }); }); @@ -1973,21 +1835,18 @@ describe('angular-spotify', function () { $httpBackend = _$httpBackend_; })); - describe('Spotify.following', function () { + describe('Spotify.getFollowed', function () { it('should call the correct URL', function () { spyOn(Spotify, 'api'); Spotify.setAuthToken('TESTING'); - Spotify.following('artist'); + Spotify.getFollowed('artist'); expect(Spotify.api).toHaveBeenCalled(); expect(Spotify.api).toHaveBeenCalledWith('/me/following', { params: { type: 'artist' - }, - headers: { - 'Authorization': 'Bearer TESTING' } }); }); @@ -1997,16 +1856,13 @@ describe('angular-spotify', function () { Spotify.setAuthToken('TESTING'); - Spotify.following('artist', { limit: 30 }); + Spotify.getFollowed('artist', { limit: 30 }); expect(Spotify.api).toHaveBeenCalled(); expect(Spotify.api).toHaveBeenCalledWith('/me/following', { params: { type: 'artist', limit: 30 - }, - headers: { - 'Authorization': 'Bearer TESTING' } }); }); @@ -2026,9 +1882,6 @@ describe('angular-spotify', function () { params: { type: 'user', ids: 'exampleuser01' - }, - headers: { - 'Authorization': 'Bearer TESTING' } }); }); @@ -2046,9 +1899,6 @@ describe('angular-spotify', function () { params: { type: 'artist', ids: '74ASZWbe4lXaubB36ztrGX,08td7MxkoHQkXnWAYD8d6Q' - }, - headers: { - 'Authorization': 'Bearer TESTING' } }); }); @@ -2068,9 +1918,6 @@ describe('angular-spotify', function () { params: { type: 'user', ids: 'exampleuser01' - }, - headers: { - 'Authorization': 'Bearer TESTING' } }); }); @@ -2088,30 +1935,24 @@ describe('angular-spotify', function () { params: { type: 'artist', ids: '74ASZWbe4lXaubB36ztrGX,08td7MxkoHQkXnWAYD8d6Q' - }, - headers: { - 'Authorization': 'Bearer TESTING' } }); }); }); - describe('Spotify.userFollowingContains', function() { + describe('Spotify.isFollowing', function() { it('should call the correct URL', function () { spyOn(Spotify, 'api'); Spotify.setAuthToken('TESTING'); - Spotify.userFollowingContains('user', 'exampleuser01'); + Spotify.isFollowing('user', 'exampleuser01'); expect(Spotify.api).toHaveBeenCalled(); expect(Spotify.api).toHaveBeenCalledWith('/me/following/contains', { params: { type: 'user', ids: 'exampleuser01' - }, - headers: { - 'Authorization': 'Bearer TESTING' } }); }); @@ -2132,7 +1973,6 @@ describe('angular-spotify', function () { public: null }, headers: { - 'Authorization': 'Bearer TESTING', 'Content-Type': 'application/json' } }); @@ -2152,7 +1992,6 @@ describe('angular-spotify', function () { public: true }, headers: { - 'Authorization': 'Bearer TESTING', 'Content-Type': 'application/json' } }); @@ -2169,29 +2008,23 @@ describe('angular-spotify', function () { expect(Spotify.api).toHaveBeenCalled(); expect(Spotify.api).toHaveBeenCalledWith('/users/jmperezperez/playlists/2v3iNvBX8Ay1Gt2uXtUKUT/followers', { - method: 'DELETE', - headers: { - 'Authorization': 'Bearer TESTING' - } + method: 'DELETE' }); }); }); - describe('Spotify.playlistFollowingContains', function () { + describe('Spotify.areFollowingPlaylist', function () { it('should call the correct URL', function () { spyOn(Spotify, 'api'); Spotify.setAuthToken('TESTING'); - Spotify.playlistFollowingContains('jmperezperez', '2v3iNvBX8Ay1Gt2uXtUKUT', 'possan,elogain'); + Spotify.areFollowingPlaylist('jmperezperez', '2v3iNvBX8Ay1Gt2uXtUKUT', 'possan,elogain'); expect(Spotify.api).toHaveBeenCalled(); expect(Spotify.api).toHaveBeenCalledWith('/users/jmperezperez/playlists/2v3iNvBX8Ay1Gt2uXtUKUT/followers/contains', { params: { ids: 'possan,elogain' - }, - headers: { - 'Authorization': 'Bearer TESTING' } }); }); @@ -2201,15 +2034,12 @@ describe('angular-spotify', function () { Spotify.setAuthToken('TESTING'); - Spotify.playlistFollowingContains('jmperezperez', '2v3iNvBX8Ay1Gt2uXtUKUT', ['possan','elogain']); + Spotify.areFollowingPlaylist('jmperezperez', '2v3iNvBX8Ay1Gt2uXtUKUT', ['possan','elogain']); expect(Spotify.api).toHaveBeenCalled(); expect(Spotify.api).toHaveBeenCalledWith('/users/jmperezperez/playlists/2v3iNvBX8Ay1Gt2uXtUKUT/followers/contains', { params: { ids: 'possan,elogain' - }, - headers: { - 'Authorization': 'Bearer TESTING' } }); }); From 2c55fbc2a16b7d93d9e86f2d0880016f273c3e99 Mon Sep 17 00:00:00 2001 From: Ed Moore Date: Fri, 15 Apr 2016 12:11:07 +0800 Subject: [PATCH 04/13] AuthToken to AccessToken --- README.md | 29 ++- src/angular-spotify.js | 42 ++-- test/spec/angular-spotify.spec.js | 309 +++++++++++++++++++++--------- 3 files changed, 273 insertions(+), 107 deletions(-) diff --git a/README.md b/README.md index 12f301f..bb599f9 100644 --- a/README.md +++ b/README.md @@ -28,8 +28,8 @@ app.config(function (SpotifyProvider) { SpotifyProvider.setClientId(''); SpotifyProvider.setRedirectUri(''); SpotifyProvider.setScope(''); - // If you already have an auth token - SpotifyProvider.setAuthToken(''); + // If you already have an access token + SpotifyProvider.setAccessToken(''); }); ``` For example: @@ -38,8 +38,8 @@ app.config(function (SpotifyProvider) { SpotifyProvider.setClientId('ABC123DEF456GHI789JKL'); SpotifyProvider.setRedirectUri('http://www.example.com/callback.html'); SpotifyProvider.setScope('user-read-private playlist-read-private playlist-modify-private playlist-modify-public'); - // If you already have an auth token - SpotifyProvider.setAuthToken('zoasliu1248sdfuiknuha7882iu4rnuwehifskmkiuwhjg23'); + // If you already have an access token + SpotifyProvider.setAccessToken('zoasliu1248sdfuiknuha7882iu4rnuwehifskmkiuwhjg23'); }); ``` @@ -798,7 +798,7 @@ Get Spotify catalog information about artists, albums, or tracks that match a ke ```js Spotify.search('Search Query', 'type', options); ``` -- type - Required. A comma-separated list of item types to search across. Valid types are: album, artist, playlist, and track. +- type - Required. An array or comma-separated list of item types to search across. Valid types are: album, artist, playlist, and track. ##### Options Object (Optional) - limit - Optional. The maximum number of objects to return. Default: 20. Minimum: 1. Maximum: 50. @@ -812,6 +812,25 @@ Spotify.search('Nirvana', 'artist').then(function (data) { }); ``` +#### Search for Albums +```js +Spotify.searchAlbums('Search Query', options); +``` + +#### Search for Artists +```js +Spotify.searchArtists('Search Query', options); +``` + +#### Search for Tracks +```js +Spotify.searchTracks('Search Query', options); +``` + +#### Search for Playlists +```js +Spotify.searchPlaylists('Search Query', options); +``` ### Tracks #### Get a Track diff --git a/src/angular-spotify.js b/src/angular-spotify.js index 49ffe26..94a670d 100644 --- a/src/angular-spotify.js +++ b/src/angular-spotify.js @@ -22,7 +22,7 @@ settings.clientId = null; settings.redirectUri = null; settings.scope = null; - settings.authToken = null; + settings.accessToken = null; this.setClientId = function (clientId) { settings.clientId = clientId; @@ -33,9 +33,9 @@ return settings.clientId; }; - this.setAuthToken = function (authToken) { - settings.authToken = authToken; - return settings.authToken; + this.setAccessToken = function (accessToken) { + settings.accessToken = accessToken; + return settings.accessToken; }; this.setRedirectUri = function (redirectUri) { @@ -73,7 +73,7 @@ this.redirectUri = settings.redirectUri; this.apiBase = settings.apiBase; this.scope = settings.scope; - this.authToken = settings.authToken; + this.accessToken = settings.accessToken; this.toQueryString = utils.toQueryString; } @@ -96,8 +96,8 @@ options = options || {}; options.method = options.method || 'GET'; options.headers = options.headers || {}; - if (this.authToken) { - options.headers.Authorization = 'Bearer ' + this.authToken; + if (this.accessToken) { + options.headers.Authorization = 'Bearer ' + this.accessToken; } $http({ @@ -533,6 +533,22 @@ }); }, + searchAlbums: function (q, options) { + return this.search(q, 'album', options); + }, + + searchArtists: function (q, options) { + return this.search(q, 'artist', options); + }, + + searchTracks: function (q, options) { + return this.search(q, 'track', options); + }, + + searchPlaylists: function (q, options) { + return this.search(q, 'playlist', options); + }, + /** ====================== Tracks ===================== @@ -569,13 +585,13 @@ /** ====================== Login ===================== */ - setAuthToken: function (authToken) { - this.authToken = authToken; - return this.authToken; + setAccessToken: function (accessToken) { + this.accessToken = accessToken; + return this.accessToken; }, - getAuthToken: function () { - return this.authToken; + getAccessToken: function () { + return this.accessToken; }, login: function () { @@ -611,7 +627,7 @@ if (authWindow) { authWindow.close(); } authCompleted = true; - that.setAuthToken(e.newValue); + that.setAccessToken(e.newValue); $window.removeEventListener('storage', storageChanged, false); deferred.resolve(e.newValue); diff --git a/test/spec/angular-spotify.spec.js b/test/spec/angular-spotify.spec.js index 0337118..bacc0ab 100644 --- a/test/spec/angular-spotify.spec.js +++ b/test/spec/angular-spotify.spec.js @@ -37,7 +37,7 @@ describe('angular-spotify', function () { }); it('should set the authToken', function () { - expect(spotifyProvider.setAuthToken('ABCDEFGHIJKLMNOP')).toBe('ABCDEFGHIJKLMNOP'); + expect(spotifyProvider.setAccessToken('ABCDEFGHIJKLMNOP')).toBe('ABCDEFGHIJKLMNOP'); }); }); @@ -58,12 +58,12 @@ describe('angular-spotify', function () { }); it('should set the AuthToken', function () { - expect(Spotify.setAuthToken('ABCDEFGHIJKLMNOP')).toBe('ABCDEFGHIJKLMNOP'); + expect(Spotify.setAccessToken('ABCDEFGHIJKLMNOP')).toBe('ABCDEFGHIJKLMNOP'); }); it('should get the AuthToken', function () { - Spotify.setAuthToken('ABCDEFGHIJKLMNOP'); - expect(Spotify.getAuthToken()).toBe('ABCDEFGHIJKLMNOP'); + Spotify.setAccessToken('ABCDEFGHIJKLMNOP'); + expect(Spotify.getAccessToken()).toBe('ABCDEFGHIJKLMNOP'); }); it('should turn an object into a query string', function () { @@ -137,7 +137,7 @@ describe('angular-spotify', function () { }).respond({}); var result; - Spotify.setAuthToken('TESTING'); + Spotify.setAccessToken('TESTING'); Spotify.api('/users/wizzler/playlists', { method: 'POST', data: { @@ -154,8 +154,7 @@ describe('angular-spotify', function () { }); }); - describe('Spotify.search', function () { - + describe('Search', function () { var $httpBackend; var $rootScope; var Spotify; @@ -168,46 +167,178 @@ describe('angular-spotify', function () { jasmine.getJSONFixtures().fixturesPath='base/test/mock'; })); - it('should make an ajax call to https://api.spotify.com/v1/search', function () { + describe('Spotify.search', function () { + it('should make an ajax call to https://api.spotify.com/v1/search', function () { - spyOn(Spotify, 'api'); + spyOn(Spotify, 'api'); - Spotify.search('Nirvana', 'artist'); + Spotify.search('Nirvana', 'artist'); - expect(Spotify.api).toHaveBeenCalledWith('/search', { - params: { - q: 'Nirvana', - type: 'artist' - } + expect(Spotify.api).toHaveBeenCalledWith('/search', { + params: { + q: 'Nirvana', + type: 'artist' + } + }); + }); + + it('should call with multiple types', function () { + + spyOn(Spotify, 'api'); + + Spotify.search('Muse', ['artist', 'track']); + + expect(Spotify.api).toHaveBeenCalledWith('/search', { + params: { + q: 'Muse', + type: 'artist,track' + } + }); + }); + + it('should return an array of artists', function () { + $httpBackend.when('GET', api + '/search?q=Nirvana&type=artist').respond( + getJSONFixture('search.artist.json') + ); + + Spotify.search('Nirvana', 'artist').then(function (data) { + expect(data).toBeDefined(); + expect(data.artists.items.length).toBe(20); + }); + + $httpBackend.flush(); + }); + + it('should reject the promise and respond with error', function () { + $httpBackend.when('GET', api + '/search?q=Nirvana').respond(400, getJSONFixture('search.missing-type.json')); + + var result; + Spotify.search('Nirvana').then(function () { + }, function (reason) { + result = reason; + }); + + $httpBackend.flush(); + expect(result).toBeDefined(); + expect(result instanceof Object).toBeTruthy(); + expect(result.error.status).toBe(400); }); }); - it('should return an array of artists', function () { - $httpBackend.when('GET', api + '/search?q=Nirvana&type=artist').respond( - getJSONFixture('search.artist.json') - ); + describe('Spotify.searchAlbums', function () { + it('should call the api with the correct params', function () { + spyOn(Spotify, 'api'); - Spotify.search('Nirvana', 'artist').then(function (data) { - expect(data).toBeDefined(); - expect(data.artists.items.length).toBe(20); + Spotify.searchAlbums('Nevermind'); + + expect(Spotify.api).toHaveBeenCalledWith('/search', { + params: { + q: 'Nevermind', + type: 'album' + } + }); }); - $httpBackend.flush(); + it('should search with options', function () { + spyOn(Spotify, 'api'); + + Spotify.searchAlbums('Nevermind', { limit: 10 }); + + expect(Spotify.api).toHaveBeenCalledWith('/search', { + params: { + q: 'Nevermind', + type: 'album', + limit: 10 + } + }); + }); }); - it('should reject the promise and respond with error', function () { - $httpBackend.when('GET', api + '/search?q=Nirvana').respond(400, getJSONFixture('search.missing-type.json')); + describe('Spotify.searchArtists', function () { + it('should call the api with the correct params', function () { + spyOn(Spotify, 'api'); + + Spotify.searchArtists('Nirvana'); - var result; - Spotify.search('Nirvana').then(function () { - }, function (reason) { - result = reason; + expect(Spotify.api).toHaveBeenCalledWith('/search', { + params: { + q: 'Nirvana', + type: 'artist' + } + }); }); - $httpBackend.flush(); - expect(result).toBeDefined(); - expect(result instanceof Object).toBeTruthy(); - expect(result.error.status).toBe(400); + it('should search with options', function () { + spyOn(Spotify, 'api'); + + Spotify.searchArtists('Nirvana', { limit: 10 }); + + expect(Spotify.api).toHaveBeenCalledWith('/search', { + params: { + q: 'Nirvana', + type: 'artist', + limit: 10 + } + }); + }); + }); + + describe('Spotify.searchTracks', function () { + it('should call the api with the correct params', function () { + spyOn(Spotify, 'api'); + + Spotify.searchTracks('Smells Like Teen Spirit'); + + expect(Spotify.api).toHaveBeenCalledWith('/search', { + params: { + q: 'Smells Like Teen Spirit', + type: 'track' + } + }); + }); + + it('should search with options', function () { + spyOn(Spotify, 'api'); + + Spotify.searchTracks('Smells Like Teen Spirit', { limit: 10 }); + + expect(Spotify.api).toHaveBeenCalledWith('/search', { + params: { + q: 'Smells Like Teen Spirit', + type: 'track', + limit: 10 + } + }); + }); + }); + + describe('Spotify.searchPlaylists', function () { + it('should call the api with the correct params', function () { + spyOn(Spotify, 'api'); + + Spotify.searchPlaylists('#ThrowbackThursday'); + + expect(Spotify.api).toHaveBeenCalledWith('/search', { + params: { + q: '#ThrowbackThursday', + type: 'playlist' + } + }); + }); + + it('should search with options', function () { + spyOn(Spotify, 'api'); + + Spotify.searchPlaylists('#ThrowbackThursday', { limit: 10 }); + + expect(Spotify.api).toHaveBeenCalledWith('/search', { + params: { + q: '#ThrowbackThursday', + type: 'playlist', + limit: 10 + } + }); + }); }); }); @@ -956,7 +1087,7 @@ describe('angular-spotify', function () { it('should call the correct URL', function () { spyOn(Spotify, 'api'); - Spotify.setAuthToken('TESTING'); + Spotify.setAccessToken('TESTING'); Spotify.getUserPlaylists('wizzler'); @@ -971,7 +1102,7 @@ describe('angular-spotify', function () { it('should call the correct url', function () { spyOn(Spotify, 'api'); - Spotify.setAuthToken('TESTING'); + Spotify.setAccessToken('TESTING'); Spotify.getPlaylist('triple.j.abc', '73ppZmbaAS2aW9hmDTTDcb'); @@ -986,7 +1117,7 @@ describe('angular-spotify', function () { it('should call the correct url', function () { spyOn(Spotify, 'api'); - Spotify.setAuthToken('TESTING'); + Spotify.setAccessToken('TESTING'); Spotify.getPlaylistTracks('triple.j.abc', '73ppZmbaAS2aW9hmDTTDcb'); @@ -1001,7 +1132,7 @@ describe('angular-spotify', function () { it('should call the correct url', function () { spyOn(Spotify, 'api'); - Spotify.setAuthToken('TESTING'); + Spotify.setAccessToken('TESTING'); Spotify.createPlaylist('1176458919', { name: 'Awesome Mix Vol. 1' @@ -1026,7 +1157,7 @@ describe('angular-spotify', function () { it('should call the correct url', function () { spyOn(Spotify, 'api'); - Spotify.setAuthToken('TESTING'); + Spotify.setAccessToken('TESTING'); Spotify.addTracksToPlaylist('triple.j.abc', '73ppZmbaAS2aW9hmDTTDcb', [ 'spotify:track:5LwukQO2fCx35GUUN6d6NW', @@ -1050,7 +1181,7 @@ describe('angular-spotify', function () { it('should be able to pass Track IDs', function () { spyOn(Spotify, 'api'); - Spotify.setAuthToken('TESTING'); + Spotify.setAccessToken('TESTING'); Spotify.addTracksToPlaylist('triple.j.abc', '73ppZmbaAS2aW9hmDTTDcb', [ '5LwukQO2fCx35GUUN6d6NW', @@ -1077,7 +1208,7 @@ describe('angular-spotify', function () { it('should call the correct url', function () { spyOn(Spotify, 'api'); - Spotify.setAuthToken('TESTING'); + Spotify.setAccessToken('TESTING'); Spotify.removeTracksFromPlaylist('triple.j.abc', '73ppZmbaAS2aW9hmDTTDcb', [ 'spotify:track:5LwukQO2fCx35GUUN6d6NW', @@ -1104,7 +1235,7 @@ describe('angular-spotify', function () { it('should be able to pass track IDs', function () { spyOn(Spotify, 'api'); - Spotify.setAuthToken('TESTING'); + Spotify.setAccessToken('TESTING'); Spotify.removeTracksFromPlaylist('triple.j.abc', '73ppZmbaAS2aW9hmDTTDcb', [ '5LwukQO2fCx35GUUN6d6NW', @@ -1133,7 +1264,7 @@ describe('angular-spotify', function () { it('should call the correct url', function () { spyOn(Spotify, 'api'); - Spotify.setAuthToken('TESTING'); + Spotify.setAccessToken('TESTING'); Spotify.reorderTracksInPlaylist('triple.j.abc', '73ppZmbaAS2aW9hmDTTDcb', { range_start: 2, @@ -1160,7 +1291,7 @@ describe('angular-spotify', function () { it('should call the correct url', function () { spyOn(Spotify, 'api'); - Spotify.setAuthToken('TESTING'); + Spotify.setAccessToken('TESTING'); Spotify.replaceTracksInPlaylist('triple.j.abc', '73ppZmbaAS2aW9hmDTTDcb', [ 'spotify:track:5LwukQO2fCx35GUUN6d6NW', @@ -1183,7 +1314,7 @@ describe('angular-spotify', function () { it('should be able to pass track IDs', function () { spyOn(Spotify, 'api'); - Spotify.setAuthToken('TESTING'); + Spotify.setAccessToken('TESTING'); Spotify.replaceTracksInPlaylist('triple.j.abc', '73ppZmbaAS2aW9hmDTTDcb', [ '5LwukQO2fCx35GUUN6d6NW', @@ -1209,7 +1340,7 @@ describe('angular-spotify', function () { it('should call the correct URL', function () { spyOn(Spotify, 'api'); - Spotify.setAuthToken('TESTING'); + Spotify.setAccessToken('TESTING'); Spotify.changePlaylistDetails('1176458919', '3ygKiRcD8ed3i2g8P7jlXr', { name: 'Awesome Mix Vol. 2' @@ -1285,7 +1416,7 @@ describe('angular-spotify', function () { it('should call the correct URL', function () { spyOn(Spotify, 'api'); - Spotify.setAuthToken('TESTING'); + Spotify.setAccessToken('TESTING'); Spotify.getMe(); @@ -1304,7 +1435,7 @@ describe('angular-spotify', function () { it('should call the correct URL', function () { spyOn(Spotify, 'api'); - Spotify.setAuthToken('TESTING'); + Spotify.setAccessToken('TESTING'); Spotify.getMySavedTracks(); @@ -1319,7 +1450,7 @@ describe('angular-spotify', function () { it('should call the correct URL', function () { spyOn(Spotify, 'api'); - Spotify.setAuthToken('TESTING'); + Spotify.setAccessToken('TESTING'); Spotify.containsMySavedTracks(['0udZHhCi7p1YzMlvI4fXoK','3SF5puV5eb6bgRSxBeMOk9']); @@ -1334,7 +1465,7 @@ describe('angular-spotify', function () { it('should be able to pass Spotify URIs', function () { spyOn(Spotify, 'api'); - Spotify.setAuthToken('TESTING'); + Spotify.setAccessToken('TESTING'); Spotify.containsMySavedTracks(['spotify:track:0udZHhCi7p1YzMlvI4fXoK','spotify:track:3SF5puV5eb6bgRSxBeMOk9']); @@ -1353,7 +1484,7 @@ describe('angular-spotify', function () { it('should call the correct URL', function () { spyOn(Spotify, 'api'); - Spotify.setAuthToken('TESTING'); + Spotify.setAccessToken('TESTING'); Spotify.addToMySavedTracks(['4iV5W9uYEdYUVa79Axb7Rh','1301WleyT98MSxVHPZCA6M']); @@ -1369,7 +1500,7 @@ describe('angular-spotify', function () { it('should be able to pass Spotify URIs', function () { spyOn(Spotify, 'api'); - Spotify.setAuthToken('TESTING'); + Spotify.setAccessToken('TESTING'); Spotify.addToMySavedTracks(['spotify:track:0udZHhCi7p1YzMlvI4fXoK','spotify:track:3SF5puV5eb6bgRSxBeMOk9']); @@ -1389,7 +1520,7 @@ describe('angular-spotify', function () { it('should call the correct URL', function () { spyOn(Spotify, 'api'); - Spotify.setAuthToken('TESTING'); + Spotify.setAccessToken('TESTING'); Spotify.removeFromMySavedTracks(['4iV5W9uYEdYUVa79Axb7Rh','1301WleyT98MSxVHPZCA6M']); @@ -1408,7 +1539,7 @@ describe('angular-spotify', function () { it('should be able to pass Spotify URIs', function () { spyOn(Spotify, 'api'); - Spotify.setAuthToken('TESTING'); + Spotify.setAccessToken('TESTING'); Spotify.removeFromMySavedTracks(['spotify:track:0udZHhCi7p1YzMlvI4fXoK','spotify:track:3SF5puV5eb6bgRSxBeMOk9']); @@ -1431,7 +1562,7 @@ describe('angular-spotify', function () { it('should call the correct URL', function () { spyOn(Spotify, 'api'); - Spotify.setAuthToken('TESTING'); + Spotify.setAccessToken('TESTING'); Spotify.getMySavedAlbums(); @@ -1446,7 +1577,7 @@ describe('angular-spotify', function () { it('should call the correct URL', function () { spyOn(Spotify, 'api'); - Spotify.setAuthToken('TESTING'); + Spotify.setAccessToken('TESTING'); Spotify.addToMySavedAlbums(['4iV5W9uYEdYUVa79Axb7Rh','1301WleyT98MSxVHPZCA6M']); @@ -1462,7 +1593,7 @@ describe('angular-spotify', function () { it('should be able to pass Spotify URIs', function () { spyOn(Spotify, 'api'); - Spotify.setAuthToken('TESTING'); + Spotify.setAccessToken('TESTING'); Spotify.addToMySavedAlbums(['spotify:album:4iV5W9uYEdYUVa79Axb7Rh','spotify:album:1301WleyT98MSxVHPZCA6M']); @@ -1482,7 +1613,7 @@ describe('angular-spotify', function () { it('should call the correct URL', function () { spyOn(Spotify, 'api'); - Spotify.setAuthToken('TESTING'); + Spotify.setAccessToken('TESTING'); Spotify.removeFromMySavedAlbums(['4iV5W9uYEdYUVa79Axb7Rh','1301WleyT98MSxVHPZCA6M']); @@ -1501,7 +1632,7 @@ describe('angular-spotify', function () { it('should be able to pass Spotify URIs', function () { spyOn(Spotify, 'api'); - Spotify.setAuthToken('TESTING'); + Spotify.setAccessToken('TESTING'); Spotify.removeFromMySavedAlbums(['spotify:album:4iV5W9uYEdYUVa79Axb7Rh','spotify:album:1301WleyT98MSxVHPZCA6M']); @@ -1524,7 +1655,7 @@ describe('angular-spotify', function () { it('should call the correct URL', function () { spyOn(Spotify, 'api'); - Spotify.setAuthToken('TESTING'); + Spotify.setAccessToken('TESTING'); Spotify.containsMySavedAlbums(['4iV5W9uYEdYUVa79Axb7Rh','1301WleyT98MSxVHPZCA6M']); @@ -1539,7 +1670,7 @@ describe('angular-spotify', function () { it('should be able to pass Spotify URIs', function () { spyOn(Spotify, 'api'); - Spotify.setAuthToken('TESTING'); + Spotify.setAccessToken('TESTING'); Spotify.containsMySavedAlbums(['spotify:album:4iV5W9uYEdYUVa79Axb7Rh','spotify:album:1301WleyT98MSxVHPZCA6M']); @@ -1560,7 +1691,7 @@ describe('angular-spotify', function () { it('should call the correct URL with authentication', function () { spyOn(Spotify, 'api'); - Spotify.setAuthToken('TESTING'); + Spotify.setAccessToken('TESTING'); Spotify.getMyTopArtists(); @@ -1571,7 +1702,7 @@ describe('angular-spotify', function () { it('should call the correct URL with authentication and options', function () { spyOn(Spotify, 'api'); - Spotify.setAuthToken('TESTING'); + Spotify.setAccessToken('TESTING'); Spotify.getMyTopArtists({ limit: 50, @@ -1592,7 +1723,7 @@ describe('angular-spotify', function () { it('should call the correct URL with authentication', function () { spyOn(Spotify, 'api'); - Spotify.setAuthToken('TESTING'); + Spotify.setAccessToken('TESTING'); Spotify.getMyTopTracks(); @@ -1603,7 +1734,7 @@ describe('angular-spotify', function () { it('should call the correct URL with authentication and options', function () { spyOn(Spotify, 'api'); - Spotify.setAuthToken('TESTING'); + Spotify.setAccessToken('TESTING'); Spotify.getMyTopTracks({ limit: 50, @@ -1636,7 +1767,7 @@ describe('angular-spotify', function () { it('should call the correct URL with authentication and options', function () { spyOn(Spotify, 'api'); - Spotify.setAuthToken('TESTING'); + Spotify.setAccessToken('TESTING'); Spotify.getFeaturedPlaylists({ country: 'NL', locale: 'nl_NL' }); @@ -1671,7 +1802,7 @@ describe('angular-spotify', function () { it('should call the correct URL with authentication and options', function () { spyOn(Spotify, 'api'); - Spotify.setAuthToken('TESTING'); + Spotify.setAccessToken('TESTING'); Spotify.getNewReleases({ country: 'NL' }); @@ -1703,7 +1834,7 @@ describe('angular-spotify', function () { it('should call the correct URL with authentication', function () { spyOn(Spotify, 'api'); - Spotify.setAuthToken('TESTING'); + Spotify.setAccessToken('TESTING'); Spotify.getCategories(); @@ -1714,7 +1845,7 @@ describe('angular-spotify', function () { it('should call the correct URL with authentication and options', function () { spyOn(Spotify, 'api'); - Spotify.setAuthToken('TESTING'); + Spotify.setAccessToken('TESTING'); Spotify.getCategories({ country: 'SG', @@ -1735,7 +1866,7 @@ describe('angular-spotify', function () { it('should call the correct URL with authentication', function () { spyOn(Spotify, 'api'); - Spotify.setAuthToken('TESTING'); + Spotify.setAccessToken('TESTING'); Spotify.getCategory('party'); @@ -1746,7 +1877,7 @@ describe('angular-spotify', function () { it('should call the correct URL with authentication and options', function () { spyOn(Spotify, 'api'); - Spotify.setAuthToken('TESTING'); + Spotify.setAccessToken('TESTING'); Spotify.getCategory('party', { country: 'SG' @@ -1765,7 +1896,7 @@ describe('angular-spotify', function () { it('should call the correct URL with authentication', function () { spyOn(Spotify, 'api'); - Spotify.setAuthToken('TESTING'); + Spotify.setAccessToken('TESTING'); Spotify.getCategoryPlaylists('party'); @@ -1776,7 +1907,7 @@ describe('angular-spotify', function () { it('should call the correct URL with authentication with options', function () { spyOn(Spotify, 'api'); - Spotify.setAuthToken('TESTING'); + Spotify.setAccessToken('TESTING'); Spotify.getCategoryPlaylists('party', { country: 'SG', @@ -1797,7 +1928,7 @@ describe('angular-spotify', function () { it('should call the correct URL with authentication', function () { spyOn(Spotify, 'api'); - Spotify.setAuthToken('TESTING'); + Spotify.setAccessToken('TESTING'); Spotify.getRecommendations({ seed_artists: '4NHQUGzhtTLFvgF5SZesLK' @@ -1816,7 +1947,7 @@ describe('angular-spotify', function () { it('should call the correct URL with authentication', function () { spyOn(Spotify, 'api'); - Spotify.setAuthToken('TESTING'); + Spotify.setAccessToken('TESTING'); Spotify.getAvailableGenreSeeds(); @@ -1839,7 +1970,7 @@ describe('angular-spotify', function () { it('should call the correct URL', function () { spyOn(Spotify, 'api'); - Spotify.setAuthToken('TESTING'); + Spotify.setAccessToken('TESTING'); Spotify.getFollowed('artist'); @@ -1854,7 +1985,7 @@ describe('angular-spotify', function () { it('should call with options', function() { spyOn(Spotify, 'api'); - Spotify.setAuthToken('TESTING'); + Spotify.setAccessToken('TESTING'); Spotify.getFollowed('artist', { limit: 30 }); @@ -1872,7 +2003,7 @@ describe('angular-spotify', function () { it('should call the correct URL', function () { spyOn(Spotify, 'api'); - Spotify.setAuthToken('TESTING'); + Spotify.setAccessToken('TESTING'); Spotify.follow('user', 'exampleuser01'); @@ -1889,7 +2020,7 @@ describe('angular-spotify', function () { it('should call with multiple ids', function() { spyOn(Spotify, 'api'); - Spotify.setAuthToken('TESTING'); + Spotify.setAccessToken('TESTING'); Spotify.follow('artist', '74ASZWbe4lXaubB36ztrGX,08td7MxkoHQkXnWAYD8d6Q'); @@ -1908,7 +2039,7 @@ describe('angular-spotify', function () { it('should call the correct URL', function () { spyOn(Spotify, 'api'); - Spotify.setAuthToken('TESTING'); + Spotify.setAccessToken('TESTING'); Spotify.unfollow('user', 'exampleuser01'); @@ -1925,7 +2056,7 @@ describe('angular-spotify', function () { it('should call with multiple ids', function() { spyOn(Spotify, 'api'); - Spotify.setAuthToken('TESTING'); + Spotify.setAccessToken('TESTING'); Spotify.unfollow('artist', '74ASZWbe4lXaubB36ztrGX,08td7MxkoHQkXnWAYD8d6Q'); @@ -1944,7 +2075,7 @@ describe('angular-spotify', function () { it('should call the correct URL', function () { spyOn(Spotify, 'api'); - Spotify.setAuthToken('TESTING'); + Spotify.setAccessToken('TESTING'); Spotify.isFollowing('user', 'exampleuser01'); @@ -1962,7 +2093,7 @@ describe('angular-spotify', function () { it ('should call the correct URL', function () { spyOn(Spotify, 'api'); - Spotify.setAuthToken('TESTING'); + Spotify.setAccessToken('TESTING'); Spotify.followPlaylist('jmperezperez', '2v3iNvBX8Ay1Gt2uXtUKUT'); @@ -1981,7 +2112,7 @@ describe('angular-spotify', function () { it('should be able to follow and set to public', function () { spyOn(Spotify, 'api'); - Spotify.setAuthToken('TESTING'); + Spotify.setAccessToken('TESTING'); Spotify.followPlaylist('jmperezperez', '2v3iNvBX8Ay1Gt2uXtUKUT', true); @@ -2002,7 +2133,7 @@ describe('angular-spotify', function () { it ('should call the correct URL', function () { spyOn(Spotify, 'api'); - Spotify.setAuthToken('TESTING'); + Spotify.setAccessToken('TESTING'); Spotify.unfollowPlaylist('jmperezperez', '2v3iNvBX8Ay1Gt2uXtUKUT'); @@ -2017,7 +2148,7 @@ describe('angular-spotify', function () { it('should call the correct URL', function () { spyOn(Spotify, 'api'); - Spotify.setAuthToken('TESTING'); + Spotify.setAccessToken('TESTING'); Spotify.areFollowingPlaylist('jmperezperez', '2v3iNvBX8Ay1Gt2uXtUKUT', 'possan,elogain'); @@ -2032,7 +2163,7 @@ describe('angular-spotify', function () { it('should be able to be called with an array of users', function () { spyOn(Spotify, 'api'); - Spotify.setAuthToken('TESTING'); + Spotify.setAccessToken('TESTING'); Spotify.areFollowingPlaylist('jmperezperez', '2v3iNvBX8Ay1Gt2uXtUKUT', ['possan','elogain']); @@ -2072,15 +2203,15 @@ describe('angular-spotify', function () { }); // it('should set the auth token', function (done) { - // spyOn(Spotify, 'setAuthToken'); + // spyOn(Spotify, 'setAccessToken'); // Spotify.login(); // setTimeout(function () { // localStorage.setItem('spotify-token', 'TESTINGTOKEN'); // }, 1000); // // console.log('spotify token: ', localStorage.getItem('spotify-token')); // setTimeout(function () { - // expect(Spotify.setAuthToken).toHaveBeenCalled(); - // expect(Spotify.setAuthToken).toHaveBeenCalledWith('TESTINGTOKEN'); + // expect(Spotify.setAccessToken).toHaveBeenCalled(); + // expect(Spotify.setAccessToken).toHaveBeenCalledWith('TESTINGTOKEN'); // done(); // }, 3500); // }); From db68f52f2cc268e52ab43c241425ebb5d80cc365 Mon Sep 17 00:00:00 2001 From: Ed Moore Date: Sat, 16 Apr 2016 09:55:59 +0800 Subject: [PATCH 05/13] Follow and unfollow users --- README.md | 20 ++++++++++++++++++++ src/angular-spotify.js | 23 +++++++++++++++++++++-- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index bb599f9..9a8928d 100644 --- a/README.md +++ b/README.md @@ -354,6 +354,16 @@ Spotify.follow('user', 'exampleuser01').then(function () { }); ``` +#### Follow Users +```js +Spotify.followUsers('ids'); +``` + +#### Follow Artists +```js +Spotify.followArtists('ids'); +``` + #### Unfollow Artists or Users Remove the current user as a follower of one or more artists or other Spotify users. ```js @@ -368,6 +378,16 @@ Spotify.unfollow('user', 'exampleuser01').then(function () { }); ``` +#### Unfollow Users +```js +Spotify.unfollowUsers('ids'); +``` + +#### Unfollow Artists +```js +Spotify.unfollowArtists('ids'); +``` + #### Check if Current User Follows Check to see if the current user is following one or more artists or other Spotify users. ```js diff --git a/src/angular-spotify.js b/src/angular-spotify.js index 94a670d..2e1a799 100644 --- a/src/angular-spotify.js +++ b/src/angular-spotify.js @@ -259,13 +259,24 @@ }); }, - follow: function (type, ids) { + follow: function (type, ids, options) { + var params = options || {}; + params.type = type; + params.ids = ids; return this.api('/me/following', { method: 'PUT', - params: { type: type, ids: ids } + params: params }); }, + followUsers: function (ids) { + return this.follow('user', ids); + }, + + followArtists: function (ids) { + return this.follow('artist', ids); + }, + unfollow: function (type, ids) { return this.api('/me/following', { method: 'DELETE', @@ -273,6 +284,14 @@ }); }, + unfollowUsers: function (ids) { + return this.unfollow('user', ids); + }, + + unfollowArtists: function (ids) { + return this.unfollow('artist', ids); + }, + isFollowing: function (type, ids) { return this.api('/me/following/contains', { params: { type: type, ids: ids } From 5899c111caf1ea87287ff674c7a14a6218f6cc5f Mon Sep 17 00:00:00 2001 From: Ed Moore Date: Sat, 16 Apr 2016 10:21:10 +0800 Subject: [PATCH 06/13] Updating following --- README.md | 10 ++ src/angular-spotify.js | 31 +++-- test/spec/angular-spotify.spec.js | 196 ++++++++++++++++++++++++++++++ 3 files changed, 230 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 9a8928d..3401827 100644 --- a/README.md +++ b/README.md @@ -403,6 +403,16 @@ Spotify.isFollowing('user', 'exampleuser01').then(function (data) { }); ``` +#### Check if Current User Follows Users +```js +Spotify.isFollowingUsers(ids); +``` + +#### Check if Current User Follows Artists +```js +Spotify.isFollowingArtists(ids); +``` + #### Follow a Playlist Add the current user as a follower of a playlist. Requires ```playlist-modify-public``` or ```playlist-modify-private``` scope to work. ```js diff --git a/src/angular-spotify.js b/src/angular-spotify.js index 2e1a799..8d0c825 100644 --- a/src/angular-spotify.js +++ b/src/angular-spotify.js @@ -259,13 +259,14 @@ }); }, - follow: function (type, ids, options) { - var params = options || {}; - params.type = type; - params.ids = ids; + follow: function (type, ids) { + var arr = angular.isArray(ids) ? ids : ids.split(','); return this.api('/me/following', { method: 'PUT', - params: params + params: { + type: type, + ids: arr.join(',') + } }); }, @@ -278,9 +279,13 @@ }, unfollow: function (type, ids) { + var arr = angular.isArray(ids) ? ids : ids.split(','); return this.api('/me/following', { method: 'DELETE', - params: { type: type, ids: ids } + params: { + type: type, + ids: arr.join(',') + } }); }, @@ -293,11 +298,23 @@ }, isFollowing: function (type, ids) { + var arr = angular.isArray(ids) ? ids : ids.split(','); return this.api('/me/following/contains', { - params: { type: type, ids: ids } + params: { + type: type, + ids: arr.join(',') + } }); }, + isFollowingUsers: function (ids) { + return this.isFollowing('user', ids); + }, + + isFollowingArtists: function (ids) { + return this.isFollowing('artist', ids); + }, + followPlaylist: function (userId, playlistId, isPublic) { return this.api('/users/' + userId + '/playlists/' + playlistId + '/followers', { method: 'PUT', diff --git a/test/spec/angular-spotify.spec.js b/test/spec/angular-spotify.spec.js index bacc0ab..dfa6b36 100644 --- a/test/spec/angular-spotify.spec.js +++ b/test/spec/angular-spotify.spec.js @@ -2035,6 +2035,78 @@ describe('angular-spotify', function () { }); }); + describe('Spotify.followUsers', function () { + it('should call the correct URL', function () { + spyOn(Spotify, 'api'); + + Spotify.setAccessToken('TESTING'); + + Spotify.followUsers('exampleuser01'); + + expect(Spotify.api).toHaveBeenCalled(); + expect(Spotify.api).toHaveBeenCalledWith('/me/following', { + method: 'PUT', + params: { + type: 'user', + ids: 'exampleuser01' + } + }); + }); + + it('should call with multiple ids', function() { + spyOn(Spotify, 'api'); + + Spotify.setAccessToken('TESTING'); + + Spotify.followUsers(['74ASZWbe4lXaubB36ztrGX','08td7MxkoHQkXnWAYD8d6Q']); + + expect(Spotify.api).toHaveBeenCalled(); + expect(Spotify.api).toHaveBeenCalledWith('/me/following', { + method: 'PUT', + params: { + type: 'user', + ids: '74ASZWbe4lXaubB36ztrGX,08td7MxkoHQkXnWAYD8d6Q' + } + }); + }); + }); + + describe('Spotify.followArtists', function () { + it('should call the correct URL', function () { + spyOn(Spotify, 'api'); + + Spotify.setAccessToken('TESTING'); + + Spotify.followArtists('exampleartist01'); + + expect(Spotify.api).toHaveBeenCalled(); + expect(Spotify.api).toHaveBeenCalledWith('/me/following', { + method: 'PUT', + params: { + type: 'artist', + ids: 'exampleartist01' + } + }); + }); + + it('should call with multiple ids', function() { + spyOn(Spotify, 'api'); + + Spotify.setAccessToken('TESTING'); + + Spotify.followArtists('74ASZWbe4lXaubB36ztrGX,08td7MxkoHQkXnWAYD8d6Q'); + + expect(Spotify.api).toHaveBeenCalled(); + expect(Spotify.api).toHaveBeenCalledWith('/me/following', { + method: 'PUT', + params: { + type: 'artist', + ids: '74ASZWbe4lXaubB36ztrGX,08td7MxkoHQkXnWAYD8d6Q' + } + }); + }); + }); + describe('Spotify.unfollow', function () { it('should call the correct URL', function () { spyOn(Spotify, 'api'); @@ -2071,6 +2143,78 @@ describe('angular-spotify', function () { }); }); + describe('Spotify.unfollowUsers', function () { + it('should call the correct URL', function () { + spyOn(Spotify, 'api'); + + Spotify.setAccessToken('TESTING'); + + Spotify.unfollowUsers('exampleuser01'); + + expect(Spotify.api).toHaveBeenCalled(); + expect(Spotify.api).toHaveBeenCalledWith('/me/following', { + method: 'DELETE', + params: { + type: 'user', + ids: 'exampleuser01' + } + }); + }); + + it('should call with multiple ids', function() { + spyOn(Spotify, 'api'); + + Spotify.setAccessToken('TESTING'); + + Spotify.unfollowUsers('74ASZWbe4lXaubB36ztrGX,08td7MxkoHQkXnWAYD8d6Q'); + + expect(Spotify.api).toHaveBeenCalled(); + expect(Spotify.api).toHaveBeenCalledWith('/me/following', { + method: 'DELETE', + params: { + type: 'user', + ids: '74ASZWbe4lXaubB36ztrGX,08td7MxkoHQkXnWAYD8d6Q' + } + }); + }); + }); + + describe('Spotify.unfollowArtists', function () { + it('should call the correct URL', function () { + spyOn(Spotify, 'api'); + + Spotify.setAccessToken('TESTING'); + + Spotify.unfollowArtists('exampleartist01'); + + expect(Spotify.api).toHaveBeenCalled(); + expect(Spotify.api).toHaveBeenCalledWith('/me/following', { + method: 'DELETE', + params: { + type: 'artist', + ids: 'exampleartist01' + } + }); + }); + + it('should call with multiple ids', function() { + spyOn(Spotify, 'api'); + + Spotify.setAccessToken('TESTING'); + + Spotify.unfollowArtists('74ASZWbe4lXaubB36ztrGX,08td7MxkoHQkXnWAYD8d6Q'); + + expect(Spotify.api).toHaveBeenCalled(); + expect(Spotify.api).toHaveBeenCalledWith('/me/following', { + method: 'DELETE', + params: { + type: 'artist', + ids: '74ASZWbe4lXaubB36ztrGX,08td7MxkoHQkXnWAYD8d6Q' + } + }); + }); + }); + describe('Spotify.isFollowing', function() { it('should call the correct URL', function () { spyOn(Spotify, 'api'); @@ -2087,6 +2231,58 @@ describe('angular-spotify', function () { } }); }); + + it('should call with multiple ids', function () { + spyOn(Spotify, 'api'); + + Spotify.setAccessToken('TESTING'); + + Spotify.isFollowing('user', ['exampleuser01', 'exampleuser02']); + + expect(Spotify.api).toHaveBeenCalled(); + expect(Spotify.api).toHaveBeenCalledWith('/me/following/contains', { + params: { + type: 'user', + ids: 'exampleuser01,exampleuser02' + } + }); + }); + }); + + describe('Spotify.isFollowingUsers', function() { + it('should call the correct URL', function () { + spyOn(Spotify, 'api'); + + Spotify.setAccessToken('TESTING'); + + Spotify.isFollowingUsers('exampleuser01'); + + expect(Spotify.api).toHaveBeenCalled(); + expect(Spotify.api).toHaveBeenCalledWith('/me/following/contains', { + params: { + type: 'user', + ids: 'exampleuser01' + } + }); + }); + }); + + describe('Spotify.isFollowingArtists', function() { + it('should call the correct URL', function () { + spyOn(Spotify, 'api'); + + Spotify.setAccessToken('TESTING'); + + Spotify.isFollowingArtists('exampleartist01'); + + expect(Spotify.api).toHaveBeenCalled(); + expect(Spotify.api).toHaveBeenCalledWith('/me/following/contains', { + params: { + type: 'artist', + ids: 'exampleartist01' + } + }); + }); }); describe('Spotify.followPlaylist', function () { From ddf32e23f3272a8d9372cb2e09750c2ace0f9afe Mon Sep 17 00:00:00 2001 From: Ed Moore Date: Sat, 16 Apr 2016 10:40:25 +0800 Subject: [PATCH 07/13] Added Spotify.removeTracksFromPlaylistWithSnapshotId --- src/angular-spotify.js | 35 ++++++++++++++++++++++--------- test/spec/angular-spotify.spec.js | 30 ++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 10 deletions(-) diff --git a/src/angular-spotify.js b/src/angular-spotify.js index 8d0c825..deea643 100644 --- a/src/angular-spotify.js +++ b/src/angular-spotify.js @@ -486,24 +486,39 @@ }); }, - removeTracksFromPlaylist: function (userId, playlistId, tracks) { - var arr = angular.isArray(tracks) ? tracks : tracks.split(','); - var trks = arr.map(function (track) { - return { - uri: track.indexOf('spotify:') === -1 ? 'spotify:track:' + track : track - }; - }); + removeTracksFromPlaylist: function (userId, playlistId, tracks, options) { + var data = options || {}; + if (tracks) { + var arr = angular.isArray(tracks) ? tracks : tracks.split(','); + var trks = arr.map(function (track) { + return { + uri: track.indexOf('spotify:') === -1 ? 'spotify:track:' + track : track + }; + }); + data.tracks = trks; + } return this.api('/users/' + userId + '/playlists/' + playlistId + '/tracks', { method: 'DELETE', - data: { - tracks: trks - }, + data: data, headers: { 'Content-Type': 'application/json' } }); }, + removeTracksFromPlaylistWithSnapshotId: function (userId, playlistId, tracks, snapshotId) { + return this.removeTracksFromPlaylist(userId, playlistId, tracks, { + snapshot_id: snapshotId + }); + }, + + removeTracksFromPlaylistInPositions: function (userId, playlistId, positions, snapshotId) { + return this.removeTracksFromPlaylist(userId, playlistId, null, { + positions: positions, + snapshot_id: snapshotId + }) + }, + reorderTracksInPlaylist: function (userId, playlistId, options) { return this.api('/users/' + userId + '/playlists/' + playlistId + '/tracks', { method: 'PUT', diff --git a/test/spec/angular-spotify.spec.js b/test/spec/angular-spotify.spec.js index dfa6b36..60c42ac 100644 --- a/test/spec/angular-spotify.spec.js +++ b/test/spec/angular-spotify.spec.js @@ -1260,6 +1260,36 @@ describe('angular-spotify', function () { }); }); + describe('Spotify.removeTracksFromPlaylistWithSnapshotId', function () { + it('should call with the correct params', function () { + spyOn(Spotify, 'api'); + + Spotify.setAccessToken('TESTING'); + + Spotify.removeTracksFromPlaylistWithSnapshotId('triple.j.abc', '73ppZmbaAS2aW9hmDTTDcb', [ + '5LwukQO2fCx35GUUN6d6NW', + '4w8CsAnzn0lXJxYlAuCtCW', + '2Foc5Q5nqNiosCNqttzHof' + ], 'a1b2c3d4e5f6g7h8i9'); + + expect(Spotify.api).toHaveBeenCalled(); + expect(Spotify.api).toHaveBeenCalledWith('/users/triple.j.abc/playlists/73ppZmbaAS2aW9hmDTTDcb/tracks', { + method: 'DELETE', + data: { + tracks: [ + {uri: 'spotify:track:5LwukQO2fCx35GUUN6d6NW'}, + {uri: 'spotify:track:4w8CsAnzn0lXJxYlAuCtCW'}, + {uri: 'spotify:track:2Foc5Q5nqNiosCNqttzHof'} + ], + snapshot_id: 'a1b2c3d4e5f6g7h8i9' + }, + headers: { + 'Content-Type': 'application/json' + } + }); + }); + }); + describe('Spotify.reorderTracksInPlaylist', function () { it('should call the correct url', function () { spyOn(Spotify, 'api'); From 47206342280fe2da165ddead8a658214552da37b Mon Sep 17 00:00:00 2001 From: Ed Moore Date: Sat, 16 Apr 2016 10:47:07 +0800 Subject: [PATCH 08/13] Testing removeTracksFromPlaylistInPositions --- test/spec/angular-spotify.spec.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/test/spec/angular-spotify.spec.js b/test/spec/angular-spotify.spec.js index 60c42ac..4e3c27c 100644 --- a/test/spec/angular-spotify.spec.js +++ b/test/spec/angular-spotify.spec.js @@ -1290,6 +1290,28 @@ describe('angular-spotify', function () { }); }); + describe('Spotify.removeTracksFromPlaylistInPositions', function () { + it('should call with the correct params', function () { + spyOn(Spotify, 'api'); + + Spotify.setAccessToken('TESTING'); + + Spotify.removeTracksFromPlaylistInPositions('triple.j.abc', '73ppZmbaAS2aW9hmDTTDcb', [1,3,5,7,9], 'a1b2c3d4e5f6g7h8i9'); + + expect(Spotify.api).toHaveBeenCalled(); + expect(Spotify.api).toHaveBeenCalledWith('/users/triple.j.abc/playlists/73ppZmbaAS2aW9hmDTTDcb/tracks', { + method: 'DELETE', + data: { + positions: [1,3,5,7,9], + snapshot_id: 'a1b2c3d4e5f6g7h8i9' + }, + headers: { + 'Content-Type': 'application/json' + } + }); + }); + }); + describe('Spotify.reorderTracksInPlaylist', function () { it('should call the correct url', function () { spyOn(Spotify, 'api'); From cfb5e08e7e82e0ecd241e394830ad6aede64ddcc Mon Sep 17 00:00:00 2001 From: Ed Moore Date: Sat, 16 Apr 2016 10:49:35 +0800 Subject: [PATCH 09/13] Added semicolon --- src/angular-spotify.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/angular-spotify.js b/src/angular-spotify.js index deea643..4b20dd2 100644 --- a/src/angular-spotify.js +++ b/src/angular-spotify.js @@ -516,7 +516,7 @@ return this.removeTracksFromPlaylist(userId, playlistId, null, { positions: positions, snapshot_id: snapshotId - }) + }); }, reorderTracksInPlaylist: function (userId, playlistId, options) { From e26d0b90ac229c6c520533258a862fa6fd99223a Mon Sep 17 00:00:00 2001 From: Ed Moore Date: Mon, 18 Apr 2016 12:05:24 +0800 Subject: [PATCH 10/13] Updated Readme --- README.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/README.md b/README.md index 3401827..ffdfa72 100644 --- a/README.md +++ b/README.md @@ -734,6 +734,34 @@ Spotify }); ``` +##### Remove Tracks from a Playlist given snapshot ID +```js +Spotify.removeTracksFromPlaylistWithSnapshotId('user_id', 'playlist_id', 'comma separated string or array of spotify track ids or uris', 'snapshot_id'); +``` + +Example: +```js +Spotify + .removeTracksFromPlaylistWithSnapshotId('1176458919', '2TkWjGCu8jurholsfdWtG4', ['spotify:track:4iV5W9uYEdYUVa79Axb7Rh', 'spotify:track:1301WleyT98MSxVHPZCA6M'], 'JbtmHBDBAYu3/bt8BOXKjzKx3i0b6LCa/wVjyl6qQ2Yf6nFXkbmzuEa+ZI/U1yF+') + .then(function (data) { + console.log('tracks removed from playlist'); + }); +``` + +##### Remove Tracks from a Playlist in positions +```js +Spotify.removeTracksFromPlaylistInPositions('user_id', 'playlist_id', 'array of positions', 'snapshot_id'); +``` + +Example: +```js +Spotify + .removeTracksFromPlaylistInPositions('1176458919', '2TkWjGCu8jurholsfdWtG4', [1,3,5,7,9], 'JbtmHBDBAYu3/bt8BOXKjzKx3i0b6LCa/wVjyl6qQ2Yf6nFXkbmzuEa+ZI/U1yF+') + .then(function (data) { + console.log('tracks removed from playlist'); + }); +``` + #### Reorder a Playlist's Tracks Reorder a track or a group of tracks in a playlist. ```js From 7126779effdbc0a36e37f57c6f12d85b27437de9 Mon Sep 17 00:00:00 2001 From: Ed Moore Date: Thu, 9 Jun 2016 09:54:21 +0800 Subject: [PATCH 11/13] Fixing naming in example controller --- examples/main.controller.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/main.controller.js b/examples/main.controller.js index 1a7202d..7338273 100644 --- a/examples/main.controller.js +++ b/examples/main.controller.js @@ -80,7 +80,7 @@ angular console.log(data); }); - Spotify.getRelatedArtists('0LcJLqbBmaGUft1e9Mm8HV').then(function (data) { + Spotify.getArtistRelatedArtists('0LcJLqbBmaGUft1e9Mm8HV').then(function (data) { console.log('=================== Get Releated Artists ==================='); console.log(data); }); From 69702fdf72a17b63e352196a147fb283da65190b Mon Sep 17 00:00:00 2001 From: Ed Moore Date: Thu, 9 Jun 2016 09:54:30 +0800 Subject: [PATCH 12/13] Cleaning up code --- src/angular-spotify.js | 45 +++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/src/angular-spotify.js b/src/angular-spotify.js index 4b20dd2..8e2b02d 100644 --- a/src/angular-spotify.js +++ b/src/angular-spotify.js @@ -1,4 +1,4 @@ -(function (window, angular, undefined) { +(function (window, angular) { 'use strict'; function getSpotifyId (s) { @@ -18,15 +18,15 @@ .provider('Spotify', function () { // Module global settings. - var settings = {}; - settings.clientId = null; - settings.redirectUri = null; - settings.scope = null; - settings.accessToken = null; + var settings = { + clientId: null, + redirectUri: null, + scope: null, + accessToken: null + }; this.setClientId = function (clientId) { - settings.clientId = clientId; - return settings.clientId; + return settings.clientId = clientId; }; this.getClientId = function () { @@ -34,13 +34,11 @@ }; this.setAccessToken = function (accessToken) { - settings.accessToken = accessToken; - return settings.accessToken; + return settings.accessToken = accessToken; }; this.setRedirectUri = function (redirectUri) { - settings.redirectUri = redirectUri; - return settings.redirectUri; + return settings.redirectUri = redirectUri; }; this.getRedirectUri = function () { @@ -48,13 +46,12 @@ }; this.setScope = function (scope) { - settings.scope = scope; - return settings.scope; + return settings.scope = scope; }; var utils = {}; utils.toQueryString = function (obj) { - var parts = []; + var parts = [] angular.forEach(obj, function (value, key) { this.push(encodeURIComponent(key) + '=' + encodeURIComponent(value)); }, parts); @@ -83,9 +80,11 @@ try { if (!win || win.closed) { window.clearInterval(interval); - cb(win); + return cb(win); } - } catch (e) {} + } catch (e) { + console.error(e); + } }, 1000); return win; } @@ -260,7 +259,7 @@ }, follow: function (type, ids) { - var arr = angular.isArray(ids) ? ids : ids.split(','); + var arr = stringToArray(ids); return this.api('/me/following', { method: 'PUT', params: { @@ -279,7 +278,7 @@ }, unfollow: function (type, ids) { - var arr = angular.isArray(ids) ? ids : ids.split(','); + var arr = stringToArray(ids); return this.api('/me/following', { method: 'DELETE', params: { @@ -298,7 +297,7 @@ }, isFollowing: function (type, ids) { - var arr = angular.isArray(ids) ? ids : ids.split(','); + var arr = stringToArray(ids); return this.api('/me/following/contains', { params: { type: type, @@ -470,7 +469,7 @@ }, addTracksToPlaylist: function (userId, playlistId, tracks, options) { - var arr = angular.isArray(tracks) ? tracks : tracks.split(','); + var arr = stringToArray(tracks); var trks = arr.map(function (value) { return value.indexOf('spotify:') === -1 ? 'spotify:track:' + value : value; }); @@ -489,7 +488,7 @@ removeTracksFromPlaylist: function (userId, playlistId, tracks, options) { var data = options || {}; if (tracks) { - var arr = angular.isArray(tracks) ? tracks : tracks.split(','); + var arr = stringToArray(tracks); var trks = arr.map(function (track) { return { uri: track.indexOf('spotify:') === -1 ? 'spotify:track:' + track : track @@ -530,7 +529,7 @@ }, replaceTracksInPlaylist: function (userId, playlistId, tracks) { - var arr = tracks = angular.isArray(tracks) ? tracks : tracks.split(','); + var arr = tracks = stringToArray(tracks); var trks = arr.map(function (track) { return track.indexOf('spotify:') === -1 ? 'spotify:track:' + track : track; }); From 45efbf6cfc6c6048c2173fd342eb7843c295f7cc Mon Sep 17 00:00:00 2001 From: Ed Moore Date: Thu, 9 Jun 2016 10:03:27 +0800 Subject: [PATCH 13/13] Fixing jshint errors --- src/angular-spotify.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/angular-spotify.js b/src/angular-spotify.js index 8e2b02d..39a8b73 100644 --- a/src/angular-spotify.js +++ b/src/angular-spotify.js @@ -26,7 +26,7 @@ }; this.setClientId = function (clientId) { - return settings.clientId = clientId; + return settings.clientId = clientId; // jshint ignore:line }; this.getClientId = function () { @@ -34,11 +34,11 @@ }; this.setAccessToken = function (accessToken) { - return settings.accessToken = accessToken; + return settings.accessToken = accessToken; // jshint ignore:line }; this.setRedirectUri = function (redirectUri) { - return settings.redirectUri = redirectUri; + return settings.redirectUri = redirectUri; // jshint ignore:line }; this.getRedirectUri = function () { @@ -46,12 +46,12 @@ }; this.setScope = function (scope) { - return settings.scope = scope; + return settings.scope = scope; // jshint ignore:line }; var utils = {}; utils.toQueryString = function (obj) { - var parts = [] + var parts = []; angular.forEach(obj, function (value, key) { this.push(encodeURIComponent(key) + '=' + encodeURIComponent(value)); }, parts);