Skip to content

Commit a513051

Browse files
committed
Update chatwork.js
1 parent 1b4631a commit a513051

File tree

1 file changed

+49
-41
lines changed

1 file changed

+49
-41
lines changed

src/chatwork.js

Lines changed: 49 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* ChatWork - JavaScript SDK for ChatWork API v1 Preview
2+
* ChatWork - Chrome JavaScript SDK for ChatWork API v1 Preview
33
*
44
* Copyright 2013, Tetsuwo OISHI.
55
* Dual license under the MIT license.
@@ -13,8 +13,14 @@ function ChatWork(param) {
1313
this.initialize.apply(this, arguments);
1414
}
1515

16+
/**
17+
* Initialize
18+
*
19+
* @param {object} param
20+
*/
1621
ChatWork.prototype.initialize = function(param) {
1722
this.apiBaseUrl = 'https://api.chatwork.com/v1';
23+
this.url = 'https://chatwork.com';
1824
this.name = '_chatwork';
1925
this.times = 0;
2026
this.requests = [];
@@ -35,68 +41,70 @@ ChatWork.prototype.initialize = function(param) {
3541
}
3642
}
3743

38-
this.output(param);
44+
this.log(param);
3945
};
4046

41-
ChatWork.prototype.setApiToken = function(val) {
42-
this.config.apiToken = val;
47+
/**
48+
* Set API Token
49+
*
50+
* @param {string} apiToken
51+
*/
52+
ChatWork.prototype.setApiToken = function(apiToken) {
53+
this.config.apiToken = apiToken;
4354
return this;
4455
};
4556

46-
ChatWork.prototype.output = function(val) {
57+
/**
58+
* Logging
59+
*
60+
*/
61+
ChatWork.prototype.log = function() {
4762
if (this.debug) {
48-
console.log(val);
63+
console.log(arguments);
4964
}
5065
};
5166

67+
/**
68+
* Serialize parameters
69+
*
70+
* @param {object} param
71+
* @param {string} prefix
72+
*/
5273
ChatWork.prototype.serialize = function(param, prefix) {
5374
var query = [];
54-
55-
for(var p in param) {
75+
for (var p in param) {
5676
var k = prefix ? prefix + '[' + p + ']' : p, v = param[p];
5777
query.push(
5878
typeof v == 'object' ?
5979
this.serialize(v, k) :
6080
encodeURIComponent(k) + '=' + encodeURIComponent(v)
6181
);
6282
}
63-
6483
return query.join('&');
6584
};
6685

86+
/**
87+
* API Caller
88+
*
89+
* @param {string} method
90+
* @param {object} param
91+
* @param {function} callback
92+
*/
6793
ChatWork.prototype.api = function(method, param, callback) {
68-
var callbackName = this.name + '_cb_' + this.times;
69-
this.win[callbackName] = callback;
70-
71-
param = param || {};
72-
param.api_key = this.config.apiKey;
73-
param.jsonp = callbackName;
74-
75-
this.requests[this.times] = {
76-
method: method,
77-
param: param,
78-
callback: callback,
79-
callbackName: callbackName
80-
};
81-
82-
if (param.limit === 'all') {
83-
param.limit = null;
94+
var endpoint = this.apiBaseUrl + method;
95+
try {
96+
var xhr = new XMLHttpRequest();
97+
xhr.onreadystatechange = function() {
98+
if (xhr.readyState == 4 && xhr.status == 200) {
99+
var response = JSON.parse(xhr.responseText);
100+
callback(response);
101+
}
102+
};
103+
xhr.open('GET', endpoint);
104+
xhr.setRequestHeader('X-ChatWorkToken', this.config.apiToken);
105+
xhr.send();
106+
} catch (ex) {
107+
this.log('xhr.exception', ex);
84108
}
85-
86-
this.times++;
87-
88-
(function(that, d, t) {
89-
var e = d.createElement(t);
90-
e.type = 'text/javascript';
91-
e.async = true;
92-
e.src = that.apiUrl;
93-
e.src += method;
94-
e.src += '?' + that.serialize(param);
95-
that.output(e.src);
96-
var s = d.getElementsByTagName(t)[0];
97-
s.parentNode.insertBefore(e, s);
98-
})(this, document, 'script');
99-
100109
return this;
101110
};
102-

0 commit comments

Comments
 (0)