|
| 1 | +/*! |
| 2 | + * ChatWork - JavaScript SDK for ChatWork API v1 Preview |
| 3 | + * |
| 4 | + * Copyright 2013, Tetsuwo OISHI. |
| 5 | + * Dual license under the MIT license. |
| 6 | + * http://tetsuwo.tumblr.com |
| 7 | + * |
| 8 | + * Version: 0.0.1 |
| 9 | + * Date: 2013-11-29 |
| 10 | + */ |
| 11 | + |
| 12 | +function ChatWork(param) { |
| 13 | + this.initialize.apply(this, arguments); |
| 14 | +} |
| 15 | + |
| 16 | +ChatWork.prototype.initialize = function(param) { |
| 17 | + this.apiBaseUrl = 'https://api.chatwork.com/v1'; |
| 18 | + this.name = '_chatwork'; |
| 19 | + this.times = 0; |
| 20 | + this.requests = []; |
| 21 | + this.data = []; |
| 22 | + this.debug = false; |
| 23 | + this.win = document.defaultView || document.parentWindow; |
| 24 | + |
| 25 | + this.config = { |
| 26 | + apiToken: null |
| 27 | + }; |
| 28 | + |
| 29 | + if (param) { |
| 30 | + if (param.debug) { |
| 31 | + this.debug = param.debug; |
| 32 | + } |
| 33 | + if (param.apiKey) { |
| 34 | + this.config.apiToken = param.apiToken; |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + this.output(param); |
| 39 | +}; |
| 40 | + |
| 41 | +ChatWork.prototype.setApiToken = function(val) { |
| 42 | + this.config.apiToken = val; |
| 43 | + return this; |
| 44 | +}; |
| 45 | + |
| 46 | +ChatWork.prototype.output = function(val) { |
| 47 | + if (this.debug) { |
| 48 | + console.log(val); |
| 49 | + } |
| 50 | +}; |
| 51 | + |
| 52 | +ChatWork.prototype.serialize = function(param, prefix) { |
| 53 | + var query = []; |
| 54 | + |
| 55 | + for(var p in param) { |
| 56 | + var k = prefix ? prefix + '[' + p + ']' : p, v = param[p]; |
| 57 | + query.push( |
| 58 | + typeof v == 'object' ? |
| 59 | + this.serialize(v, k) : |
| 60 | + encodeURIComponent(k) + '=' + encodeURIComponent(v) |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + return query.join('&'); |
| 65 | +}; |
| 66 | + |
| 67 | +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; |
| 84 | + } |
| 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 | + return this; |
| 101 | +}; |
| 102 | + |
0 commit comments