Skip to content

Commit

Permalink
Update chatwork.js
Browse files Browse the repository at this point in the history
  • Loading branch information
tetsuwo committed Jan 5, 2014
1 parent 1b4631a commit a513051
Showing 1 changed file with 49 additions and 41 deletions.
90 changes: 49 additions & 41 deletions src/chatwork.js
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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 = [];
Expand All @@ -35,68 +41,70 @@ 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' ?
this.serialize(v, k) :
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;
};

0 comments on commit a513051

Please sign in to comment.