From a51305153bb151571ef6985097642e383067d871 Mon Sep 17 00:00:00 2001 From: Tetsuwo OISHI Date: Sun, 5 Jan 2014 22:48:43 +0900 Subject: [PATCH] Update chatwork.js --- src/chatwork.js | 90 +++++++++++++++++++++++++++---------------------- 1 file changed, 49 insertions(+), 41 deletions(-) diff --git a/src/chatwork.js b/src/chatwork.js index 56fc928..5410943 100644 --- a/src/chatwork.js +++ b/src/chatwork.js @@ -1,5 +1,5 @@ /*! - * ChatWork - JavaScript SDK for ChatWork API v1 Preview + * ChatWork - Chrome JavaScript SDK for ChatWork API v1 Preview * * Copyright 2013, Tetsuwo OISHI. * Dual license under the MIT license. @@ -13,8 +13,14 @@ function ChatWork(param) { this.initialize.apply(this, arguments); } +/** + * Initialize + * + * @param {object} param + */ ChatWork.prototype.initialize = function(param) { this.apiBaseUrl = 'https://api.chatwork.com/v1'; + this.url = 'https://chatwork.com'; this.name = '_chatwork'; this.times = 0; this.requests = []; @@ -35,24 +41,38 @@ ChatWork.prototype.initialize = function(param) { } } - this.output(param); + this.log(param); }; -ChatWork.prototype.setApiToken = function(val) { - this.config.apiToken = val; +/** + * Set API Token + * + * @param {string} apiToken + */ +ChatWork.prototype.setApiToken = function(apiToken) { + this.config.apiToken = apiToken; return this; }; -ChatWork.prototype.output = function(val) { +/** + * Logging + * + */ +ChatWork.prototype.log = function() { if (this.debug) { - console.log(val); + console.log(arguments); } }; +/** + * Serialize parameters + * + * @param {object} param + * @param {string} prefix + */ ChatWork.prototype.serialize = function(param, prefix) { var query = []; - - for(var p in param) { + for (var p in param) { var k = prefix ? prefix + '[' + p + ']' : p, v = param[p]; query.push( typeof v == 'object' ? @@ -60,43 +80,31 @@ ChatWork.prototype.serialize = function(param, prefix) { encodeURIComponent(k) + '=' + encodeURIComponent(v) ); } - return query.join('&'); }; +/** + * API Caller + * + * @param {string} method + * @param {object} param + * @param {function} callback + */ ChatWork.prototype.api = function(method, param, callback) { - var callbackName = this.name + '_cb_' + this.times; - this.win[callbackName] = callback; - - param = param || {}; - param.api_key = this.config.apiKey; - param.jsonp = callbackName; - - this.requests[this.times] = { - method: method, - param: param, - callback: callback, - callbackName: callbackName - }; - - if (param.limit === 'all') { - param.limit = null; + var endpoint = this.apiBaseUrl + method; + try { + var xhr = new XMLHttpRequest(); + xhr.onreadystatechange = function() { + if (xhr.readyState == 4 && xhr.status == 200) { + var response = JSON.parse(xhr.responseText); + callback(response); + } + }; + xhr.open('GET', endpoint); + xhr.setRequestHeader('X-ChatWorkToken', this.config.apiToken); + xhr.send(); + } catch (ex) { + this.log('xhr.exception', ex); } - - this.times++; - - (function(that, d, t) { - var e = d.createElement(t); - e.type = 'text/javascript'; - e.async = true; - e.src = that.apiUrl; - e.src += method; - e.src += '?' + that.serialize(param); - that.output(e.src); - var s = d.getElementsByTagName(t)[0]; - s.parentNode.insertBefore(e, s); - })(this, document, 'script'); - return this; }; -