<commit_before>/**
 * UCengine library
 * http://ucengine.org/
 * (c) 2010 af83
 */
(function(g) {
    var VERSION = "0.1";

    function getCollection(url, params, callback) {
        get(url, params, function(err, result, xhr) {
            if (!err) {
                callback(err, result.result, xhr);
            } else {
                callback(err, result, xhr);
            }
        });
    }

    function uce_api_call(method, url, data, callback)
    {
        var call_back = callback || $.noop;
        url = '/api/' + VERSION + url;
        return $.ajax({
	    type     : method,
            dataType : "json",
	    url      : url,
	    data     : data,
            complete : function(xhr, textStatus) {
                var response = jQuery.parseJSON(xhr.responseText);
                if (xhr.status >= 400) {
                    call_back(xhr.status, response, xhr);
                } else {
                    call_back(null, response, xhr);
                }
            }
        });
    }

    function get(url, data, callback) {
        return uce_api_call("get", url, data, callback);
    }

    function put(url, data, callback) {
        data = $.extend({"_method": "put"}, data);
        return uce_api_call("post", url, data, callback);
    }

    function del(url, data, callback) {
        data = $.extend({"_method": "delete"}, data);
        return uce_api_call("post", url, data, callback);
    }

    function Uce(presence) {
        return {
            getWaiter : function(calls_needed, callback) {
                if(calls_needed == 0)
                    callback();
                var ok = true;
                var waiter = function(){
                    --calls_needed;
                    if (calls_needed == 0 && ok)
                        callback();
                    // XXX: should we raise an error if waiter called too many times?
                };
                return waiter;
            },
            time: function(callback) {
                get("/time", {}, function(err, result, xhr) {
                    callback(err, result.result, xhr);
                });
                return this;
            },
            presence: {
                /**
                 * Create user presence
                 */
                create: function(auth, credential, org, uid, nickname, callback)
                {
                    var params = {"auth" : auth, "metadata[nickname]" : nickname};
                    if (credential) {
                        params.credential = credential;
                    }
                    put("/presence/" + org + "/" + uid, params, function(err, result, xhr) {
                        if (err) {
                            callback(err, result, xhr);
                        } else {
                            var presence = {"uid": uid, "sid": result.result};
                            callback(err, presence, xhr);
                        }
                    });
                    return this;
                },
                /**
                 * Close user presence
                 */
                close: function(org, callback) {
                    del("/presence/" + org + "/" + presence.uid +"/"+ presence.sid, presence, callback);
                    return this;
                }
            },
            /**
             * Attach presence to a new uce object
             */
            attachPresence : function(presence) {
                return new Uce(presence);
            },
            org : function(orgname) {<commit_msg>Update uce.js to use new presence API<commit_after>/**
 * UCengine library
 * http://ucengine.org/
 * (c) 2010 af83
 */
(function(g) {
    var VERSION = "0.1";

    function getCollection(url, params, callback) {
        get(url, params, function(err, result, xhr) {
            if (!err) {
                callback(err, result.result, xhr);
            } else {
                callback(err, result, xhr);
            }
        });
    }

    function uce_api_call(method, url, data, callback)
    {
        var call_back = callback || $.noop;
        url = '/api/' + VERSION + url;
        return $.ajax({
	    type     : method,
            dataType : "json",
	    url      : url,
	    data     : data,
            complete : function(xhr, textStatus) {
                var response = jQuery.parseJSON(xhr.responseText);
                if (xhr.status >= 400) {
                    call_back(xhr.status, response, xhr);
                } else {
                    call_back(null, response, xhr);
                }
            }
        });
    }

    function get(url, data, callback) {
        return uce_api_call("get", url, data, callback);
    }

    function put(url, data, callback) {
        data = $.extend({"_method": "put"}, data);
        return uce_api_call("post", url, data, callback);
    }

    function del(url, data, callback) {
        data = $.extend({"_method": "delete"}, data);
        return uce_api_call("post", url, data, callback);
    }

    function Uce(presence) {
        return {
            getWaiter : function(calls_needed, callback) {
                if(calls_needed == 0)
                    callback();
                var ok = true;
                var waiter = function(){
                    --calls_needed;
                    if (calls_needed == 0 && ok)
                        callback();
                    // XXX: should we raise an error if waiter called too many times?
                };
                return waiter;
            },
            time: function(callback) {
                get("/time", {}, function(err, result, xhr) {
                    callback(err, result.result, xhr);
                });
                return this;
            },
            presence: {
                /**
                 * Create user presence
                 */
                create: function(auth, credential, org, uid, nickname, callback)
                {
                    var params = {"auth" : auth,
				  "metadata" : {"nickname": nickname, "org": org}};
                    if (credential) {
                        params.credential = credential;
                    }
                    put("/presence/" + uid, params, function(err, result, xhr) {
                        if (err) {
                            callback(err, result, xhr);
                        } else {
                            var presence = {"uid": uid, "sid": result.result};
                            callback(err, presence, xhr);
                        }
                    });
                    return this;
                },
                /**
                 * Close user presence
                 */
                close: function(org, callback) {
                    del("/presence/" + presence.uid +"/"+ presence.sid, presence, callback);
                    return this;
                }
            },
            /**
             * Attach presence to a new uce object
             */
            attachPresence : function(presence) {
                return new Uce(presence);
            },
            org : function(orgname) {
{'fraction_non_alphanumeric': 0.11675977653631285, 'fraction_numerical': 0.0036312849162011174, 'mean_word_length': 1.1117664405780006, 'pattern_counts': {'":': 10, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5174425', 'n_tokens_mistral': 1906, 'n_tokens_neox': 1766, 'n_words': 451}