1
1
/*!
2
- * ChatWork - JavaScript SDK for ChatWork API v1 Preview
2
+ * ChatWork - Chrome JavaScript SDK for ChatWork API v1 Preview
3
3
*
4
4
* Copyright 2013, Tetsuwo OISHI.
5
5
* Dual license under the MIT license.
@@ -13,8 +13,14 @@ function ChatWork(param) {
13
13
this . initialize . apply ( this , arguments ) ;
14
14
}
15
15
16
+ /**
17
+ * Initialize
18
+ *
19
+ * @param {object } param
20
+ */
16
21
ChatWork . prototype . initialize = function ( param ) {
17
22
this . apiBaseUrl = 'https://api.chatwork.com/v1' ;
23
+ this . url = 'https://chatwork.com' ;
18
24
this . name = '_chatwork' ;
19
25
this . times = 0 ;
20
26
this . requests = [ ] ;
@@ -35,68 +41,70 @@ ChatWork.prototype.initialize = function(param) {
35
41
}
36
42
}
37
43
38
- this . output ( param ) ;
44
+ this . log ( param ) ;
39
45
} ;
40
46
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 ;
43
54
return this ;
44
55
} ;
45
56
46
- ChatWork . prototype . output = function ( val ) {
57
+ /**
58
+ * Logging
59
+ *
60
+ */
61
+ ChatWork . prototype . log = function ( ) {
47
62
if ( this . debug ) {
48
- console . log ( val ) ;
63
+ console . log ( arguments ) ;
49
64
}
50
65
} ;
51
66
67
+ /**
68
+ * Serialize parameters
69
+ *
70
+ * @param {object } param
71
+ * @param {string } prefix
72
+ */
52
73
ChatWork . prototype . serialize = function ( param , prefix ) {
53
74
var query = [ ] ;
54
-
55
- for ( var p in param ) {
75
+ for ( var p in param ) {
56
76
var k = prefix ? prefix + '[' + p + ']' : p , v = param [ p ] ;
57
77
query . push (
58
78
typeof v == 'object' ?
59
79
this . serialize ( v , k ) :
60
80
encodeURIComponent ( k ) + '=' + encodeURIComponent ( v )
61
81
) ;
62
82
}
63
-
64
83
return query . join ( '&' ) ;
65
84
} ;
66
85
86
+ /**
87
+ * API Caller
88
+ *
89
+ * @param {string } method
90
+ * @param {object } param
91
+ * @param {function } callback
92
+ */
67
93
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 ) ;
84
108
}
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
-
100
109
return this ;
101
110
} ;
102
-
0 commit comments