Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Moved Album.prototype.type getter const-to-string conversion into the…

… JS code (int value exposed as member _type). Session.prototype.connectionState now returns a string rep (the int value is exposed as member _connectionState). Also added search example.
  • Loading branch information...
commit 203e1658527ed4c88fc3fe344a9b56647bd650ff 1 parent 64d7ab0
Rasmus rsms authored
13 examples/searching.js
View
@@ -0,0 +1,13 @@
+var sys = require('sys'),
+ spotify = require('../spotify'),
+ account = require('../account');
+
+var session = new spotify.Session({ applicationKey: account.applicationKey });
+session.addListener('logMessage', sys.print);
+session.login(account.username, account.password, function (err) {
+ if (err) return sys.error(err.stack || err);
+ session.search('album:belle', function(err, result){
+ sys.puts(sys.inspect(result));
+ session.logout();
+ })
+});
54 spotify/index.js
View
@@ -1,2 +1,56 @@
var binding = require('./binding');
Object.keys(binding).forEach(function(k){ exports[k] = binding[k]; });
+
+// helper to define immutable <constants> on <obj> -- see below for examples
+function defineConstants(obj, constants) {
+ var m = {};
+ for (var name in constants) {
+ m[name] = {
+ value: constants[name],
+ writable: false,
+ enumerable: true,
+ configurable: false
+ };
+ }
+ Object.defineProperties(obj, m);
+}
+
+// connection state constants
+defineConstants(binding.Session, {
+ STATE_LOGGED_OUT: 0, // User not yet logged in
+ STATE_LOGGED_IN: 1, // Logged in against a Spotify access point
+ STATE_DISCONNECTED: 2, // Was logged in, has been disconnected
+ STATE_UNDEFINED: 3, // The connection state is undefined
+});
+
+// connection state getter
+Object.defineProperty(binding.Session.prototype, "connectionState", {
+ enumerable: true,
+ get: function(){
+ switch (this._connectionState) {
+ case binding.Album.STATE_LOGGED_OUT: return "logged_out";
+ case binding.Album.STATE_LOGGED_IN: return "logged_in";
+ case binding.Album.STATE_DISCONNECTED: return "disconnected";
+ }
+ },
+});
+
+// album type constants
+defineConstants(binding.Album, {
+ TYPE_ALBUM: 0, // Normal album
+ TYPE_SINGLE: 1, // Single
+ TYPE_COMPILATION: 2, // Compilation
+});
+
+// album type getter
+Object.defineProperty(binding.Album.prototype, "type", {
+ enumerable: true,
+ get: function(){
+ switch (this._type) {
+ case binding.Album.TYPE_ALBUM: return "album";
+ case binding.Album.TYPE_SINGLE: return "single";
+ case binding.Album.TYPE_COMPILATION: return "compilation";
+ }
+ return "unknown";
+ },
+});
20 src/album.cc
View
@@ -55,22 +55,10 @@ void Album::SetupBackingAlbum() {
handle_->Set(String::New("name"), String::New(sp_album_name(album_)));
handle_->Set(String::New("year"), Integer::New(sp_album_year(album_)));
- // todo: symbolize type constants
- Handle<String> type;
- switch (sp_album_type(album_)) {
- case SP_ALBUMTYPE_ALBUM:
- handle_->Set(String::New("type"), String::NewSymbol("album"));
- break;
- case SP_ALBUMTYPE_SINGLE:
- handle_->Set(String::New("type"), String::NewSymbol("single"));
- break;
- case SP_ALBUMTYPE_COMPILATION:
- handle_->Set(String::New("type"), String::NewSymbol("compilation"));
- break;
- }
- // intentional: do not set "type" property for unknown types.
-
- // todo: lazy getter for artist
+ // Type is only available when browsing albums, so e.g. will be
+ // SP_ALBUMTYPE_UNKNOWN for search results. There's a "type" getter defined
+ // in index.js which returns a string rep, based on this value.
+ handle_->Set(String::New("_type"), Integer::New(sp_album_type(album_)));
}
Handle<Value> Album::LoadedGetter(Local<String> property, const AccessorInfo& info) {
2  src/session.cc
View
@@ -492,7 +492,7 @@ void Session::Initialize(Handle<Object> target) {
Local<ObjectTemplate> instance_t = t->InstanceTemplate();
instance_t->SetInternalFieldCount(1);
instance_t->SetAccessor(String::New("user"), UserGetter);
- instance_t->SetAccessor(String::New("connectionState"),
+ instance_t->SetAccessor(String::New("_connectionState"),
ConnectionStateGetter);
instance_t->SetAccessor(String::New("playlists"), PlaylistContainerGetter);
Please sign in to comment.
Something went wrong with that request. Please try again.