Skip to content
Browse files

pass tracks from searches are loaded track objects

  • Loading branch information...
1 parent aee2411 commit 4f2d34f9f42b5e8ad2f083d2cf3b7ae01616c659 @Floby committed
Showing with 56 additions and 3 deletions.
  1. +1 −0 binding.gyp
  2. +6 −0 lib/Search.js
  3. +16 −1 lib/Track.js
  4. +1 −0 src/binding.cc
  5. +5 −0 src/common.h
  6. +27 −0 src/search.cc
  7. +0 −2 test/test-020-search-02-process.js
View
1 binding.gyp
@@ -6,6 +6,7 @@
"src/binding.cc",
"src/session.cc",
"src/search.cc",
+ "src/track.cc",
],
"cflags": ["-Wall", "-g", "-O0"],
"conditions" : [
View
6 lib/Search.js
@@ -1,5 +1,6 @@
var b = require('bindings')('spotify.node');
var Session = require('./Session');
+var Track = require('./Track');
var util = require('util');
var EventEmitter = require('events').EventEmitter;
var format = require('format').format;
@@ -65,6 +66,11 @@ Search.prototype.execute = function execute(cb) {
Search.prototype._processResults = function _processResults(search) {
this.tracks = new Array(b.search_num_tracks(this._sp_search));
+
+ for (var i = 0; i < this.tracks.length; ++i) {
+ this.tracks[i] = new Track(b.search_track(this._sp_search, i));
+ }
+
this.artists = [];
this.albums = [];
this.playlists = [];
View
17 lib/Track.js
@@ -1 +1,16 @@
-/// DO TOMORROW
+var b = require('bindings')('spotify.node');
+var Session = require('./Session');
+var util = require('util');
+var EventEmitter = require('events').EventEmitter;
+
+function Track (sp_track) {
+ this._sp_track = sp_track;
+}
+util.inherits(Track, EventEmitter);
+
+Track.prototype.isReady = function isReady() {
+ return b.track_is_loaded(this._sp_track);
+};
+
+
+module.exports = Track;
View
1 src/binding.cc
@@ -28,6 +28,7 @@ extern "C" {
// initializing all modules
nsp::init_session(target);
nsp::init_search(target);
+ nsp::init_track(target);
}
}
View
5 src/common.h
@@ -127,6 +127,11 @@ namespace nsp {
* init the search related functions to the target module exports
*/
void init_search(v8::Handle<v8::Object> target);
+ /**
+ * init the track related functions to the target module exports
+ */
+ void init_track(v8::Handle<v8::Object> target);
+
/**
* This utility class allows to keep track of a C pointer that we attached
View
27 src/search.cc
@@ -106,7 +106,34 @@ static Handle<Value> Search_Num_Tracks(const Arguments& args) {
return scope.Close(Number::New(num));
}
+/**
+ * JS search_track implementation. gets a track a the given index in a search result
+ */
+static Handle<Value> Search_Track(const Arguments& args) {
+ HandleScope scope;
+
+ // test arguments sanity
+ assert(args.Length() == 2);
+ assert(args[0]->IsObject());
+ assert(args[1]->IsNumber());
+
+ // gets sp_search pointer from given object
+ ObjectHandle<sp_search>* search = ObjectHandle<sp_search>::Unwrap(args[0]);
+ int index = args[1]->ToNumber()->Int32Value();
+
+ // check index is within search results range
+ assert(index >= 0);
+ assert(index < sp_search_num_tracks(search->pointer));
+
+ // create new handle for this track
+ ObjectHandle<sp_track>* track = new ObjectHandle<sp_track>("sp_track");
+ track->pointer = sp_search_track(search->pointer, index);
+
+ return scope.Close(track->object);
+}
+
void nsp::init_search(Handle<Object> target) {
NODE_SET_METHOD(target, "search_create", Search_Create);
NODE_SET_METHOD(target, "search_num_tracks", Search_Num_Tracks);
+ NODE_SET_METHOD(target, "search_track", Search_Track);
}
View
2 test/test-020-search-02-process.js
@@ -14,9 +14,7 @@ exports.setUp = function(cb) {
exports.testGetTrackFromSearchResult = function(test) {
var search = new sp.Search('artist:"Guillemots" track:"Fleet"');
- console.log('executing search')
search.execute(function() {
- console.log('got search results');
test.doesNotThrow(function() {
test.ok(search.tracks.length > 0, "the search should return at least one result");
test.ok(search.tracks[0] instanceof sp.Track, "the track results should be loaded track objects");

0 comments on commit 4f2d34f

Please sign in to comment.
Something went wrong with that request. Please try again.