Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add tests and implementation for track starred, availability and popu…

…larity
  • Loading branch information...
commit b32461a35cfbc088eec6898152037b2822edc843 1 parent 51a4f74
@Floby authored
View
5 lib/SpObject.js
@@ -1,4 +1,5 @@
var b = require('bindings')('spotify.node');
+var sp = require('./libspotify');
var util = require('util');
var EventEmitter = require('events').EventEmitter;
var backoff = require('backoff');
@@ -45,5 +46,9 @@ SpObject.prototype._loadingTimeout = function _loadingTimeout() {
this.emit('load-timeout');
};
+SpObject.prototype.getSession = function getSession() {
+ return sp.Session.currentSession;
+};
+
module.exports = SpObject;
View
25 lib/Track.js
@@ -25,7 +25,32 @@ Track.prototype._populateAttributes = function _populateAttributes() {
this.artists.push(b.track_artist(this._sp_object, i));
}
this.artist = new sp.Artist(b.track_artist(this._sp_object, 0));
+
+ this.availability = b.track_get_availability(this.getSession()._sp_session, this._sp_object);
+ this.popularity = b.track_popularity(this._sp_object);
+};
+
+Track.prototype.isAvailable = function isAvailable() {
+ return this.availability === 'AVAILABLE';
};
+Track.prototype.isStarred = function isStarred() {
+ return b.track_is_starred(this.getSession()._sp_session, this._sp_object);
+};
+
+Track.prototype.star = function star() {
+ return b.track_set_starred(
+ this.getSession()._sp_session,
+ [this._sp_object],
+ true
+ );
+};
+Track.prototype.unstar = function unstar() {
+ return b.track_set_starred(
+ this.getSession()._sp_session,
+ [this._sp_object],
+ false
+ );
+};
module.exports = Track;
View
100 src/track.cc
@@ -119,10 +119,110 @@ static Handle<Value> Track_Artist(const Arguments& args) {
return scope.Close(artist->object);
}
+/**
+ * JS track_get_availability implementation. checks if a given track is loaded
+ */
+static Handle<Value> Track_Get_Availability(const Arguments& args) {
+ HandleScope scope;
+
+ // test arguments sanity
+ assert(args.Length() == 2);
+ assert(args[0]->IsObject());
+ assert(args[1]->IsObject());
+
+ // gets sp_track pointer from given object
+ ObjectHandle<sp_session>* session = ObjectHandle<sp_session>::Unwrap(args[0]);
+ ObjectHandle<sp_track>* track = ObjectHandle<sp_track>::Unwrap(args[1]);
+
+ sp_availability availability = sp_track_get_availability(session->pointer, track->pointer);
+ Handle<Value> res;
+ switch (availability) {
+ case SP_TRACK_AVAILABILITY_AVAILABLE:
+ res = String::New("AVAILABLE");
+ break;
+ case SP_TRACK_AVAILABILITY_NOT_STREAMABLE:
+ res = String::New("NOT_STREAMABLE");
+ break;
+ case SP_TRACK_AVAILABILITY_BANNED_BY_ARTIST:
+ res = String::New("BANNED_BY_ARTIST");
+ break;
+ case SP_TRACK_AVAILABILITY_UNAVAILABLE:
+ default:
+ res = String::New("UNAVAILABLE");
+ break;
+ }
+
+ return scope.Close(res);
+}
+
+static Handle<Value> Track_Is_Starred(const Arguments& args) {
+ HandleScope scope;
+
+ // test arguments sanity
+ assert(args.Length() == 2);
+ assert(args[0]->IsObject());
+ assert(args[1]->IsObject());
+
+ // get session and track pointers from object
+ ObjectHandle<sp_session>* session = ObjectHandle<sp_session>::Unwrap(args[0]);
+ ObjectHandle<sp_track>* track = ObjectHandle<sp_track>::Unwrap(args[1]);
+
+ bool starred = sp_track_is_starred(session->pointer, track->pointer);
+
+ return scope.Close(Boolean::New(starred));
+}
+
+static Handle<Value> Track_Set_Starred(const Arguments& args) {
+ HandleScope scope;
+
+ // test arguments sanity
+ assert(args.Length() == 3);
+ assert(args[0]->IsObject());
+ assert(args[1]->IsArray());
+ assert(args[2]->IsBoolean());
+
+ ObjectHandle<sp_session>* session = ObjectHandle<sp_session>::Unwrap(args[0]);
+
+ Handle<Array> arr = Local<Array>(Array::Cast(*args[1]));
+ unsigned int length = arr->Length();
+ sp_track* tracks[length];
+ for(unsigned int i=0; i<length; ++i) {
+ tracks[i] = ObjectHandle<sp_track>::Unwrap(arr->Get(i))->pointer;
+ }
+
+ sp_error error = sp_track_set_starred(session->pointer, tracks, length, args[2]->ToBoolean()->BooleanValue());
+ NSP_THROW_IF_ERROR(error);
+
+ return scope.Close(Undefined());
+}
+
+/**
+ * JS track_popularity implementation. checks if a given track is loaded
+ */
+static Handle<Value> Track_Popularity(const Arguments& args) {
+ HandleScope scope;
+
+ // test arguments sanity
+ assert(args.Length() == 1);
+ assert(args[0]->IsObject());
+
+ // gets sp_track pointer from given object
+ ObjectHandle<sp_track>* track = ObjectHandle<sp_track>::Unwrap(args[0]);
+
+ // actually call sp_track_popularity
+ int popularity = sp_track_popularity(track->pointer);
+
+ return scope.Close(Number::New(popularity));
+}
+
void nsp::init_track(Handle<Object> target) {
NODE_SET_METHOD(target, "track_is_loaded", Track_Is_Loaded);
NODE_SET_METHOD(target, "track_duration", Track_Duration);
NODE_SET_METHOD(target, "track_num_artists", Track_Num_Artists);
NODE_SET_METHOD(target, "track_name", Track_Name);
NODE_SET_METHOD(target, "track_artist", Track_Artist);
+ NODE_SET_METHOD(target, "track_get_availability", Track_Get_Availability);
+ NODE_SET_METHOD(target, "track_is_starred", Track_Is_Starred);
+ NODE_SET_METHOD(target, "track_popularity", Track_Popularity);
+ NODE_SET_METHOD(target, "track_set_starred", Track_Set_Starred);
}
View
15 test/test-030-track.js
@@ -22,6 +22,21 @@ exports.track = {
test.equal('Guillemots', first.artist, "the track should be a guillemots song");
test.equal('Fleet', first.title, "the track should be a guillemots song");
test.ok(first.duration > 0, 'The track should have a non zero duration');
+ test.ok(first.isAvailable(), 'The track should be available');
+ test.equal('AVAILABLE', first.availability);
+ test.equal(typeof first.isStarred(), 'boolean', 'isStarred should return a boolean');
+ test.equal(typeof first.popularity, 'number', 'Popularity should be a number');
+ test.ok(first.popularity >= 0, 'Popularity should be greater than zero');
+ test.ok(first.popularity <= 100, 'Popularity should be less than 100');
+
+ test.doesNotThrow(function() {
+ var starred = first.isStarred();
+ first.star();
+ test.ok(first.isStarred(), 'Track should be starred');
+ first.unstar();
+ test.ok(!first.isStarred(), 'Track should not be starred');
+ if(starred) first.star();
+ }, 'starring and unstarring a track should not throw');
return test.done();
});
}
Please sign in to comment.
Something went wrong with that request. Please try again.