(hoofbeats) Add support for pluggable resolvers.
authorSalvatore Iovene <salvatore@iovene.com>
Mon, 18 Mar 2013 07:37:36 +0000 (09:37 +0200)
committerSalvatore Iovene <salvatore@iovene.com>
Mon, 18 Mar 2013 07:37:36 +0000 (09:37 +0200)
A resolver is something used to get song data online, e.g. MusicBrainz.

examples/hoofbeats/index.html
examples/hoofbeats/javascripts/library.js
examples/hoofbeats/javascripts/resolvers/dummy.js [new file with mode: 0644]
examples/hoofbeats/javascripts/resolvers/musicbrainz.js [moved from examples/hoofbeats/javascripts/musicbrainz.js with 98% similarity]
examples/hoofbeats/tests/index.html
examples/hoofbeats/tests/unit/library.js
examples/hoofbeats/tests/unit/resolvers/musicbrainz.js [moved from examples/hoofbeats/tests/unit/musicbrainz.js with 100% similarity]
grunt.js

index fcf1197..e9cf520 100644 (file)
@@ -62,7 +62,7 @@
     <script src="lib/cowhide.js"></script>
 
     <!-- our own js files -->
-    <script src="javascripts/musicbrainz.js"></script>
+    <script src="javascripts/resolvers/musicbrainz.js"></script>
     <script src="javascripts/library.js"></script>
 
     <!-- the application framework -->
index e8cec59..ada40ba 100644 (file)
@@ -6,12 +6,11 @@
         var _priv = {
             initialized: false,
             deferred: undefined,
-            // Whether items should be resolved on MusicBrainz
-            resolve: false,
             fetchCount: 100,
             fetchOffset: 0,
             oneTime: false,
             mediaItems: [],
+            resolveBackend: MusicBrainz,
 
             errorCB: function(error) {
                 console.log("HoofbeatsLibrary.errorCB: " + error.name);
             findCB: function(items) {
                 items.forEach(function(item, index, items) {
                     _priv.mediaItems.push(item);
-                    if (_priv.resolve) {
-                        win.MusicBrainz.getArtist(item.artists[0]).done(
+                    if (_priv.resolveBackend !== undefined) {
+                        _priv.resolveBackend.getArtist(item.artists[0]).done(
                             function(data) {
                                 console.log(
                                     "HoofbeatsLibrary.findCB: " +
-                                    "item resolved on MusicBrainz: " +
-                                    data.name);
+                                    "item resolved on " + _priv.resolveBackend.name() +
+                                    ": " + data.name);
                             }
                         );
                     }
@@ -53,7 +52,6 @@
 
 
         // Public stuff
-
         return {
             initialize: function() {
                 if (_priv.initialized)
@@ -73,8 +71,8 @@
 
             scan: function(options) {
                 var opts = options || {};
-                if (opts.resolve !== undefined) {
-                    _priv.resolve = opts.resolve;
+                if (opts.resolveBackend !== MusicBrainz) {
+                    _priv.resolveBackend = opts.resolveBackend;
                 }
 
                 _priv.deferred = new $.Deferred();
             getInitialized: function() { return _priv.initialized; },
             getItems: function() { return _priv.mediaItems; },
             getSize: function() { return _priv.mediaItems.length; },
-            getResolve: function() {return _priv.resolve; }
+            getResolve: function() {return _priv.resolveBackend !== undefined; }
         };
     };
 
diff --git a/examples/hoofbeats/javascripts/resolvers/dummy.js b/examples/hoofbeats/javascripts/resolvers/dummy.js
new file mode 100644 (file)
index 0000000..2893a9a
--- /dev/null
@@ -0,0 +1,16 @@
+/* vi: set et sw=4 ts=4 si: */
+(function(win, $) {
+    var dummyResolver = function() {
+        return {
+            name: function() { return "DummyResolver"; },
+
+            getArtist: function(q) {
+                return new $.Deferred().resolve({
+                    name: 'Regina Spektor'
+                });
+            }
+        };
+    };
+
+    win.DummyResolver = new dummyResolver();
+}(window, jQuery));
@@ -63,6 +63,8 @@
 
         /* Public methods */
         return {
+            name: function() { return "MusicBrainz"; },
+
             getArtist: function(q) {
                 var lookup_url = _priv._baseUrl + 'artist/?query="' + q +
                                  '"&' + _priv._fmtArg,
index 7aaec1f..fab892f 100644 (file)
   <script src="unit/hoofbeats-phantom.js"></script>
 
   <!-- source -->
+  <script src="javascripts/resolvers/dummy.js"></script>
+  <script src="javascripts/resolvers/musicbrainz.js"></script>
   <script src="javascripts/library.js"></script>
-  <script src="javascripts/musicbrainz.js"></script>
 
   <!-- unit tests -->
+  <script src="unit/resolvers/musicbrainz.js"></script>
   <script src="unit/library.js"></script>
-  <script src="unit/musicbrainz.js"></script>
 </head>
 <body>
   <div>
index e746e96..1a7a7da 100644 (file)
@@ -14,14 +14,14 @@ $(function() {
         var lib = new HoofbeatsLibrary();
         window.tizen = undefined;
         raises(function() {
-            lib.scan();
+            lib.scan({resolveBackend: DummyResolver});
         }, Error, "scan throws Error");
     });
 
     test("successful scan", function() {
         var lib = new HoofbeatsLibrary();
         stop();
-        lib.scan().then(function() {
+        lib.scan({resolveBackend: DummyResolver}).then(function() {
             ok(lib.getSize() > 0, "there are items in the library");
             start();
         });
@@ -30,8 +30,8 @@ $(function() {
     test("successful scan without resolving", function() {
         var lib = new HoofbeatsLibrary();
         stop();
-        lib.scan({resolve: false}).then(function() {
-            ok(lib.getResolve() == false, "lib.resolve is false");
+        lib.scan({resolveBackend: undefined}).then(function() {
+            ok(lib.getResolve() == false, "lib.getResolve() is false");
             start();
         });
     });
@@ -39,7 +39,7 @@ $(function() {
     test("scan with count", function() {
         var lib = new HoofbeatsLibrary();
         stop();
-        lib.scan({count: 1}).then(function() {
+        lib.scan({count: 1, resolveBackend: DummyResolver}).then(function() {
             ok(lib.getSize() == 1, "there is one item in the library");
             start();
         });
@@ -50,7 +50,7 @@ $(function() {
             item_id = 'e7e7023b-54b3-41d5-b4a1-aa24498e0572';
 
         stop();
-        lib.scan({count: 1}).then(function() {
+        lib.scan({count: 1, resolveBackend: DummyResolver}).then(function() {
             var item = lib.item(item_id);
             ok(item !== undefined, "item was found");
             ok(item.id == item_id, "correct item was found");
index 257f4fe..fd30df5 100644 (file)
--- a/grunt.js
+++ b/grunt.js
@@ -38,6 +38,7 @@ module.exports = function(grunt) {
         'examples/widget-gallery/javascripts/*.js',
 
         'examples/hoofbeats/javascripts/*.js',
+        'examples/hoofbeats/javascripts/resolvers/*.js',
 
         'test/**/*.js'
       ]