-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmintpresso-0.2.coffee
More file actions
435 lines (394 loc) · 15.6 KB
/
mintpresso-0.2.coffee
File metadata and controls
435 lines (394 loc) · 15.6 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
###
mintpresso-0.2.coffee
Author: Jinhyuk Lee <eces at mstock.org>
Organization: MINTPRESSO <support at mintpresso.com>
Repository: https://github.com/mintpresso/javascript-api
Description:
This is a JavaScript API library for MINTPRESSO Data Cloud.
Supports basic graph operations.
Documentation: docs.mintpresso.com/javascript-api/index.html
###
if String.prototype.format is undefined
String.prototype.format = () ->
_arguments = arguments
this.replace /{(\d+)}/g, (match, number) ->
if typeof _arguments[number] isnt 'undefined' then _arguments[number] else match
String.prototype.endsWith = (suffix) ->
return (this.substr(this.length - suffix.length) is suffix)
String.prototype.startsWith = (prefix) ->
return (this.substr(0, prefix.length) is prefix)
try
prefix =
log: '[MINTPRESSO] '
version: '/v1'
client =
name: 'JS 0.2 API'
key: ''
id: ''
useDebugCallback: true
getAPI: () ->
return 'api_token=' + client.key
server =
list: []
iteration: 0
get: () ->
return server.list[server.iteration]
isReady: false
timeout: 5000
urls:
getPoint: "/account/{0}/point/{1}?"
getPointByTypeOrIdentifier: "/account/{0}/point?type={1}&identifier={2}&"
addPoint: "/post/account/{0}/point?json={1}&updateIfExists={2}&"
findEdge: "/account/{0}/edge?subjectId={1}&subjectType={2}&subjectIdentifier={3}&verb={4}&objectId={5}&objectType={6}&objectIdentifier={7}&getInnerModels={8}&"
linkWithEdge: "/post/account/{0}/edge?json={1}&"
dataType: 'jsonp'
useCallback: true
callbackName: 'JSAPIMINTPRESSOCALLBACK'
feature =
pageTracker: false
model =
verbs: new Array('do', 'does', 'did', 'verb')
mark: '?'
subjectMark: '$'
objectMark: '_'
point:
prototype: new Array('type', 'identifier', 'data')
edge:
prototype: new Array('subjectId', 'subjectType', 'verb', 'objectId', 'objectType')
getPoint = (id, callback) ->
jQuery.ajax {
url: server.get() + prefix.version + server.urls.getPoint.format(client.id, id) + client.getAPI()
type: 'GET'
async: true
cache: false
crossDomain: true
dataType: server.dataType
jsonpCallback: server.callbackName
timeout: server.timeout
success: (json) ->
data = undefined
if json.status.code is 200
if json.point.data isnt undefined
for key of json.point.data
if key isnt 'data'
json.point[key] = json.point.data[key]
else
data = json.point.data[key]
if data is undefined
`delete json.point.data`
else
json.point.data = data
callback json
error: (xhr, status, error) ->
console.error "#{prefix.log} Response(#{status}) #{error}"
callback {
status: {
code: status
message: "Response(#{status}) #{error}"
}
}
}
getPointByTypeOrIdentifier = (json, callback) ->
i = 0
_type = ""
_identifier = ""
for key of json
if i > 0
console.log "#{prefix.log}Too many arguments are given to be an informative query though no question marks are found - mintpresso.get"
break
if key.length is 0 or key isnt model.mark
_type = encodeURIComponent(key)
if json[key].length is 0 or json[key] is model.mark
console.log prefix.log + "#{prefix.log}No question mark is allowed on 'identifier' field - mintpresso.get"
return false
else
_identifier = encodeURIComponent(json[key])
i++
jQuery.ajax {
url: server.get() + prefix.version + server.urls.getPointByTypeOrIdentifier.format(client.id, _type, _identifier) + client.getAPI()
type: 'GET'
async: true
cache: false
crossDomain: true
dataType: server.dataType
jsonpCallback: server.callbackName
timeout: server.timeout
success: (json) ->
data = undefined
if json.status.code is 200
if json.point isnt undefined
if json.point.data isnt undefined
for key of json.point.data
if key isnt 'data'
json.point[key] = json.point.data[key]
else
data = json.point.data[key]
if data is undefined
`delete json.point.data`
else
json.point.data = data
else if json.points isnt undefined
for i in [0..json.points.length-1] by 1
point = json.points[i]
if point.data isnt undefined
for key of point.data
if key isnt 'data'
json.points[i][key] = point.data[key]
else
data = point.data[key]
if data is undefined
`delete json.points[i].data`
else
json.points[i].data = data
else
console.error prefix.log + "Found results neither point nor points - mintpresso._getPointByTypeOrIdentifier"
callback json
error: (xhr, status, error) ->
console.error "#{prefix.log} Response(#{status}) #{error}"
callback JSON.parse(xhr.responseText)
}
findEdges = (json, callback, getInnerModels = false) ->
i = 1
sType = ''
sId = -1
sString = ''
v = ''
oType = ''
oId = -1
oString = ''
for key of json
switch i
when 1
if key isnt undefined and key.indexOf(model.subjectMark) is 0
sType = key.substring(1, key.length)
else
sType = key
sType = encodeURIComponent(sType)
sId = encodeURIComponent(json[key]) if json[key] isnt model.mark and typeof json[key] is 'number'
sString = encodeURIComponent(json[key]) if json[key] isnt model.mark and typeof json[key] is 'string'
when 2
if model.verbs.indexOf(key) is -1
console.log prefix.log + 'Verb isn\'t match with do/does/did/verb. - mintpresso.get'
else
v = encodeURIComponent(json[key]) if json[key] isnt model.mark
when 3
if key isnt undefined and key.indexOf(model.objectMark) is 0
oType = key.substring(1, key.length)
else
oType = key
oType = encodeURIComponent(oType)
oId = encodeURIComponent(json[key]) if json[key] isnt model.mark and typeof json[key] is 'number'
oString = encodeURIComponent(json[key]) if json[key] isnt model.mark and typeof json[key] is 'string'
else
console.log prefix.log + 'Too many arguments are given to be a form of subject/verb/object query - mintpresso.get'
return false
i++
jQuery.ajax {
url: server.get() + prefix.version + server.urls.findEdge.format(client.id, sId, sType, sString, v, oId, oType, oString, getInnerModels) + client.getAPI()
type: 'GET'
async: true
cache: false
crossDomain: true
dataType: server.dataType
jsonpCallback: server.callbackName
timeout: server.timeout
success: (json) ->
callback json
error: (xhr, status, error) ->
console.error "#{prefix.log} Response(#{status}) #{error}"
callback JSON.parse(xhr.responseText)
}
addPoint = (json, callback, updateIfExists = false) ->
value = {}
value.point = {}
value.point.data = {}
# Promote first citizen keys
for key of json
if model.point.prototype.indexOf(key) isnt -1
value.point[key] = json[key]
else
value.point.data[key] = json[key]
jQuery.ajax {
url: server.get() + prefix.version + server.urls.addPoint.format(client.id, encodeURIComponent(JSON.stringify(value)), updateIfExists) + client.getAPI()
type: 'GET'
async: true
cache: false
crossDomain: true
dataType: server.dataType
jsonpCallback: server.callbackName
timeout: server.timeout
success: (json) ->
callback json
error: (xhr, status, error) ->
console.error "#{prefix.log} Response(#{status}) #{error}"
callback JSON.parse(xhr.responseText)
}
linkWithEdge = (json, callback) ->
value = {}
value.edge = {}
i = 1
for key of json
switch i
when 1
value.edge.subjectType = key if key isnt model.mark and model.edge.prototype.indexOf(key) is -1
value.edge.subjectId = json[key] if json[key] isnt model.mark
if value.edge.subjectType isnt undefined and value.edge.subjectType.indexOf(model.subjectMark) is 0
value.edge.subjectType = value.edge.subjectType.substring(1, value.edge.subjectType.length)
when 2
if model.verbs.indexOf(key) is -1
console.log "#{prefix.log} Verb isn\'t match with do/does/did/verb. - mintpresso.set"
else
value.edge.verb = json[key]
when 3
value.edge.objectType = key if key isnt model.mark and model.edge.prototype.indexOf(key) is -1
value.edge.objectId = json[key] if json[key] isnt model.mark
if value.edge.objectType isnt undefined and value.edge.objectType.indexOf(model.objectMark) is 0
value.edge.objectType = value.edge.objectType.substring(1, value.edge.objectType.length)
else
console.log "#{prefix.log} Too many arguments are given to be a form of subject/verb/object query - mintpresso.set"
return false
i++
jQuery.ajax {
url: server.get() + prefix.version + server.urls.linkWithEdge.format(client.id, encodeURIComponent(JSON.stringify(value))) + client.getAPI()
type: 'GET'
async: true
cache: false
crossDomain: true
dataType: server.dataType
jsonpCallback: server.callbackName
timeout: server.timeout
success: (json) ->
callback json
error: (xhr, status, error) ->
console.error "#{prefix.log} Response(#{status}) #{error}"
callback JSON.parse(xhr.responseText)
}
window.mintpresso = {}
window.mintpresso =
get: () ->
###
@param
Object json, Function callback[, Boolean getInnerModels = false]
###
# Object json, Function callback[, Boolean getInnerModels = false | Object optionsObject{getInnerModels: Boolean = false})]
if server.isReady is false
return console.warn "#{prefix.log}Not initialized. Add mintpress.init in your code with API key."
if arguments.length is 0
console.warn "#{prefix.log}An argument is required for mintpresso.get method."
else if arguments.length <= 2
getInnerModels = false
# if arguments[2] isnt undefined
# if arguments[2] typeof 'boolean'
# getInnerModels = arguments
# else if arguments[2] typeof 'object' and `'getInnerModels' in arguments[2]`
# getInnerModels = Boolean(arguments[2] === true)
if arguments[2] isnt undefined and typeof 'boolean'
getInnerModels = Boolean(arguments[2] is true)
if arguments[1] isnt undefined and typeof arguments[1] is 'function'
callback = arguments[1]
else
callback = window.mintpresso.callback
if typeof arguments[0] is 'number'
return getPoint arguments[0], callback
else if typeof arguments[0] is 'object'
json = arguments[0]
hasMark = false
conditions = 0
for key of json
conditions++
if key is "?" or json[key] is "?"
hasMark = true
if (hasMark and conditions > 1) or conditions is 3
findEdges arguments[0], callback, getInnerModels
else
getPointByTypeOrIdentifier arguments[0], callback
else
console.warn "#{prefix.log} An argument type of Number or String is required for mintresso.get method."
else
console.warn "#{prefix.log} Too many arguments in mintpresso.get method."
set: () ->
###
@param
Object json[, Function callback[, Boolean updateIfExists = false]]
###
if server.isReady is false
return console.warn "#{prefix.log}Not initialized. Add mintpress.init in your code with API key."
if arguments.length is 0
console.warn "#{prefix.log}An argument is required for mintpresso.set method."
else if arguments.length <= 3
if typeof arguments[0] is 'object'
isEdgeOperation = false
for key of arguments[0]
if model.verbs.indexOf(key) isnt -1
isEdgeOperation = true
break
updateIfExists = false
callback = window.mintpresso.callback
if arguments[1] isnt undefined
if typeof arguments[1] is 'function'
callback = arguments[1]
if arguments[2] isnt undefined and typeof arguments is 'boolean'
updateIfExists = Boolean(arguments[2] is true)
else if typeof arguments[1] is 'boolean'
updateIfExists = Boolean(arguments[1] is true)
if isEdgeOperation is true
linkWithEdge arguments[0], callback
else
if arguments[1] is undefined or arguments[1] is false
addPoint arguments[0], callback, updateIfExists
else
addPoint arguments[0], callback, updateIfExists
else
console.warn "#{prefix.log}An JSON object is required for mintpresso.set method."
else
console.warn "#{prefix.log}Too many arguments in mintpresso.get method."
true
# For debug
callback: (response) ->
if client.useDebugCallback is true
if response?.status?.code > 201
console.log "#{prefix.log}Response(#{response.status.code}): #{response.status.message}", response
else
console.log "#{prefix.log}Response(#{response.status.code}): ", response
init: (key, id, option) ->
if typeof key isnt 'string'
return console.warn "#{prefix.log} Not initialized. Invalid API key. (required: String)"
# uuid = key.match /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i
# if uuid is null or uuid.length is 0
# return console.warn "#{prefix.log} Not initialized. Invalid API key. (required: UUID Format String)"
client.key = key
client.id = id
# use ajax callback (required CORS) or not
if option.withoutCallback isnt undefined and option.withoutCallback is true
server.dataType = 'json'
server.callbackName = undefined
server.useCallback = false
else
server.useCallback = true
# use custom domain for any purpose
domain = '//api.mintpresso.com'
if option.useLocalhost isnt undefined and option.useLocalhost is true
console.log "#{prefix.log}Using localhost server (http://localhost:15100)"
domain = '//localhost:15100'
# init server urls
if 'https:' is document.location.protocol
server.list.push 'https:' + domain
else
server.list.push 'http:' + domain
# call given function just after mintpressso API init.
if option.callbackFunction isnt undefined and option.callbackFunction.length > 0 and `option.callbackFunction in window`
window[option.callbackFunction](window.mintpresso)
console.log "#{prefix.log}window.#{option.callbackFunction} is called."
# show description of all queries and APIs
if option.disableDebugCallback isnt undefined and option.disableDebugCallback is true
client.useDebugCallback = false
else
client.useDebugCallback = true
server.isReady = true
true
catch e
if window.mintpresso isnt undefined and true
throw e
else
console.warn "#{prefix.log} Failed to load API."
true