Add Intel copyright header.
[profile/ivi/cowhide.git] / examples / hoofbeats / javascripts / resolvers / musicbrainz.js
1 /* vi: set et sw=4 ts=4 si: */
2 /*
3  * Copyright (c) 2012, Intel Corporation.
4  *
5  * This program is licensed under the terms and conditions of the
6  * Apache License, version 2.0.  The full text of the Apache License is at
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  */
10
11 (function(win, $) {
12     var musicbrainz = function() {
13         var _priv = {
14             _baseUrl: 'http://musicbrainz.org/ws/2/',
15             _fmtArg: 'fmt=json',
16             _interval: undefined,
17             _queue: [],
18             _url_cache: {},
19
20
21             /* Internal methods to perform XHR requests. */
22
23             _get: function(request) {
24                 if (request.url in _priv._url_cache) {
25                     request.deferred.resolve(_priv._url_cache[request.url]);
26                 } else {
27                     $.getJSON(request.url)
28                     .done(function(data, statusText, xhr) {
29                         _priv._url_cache[request.url] = data;
30                         request.deferred.resolve(data);
31                     })
32                     .error(function(xhr) {
33                         if (xhr.status === 503) {
34                             _priv._enqueue(request); // put it back in the queue
35                         }
36                     });
37                 }
38
39                 return request.deferred.promise();
40             },
41
42
43             /* Internal methods for the handling of the request queue */
44
45             _startQueue: function() {
46                 if (_priv._interval === undefined) {
47                     _priv._interval = setInterval(function() {
48                         _priv._dequeue();
49                     }, 2000);
50                 }
51             },
52
53             _stopQueue: function() {
54                 clearInterval(_priv._interval);
55                 _priv._interval = undefined;
56             },
57
58             _enqueue: function(request) {
59                 if ($.inArray(request.url, _priv._queue) < 0) {
60                     _priv._queue.push(request);
61                 }
62             },
63
64             _dequeue: function() {
65                 var request = _priv._queue.shift();
66                 if (request !== undefined) {
67                     _priv._get(request);
68                 }
69             }
70         }; // _priv
71
72
73         /* Public methods */
74         return {
75             name: function() { return "MusicBrainz"; },
76
77             getArtist: function(q) {
78                 var lookup_url = _priv._baseUrl + 'artist/?query="' + q +
79                                  '"&' + _priv._fmtArg,
80                     lookup_deferred = new $.Deferred(),
81                     returned_deferred = new $.Deferred();
82
83                 if (q !== undefined) {
84                     if (_priv._interval === undefined) {
85                         _priv._startQueue();
86                     }
87
88                     _priv._enqueue({url: lookup_url, deferred: lookup_deferred});
89                     lookup_deferred.done(function(data) {
90                         if (data.count > 0) {
91                             var artist_url = _priv._baseUrl + 'artist/' +
92                                              data.artist[0].id + '?' + _priv._fmtArg,
93                                 artist_deferred = new $.Deferred();
94
95                             _priv._get({url: artist_url, deferred: artist_deferred});
96                             artist_deferred.done(function(data) {
97                                 returned_deferred.resolve(data);
98                             });
99                         } else {
100                             returned_deferred.reject();
101                         }
102                     });
103                 } else {
104                     returned_deferred.reject();
105                 }
106
107                 return returned_deferred.promise();
108             }
109         };
110     };
111
112     win.MusicBrainz = new musicbrainz();
113 }(window, jQuery));