Commits

Anonymous committed fbde4ae

url-quote buckets and keys in jiak.js

  • Participants
  • Parent commits 5dfaf37

Comments (0)

Files changed (2)

File client_lib/jiak.js

     else
         req.type = 'PUT';
     
-    req.url = this.baseurl+Object.bucket+'/';
+    req.url = this.path(Object.bucket);
     if (Object.key) req.url += Object.key;
     
     if (!(this.opts.noReturnBody || NoReturnBody))
 
 JiakClient.prototype.fetch = function(Bucket, Key, Callback) {
     return $.ajax({
-        url:      this.baseurl+Bucket+'/'+Key,
+        url:      this.path(Bucket, Key),
         dataType: "json",
         success:  Callback
     });
 JiakClient.prototype.remove = function(Bucket, Key, Callback) {
     return $.ajax({
         type:    'DELETE',
-        url:     this.baseurl+Bucket+'/'+Key,
+        url:     this.path(Bucket, Key),
         success: Callback
     });
 }
     // Start can be either and object with {bucket:B, key:K}
     // or a list with [Bucket, Key, ...]
     if ('bucket' in Start)
-        req.url = this.baseurl+Start.bucket+'/'+Start.key+'/';
+        req.url = this.path(Start.bucket, Start.key)+'/';
     else
-        req.url = this.baseurl+Start[0]+'/'+Start[1]+'/';
+        req.url = this.path(Start[0], Start[1])+'/';
 
     // Spec should be a list of objects with
     //    {bucket:B, tag:T, acc:A}
     //   false to have them excluded from the response (always true
     //   for the last step
     for (i in Spec) {
-        req.url += (Spec[i].bucket||'_')+','+
-            (Spec[i].tag||'_')+','+
+        req.url += encodeURIComponent(Spec[i].bucket||'_')+','+
+            encodeURIComponent(Spec[i].tag||'_')+','+
             ((Spec[i].acc || i == Spec.length-1) ? '1' : '_')+'/';
     }
 
 
     $.ajax({
         type:        'PUT',
-        url:         this.baseurl+Bucket,
+        url:         this.path(Bucket),
         contentType: 'application/json',
         data:        JSON.stringify({schema:Schema}),
         success:     Callback ? function() { Callback(true); } : undefined,
         error:       Callback ? function() { Callback(false); } : undefined
     });
+}
+
+JiakClient.prototype.path = function(Bucket, Key) {
+    var p = this.baseurl;
+    if (Bucket) {
+        p += encodeURIComponent(Bucket)+'/';
+        if (Key)
+            p += encodeURIComponent(Key);
+    }
+    return p;
 }

File demo/stickynotes/priv/www/js/jiak.js

 //      Client.store(note);
 //   });
 //
-//   Client.store({'bucket':'note'
+//   Client.store({'bucket':'note',
 //                 'object':{'text':'a new note'},
 //                 'links':[]},
 //                function(note) {
-//                  alert('new note's key: '+note.key);
+//                  alert("new note's key: "+note.key);
 //                });
 //
 //   Client.walk(['note', '456'],
 //               [{'bucket':'person', 'tag':'author'}],
 //               function(data) {
 //                 var authors = data.results[0];
-//                 alert('note's author is: '+
+//                 alert("note's author is: "+
 //                       authors[0].object.name);
 //               });
+
+// This file is provided to you under the Apache License,
+// Version 2.0 (the "License"); you may not use this file
+// except in compliance with the License.  You may obtain
+// a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.    
+
+
 function JiakClient(BaseUrl, Opts) {
     this.baseurl = BaseUrl;
     if (!(this.baseurl.slice(-1) == '/'))
     else
         req.type = 'PUT';
     
-    req.url = this.baseurl+Object.bucket+'/';
+    req.url = this.path(Object.bucket);
     if (Object.key) req.url += Object.key;
     
     if (!(this.opts.noReturnBody || NoReturnBody))
 
 JiakClient.prototype.fetch = function(Bucket, Key, Callback) {
     return $.ajax({
-        url:      this.baseurl+Bucket+'/'+Key,
+        url:      this.path(Bucket, Key),
         dataType: "json",
         success:  Callback
     });
 JiakClient.prototype.remove = function(Bucket, Key, Callback) {
     return $.ajax({
         type:    'DELETE',
-        url:     this.baseurl+Bucket+'/'+Key,
+        url:     this.path(Bucket, Key),
         success: Callback
     });
 }
     // Start can be either and object with {bucket:B, key:K}
     // or a list with [Bucket, Key, ...]
     if ('bucket' in Start)
-        req.url = this.baseurl+Start.bucket+'/'+Start.key+'/';
+        req.url = this.path(Start.bucket, Start.key)+'/';
     else
-        req.url = this.baseurl+Start[0]+'/'+Start[1]+'/';
+        req.url = this.path(Start[0], Start[1])+'/';
 
     // Spec should be a list of objects with
     //    {bucket:B, tag:T, acc:A}
     //   false to have them excluded from the response (always true
     //   for the last step
     for (i in Spec) {
-        req.url += (Spec[i].bucket||'_')+','+
-            (Spec[i].tag||'_')+','+
+        req.url += encodeURIComponent(Spec[i].bucket||'_')+','+
+            encodeURIComponent(Spec[i].tag||'_')+','+
             ((Spec[i].acc || i == Spec.length-1) ? '1' : '_')+'/';
     }
 
 
     $.ajax({
         type:        'PUT',
-        url:         this.baseurl+Bucket,
+        url:         this.path(Bucket),
         contentType: 'application/json',
         data:        JSON.stringify({schema:Schema}),
         success:     Callback ? function() { Callback(true); } : undefined,
         error:       Callback ? function() { Callback(false); } : undefined
     });
+}
+
+JiakClient.prototype.path = function(Bucket, Key) {
+    var p = this.baseurl;
+    if (Bucket) {
+        p += encodeURIComponent(Bucket)+'/';
+        if (Key)
+            p += encodeURIComponent(Key);
+    }
+    return p;
 }