pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

helma private pastebin - collaborative debugging tool What's a private pastebin?


Posted by Joshua Paine on Sun 9 Dec 06:19
report abuse | download | new post

  1. // Copyright (C) 2007 Damien Katz <damien_katz@yahoo.com>.
  2.  
  3. // This file is licenced under a Creative Commons Attribution 3.0 Unported
  4. // Licence <http://creativecommons.org/licenses/by/3.0/>.
  5.  
  6. // A simple class to represent a database. Uses XMLHttpRequest
  7. // to interface with the CouchDB server.
  8.  
  9. // Modified by Joshua Paine to use Helma's helma.Http object with a few
  10. // added compatibility features in place of XMLHttpRequest.
  11. // And to specify a hostname and port.
  12. // And changed obj.toJSONString() -> obj.toJSON().
  13.  
  14. app.addRepository('modules/helma/Http.js');
  15.  
  16. function CouchDB(name,host,port) {
  17.   this.host = host ? host : 'localhost';
  18.   this.port = port ? port : 5984;
  19.   this.name = name
  20.   this.url = "http://"+this.host+":"+this.port+"/" + encodeURIComponent(name) +"/";
  21.   xhr = CouchDB.new_XMLHttpRequest();
  22.  
  23.   // Creates the database on the server
  24.   this.createDb = function() {
  25.     xhr.open("PUT", this.url, false);
  26.     xhr.send(null);
  27.     if (xhr.status != 201)
  28.       throw xhr.responseText.parseJSON();
  29.     return xhr.responseText.parseJSON();
  30.   }
  31.  
  32.   // Deletes the database on the server
  33.   this.deleteDb = function() {
  34.     xhr.open("DELETE", this.url, false);
  35.     xhr.send(null);
  36.     if (xhr.status == 404)
  37.       return false;
  38.     if (xhr.status != 202)
  39.       throw xhr.responseText.parseJSON();
  40.     return xhr.responseText.parseJSON();
  41.   }
  42.  
  43.   // Save a document to the database
  44.   this.save = function(doc, options) {
  45.     if (doc._id == undefined)
  46.       xhr.open("POST", this.url + encodeOptions(options), false);
  47.     else
  48.       xhr.open("PUT", this.url  + encodeURIComponent(doc._id)
  49.         + encodeOptions(options), false);
  50.     xhr.send(doc.toJSON());
  51.     if (xhr.status != 201)
  52.       throw xhr.responseText.parseJSON();
  53.     var result = xhr.responseText.parseJSON();
  54.     // set the _id and _rev members on the input object, for caller convenience.
  55.     doc._id = result.id;
  56.     doc._rev = result.rev;
  57.     return result;
  58.   }
  59.  
  60.   // Open a document from the database
  61.   this.open = function(docId, options) {
  62.     xhr.open("GET", this.url + encodeURIComponent(docId)
  63.         + encodeOptions(options), false);
  64.     xhr.send(null);
  65.     if (xhr.status == 404)
  66.       return null;
  67.     if (xhr.status != 200)
  68.       throw xhr.responseText.parseJSON();
  69.     return xhr.responseText.parseJSON();
  70.   }
  71.  
  72.   // Deletes a document from the database
  73.   this.deleteDoc = function(doc) {
  74.     xhr.open("DELETE", this.url + encodeURIComponent(doc._id)
  75.         + "?rev=" + doc._rev, false);
  76.     xhr.send(null);
  77.     if (xhr.status != 202)
  78.       throw xhr.responseText.parseJSON();
  79.     var result = xhr.responseText.parseJSON();
  80.     doc._rev = result.rev; //record rev in input document
  81.     doc._deleted = true;
  82.     return result;
  83.   }
  84.  
  85.   this.bulkSave = function(docs, options) {
  86.     xhr.open("POST", this.url + "_bulk_docs" + encodeOptions(options), false);
  87.     xhr.send(docs.toJSON());
  88.     if (xhr.status != 201)
  89.       throw xhr.responseText.parseJSON();
  90.     return xhr.responseText.parseJSON();
  91.   }
  92.  
  93.   // Applies the map function to the contents of database and returns the results.
  94.   this.query = function(mapFun, options) {
  95.     xhr.open("POST", this.url + "_temp_view" + encodeOptions(options), false);
  96.     // specify the query language we are using
  97.     xhr.setRequestHeader("content-type", "text/javascript");
  98.     if (typeof(mapFun) != "string")
  99.       mapFun = mapFun.toSource ? mapFun.toSource() : "(" + mapFun.toString() + ")";
  100.     xhr.send(mapFun);
  101.     if (xhr.status != 200)
  102.       throw xhr.responseText.parseJSON();
  103.     return xhr.responseText.parseJSON();
  104.   }
  105.  
  106.   this.view = function(viewname, options) {
  107.     xhr.open("GET", this.url + "_view/" + viewname
  108.         + encodeOptions(options), false);
  109.     xhr.send(null);
  110.     if (xhr.status == 404)
  111.       return null;
  112.     if (xhr.status != 200)
  113.       throw xhr.responseText.parseJSON();
  114.     return xhr.responseText.parseJSON();
  115.   }
  116.  
  117.   // gets information about the database
  118.   this.info = function() {
  119.     xhr.open("GET", this.url, false);
  120.     xhr.send(null);
  121.     if (xhr.status != 200)
  122.       throw xhr.responseText.parseJSON();
  123.     return xhr.responseText.parseJSON();
  124.   }
  125.  
  126.   this.allDocs = function(options) {
  127.     xhr.open("GET", this.url + "_all_docs" + encodeOptions(options), false);
  128.     xhr.send(null);
  129.     if (xhr.status != 200)
  130.       throw xhr.responseText.parseJSON();
  131.     return xhr.responseText.parseJSON();
  132.   }
  133.  
  134.   // Convert a options object to an url query string.
  135.   // ex: {key:'value',key2:'value2'} becomes '?key="value"&key2="value2"'
  136.   function encodeOptions(options) {
  137.     var buf = []
  138.     if (typeof(options) == "object" && options !== null) {
  139.       for (var name in options) {
  140.         if (!options.hasOwnProperty(name)) continue;
  141.         var value = options[name];
  142.         if (name == "key" || name == "startkey" || name == "endkey") {
  143.           value = toJSON(value);
  144.         }
  145.         buf.push(encodeURIComponent(name) + "=" + encodeURIComponent(value));
  146.       }
  147.     }
  148.     if (!buf.length) {
  149.       return "";
  150.     }
  151.     return "?" + buf.join("&");
  152.   }
  153.  
  154.   function toJSON(obj) {
  155.     if (obj === null) {
  156.       return "null";
  157.     }
  158.     return obj.toJSON()
  159.   }
  160. }
  161.  
  162. CouchDB.new_XMLHttpRequest = function() {
  163.   var h = new helma.Http();
  164.   return h;
  165. }
  166.  
  167. helma.Http.prototype.open = function(method,url) {
  168.         this.xhrUrl = url;
  169.         this.setMethod(method);
  170. }
  171.  
  172. helma.Http.prototype.send = function(content) {
  173.         this.setContent(content);
  174.         var response = this.getUrl(this.xhrUrl);
  175.         this.status = response.code;
  176.         this.responseText = response.content ? response.content : '';
  177. }
  178.  
  179. helma.Http.prototype.setRequestHeader = function(name, value) {
  180.         return this.setHeader(name,value);
  181. }

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me so that I can delete my post