This repository has been archived by the owner on Mar 10, 2020. It is now read-only.
/
xfuse.coffee
194 lines (164 loc) · 5 KB
/
xfuse.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
###
copyright (c) 2012 Avectra, Inc.
###
https = require 'https'
qs = require 'querystring'
exports.version = '0.1.0'
apiToken = null
siteUrl = null
apiUrl = '/api/rest/1.0'
class XFuse
constructor : (method, url, postData = {}, callback = () -> return) ->
if typeof postData == 'function'
callback = postData
postData = {}
###
Check if everything is set up
###
@isSetUp(method, url, postData, callback)
url = apiUrl + @.cleanUrl url
@callback = callback
@postData = JSON.stringify postData
@options = @options || {}
@options.host = siteUrl
@options.hostname = siteUrl
@options.auth = apiToken + ':'
@options.method = method
@options.path = url
###
Have to do this because delete is a reserved operator
###
if method.toLowerCase() == 'delete'
@del()
else
@[method.toLowerCase()]()
return @
isSetUp : (method, url, postData, callback) ->
if apiToken == null
return callback { message : "xfuse needs an API Token" }, null
if siteUrl == null
return callback { message : "xfuse needs the address to memberfuse community" }, null
if typeof url != 'string'
return callback { message : "xfuse requires url to be a string" }, null
if typeof postData != 'object'
return callback { message : "Data must be in Object form" }, null
return
cleanUrl : (url) ->
if url.charAt(0) != '/'
url = '/' + url
return url
end : (body) ->
if (body && typeof body == 'string')
try
json = JSON.parse body
catch e
err =
message : 'Error parsing json'
exception : e
if (!err && (json && json.error))
err = json.error
@callback err, json
return
get : () ->
https.get @options, (res) =>
if res.statusCode != 200
this.callback
message : 'Request could not be interpreted by the server'
exception : Error
, null
return
body = ''
res.setEncoding 'utf8'
res.on 'data', (chunk) ->
body += chunk
return
res.on 'end', () =>
@end body
return
return
.on 'error', (e) =>
@callback e, null
return
return
post : () ->
req = https.request @options, (res) =>
body = ''
res.setEncoding 'utf8'
res.on 'data', (chunk) ->
body += chunk
return
res.on 'end', () =>
@end body
return
return
req.on 'error', (e) =>
@callback e, null
return
req.setHeader 'Content-Type', 'application/json'
req.setHeader 'Content-Length', @postData.length
req.end @postData, 'utf8'
return
put : () ->
req = https.request @options, (res) =>
body = ''
res.setEncoding 'utf8'
res.on 'data', (chunk) ->
body += chunk
return
res.on 'end', () =>
@end body
return
return
req.on 'error', (e) =>
@callback e, null
return
req.setHeader 'Content-Type', 'application/json'
req.setHeader 'Content-Length', @postData.length
req.end @postData, 'utf8'
return
del : () ->
req = https.request @options, (res) =>
body = ''
res.setEncoding 'utf8'
res.on 'data', (chunk) ->
body += chunk
return
res.on 'end', () =>
@end body
return
req.on 'error', (e) =>
@callback e, null
return
req.setHeader 'Content-Type', 'application/json'
req.setHeader 'Content-Length', 0
req.end()
return
exports.get = (url, params, callback) ->
if typeof params == 'function'
callback = params
params = null
if params
url += '?' + qs.stringify params
new XFuse 'GET', url, callback
exports.post = (url, postData, callback) ->
new XFuse 'POST', url, postData, callback
exports.put = (url, putData, callback) ->
new XFuse 'PUT', url, putData, callback
exports.del = (url, callback) ->
new XFuse 'DELETE', url, callback
exports.setOptions = (options) ->
if typeof options == 'object'
@options = options
@
exports.getOptions = () ->
@options || {}
exports.setApiToken = (token) ->
apiToken = token
@
exports.getApiToken = () ->
return apiToken
exports.setSiteUrl = (url) ->
siteUrl = url
@
exports.getSiteUrl = () ->
siteUrl