-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathjanus.coffee
132 lines (97 loc) · 3.11 KB
/
janus.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
util = require 'util'
{Client} = require 'node-rest-client'
client = new Client
class RequestError extends Error
constructor: (@response, message = @response.statusMessage, fileName, lineNumber) ->
super message, fileName, lineNumber
config =
host: 'localhost'
port: 8182
proto: 'http'
config.url = "#{config.proto}://#{config.host}:#{config.port}/"
request = (method, query) ->
args =
data: gremlin: query
headers:
"Content-Type": "application/json"
{url} = config
new Promise (resolve, reject) ->
client[method] url, args, (data, response) ->
switch response.statusCode
when 200 then resolve data
else reject new RequestError response
get = (path, data) ->
global.requested = get: [path, data]
request 'get', path, data
.then (ret) -> ret.result.data
post = (path, data) ->
global.posted = post: [path, data]
request 'post', path, data
.then (ret) -> ret.result.data
if false
get()
.catch (e) -> console.log e
.then (d) -> console.log d
p = (promise) ->
promise
.then (d) -> console.log util.inspect global.d = d, colors: true
.catch (e) -> console.log "error", (global.e = e).response?.statusMessage or util.inspect e, colors: true
englishList = (l, conjunction) ->
[first..., last] = l
(switch l.length
when 0 then []
when 1 then [l[0]]
when 2 then [l[0], conjunction, last]
else [first.join(", "), conjunction, last]
).join " "
expectOneOf = (value, types...) ->
if t = typeof value not in types
throw new TypeError "Wanted one of #{englishList types, 'or'}, got #{t}"
expectString = (value...) ->
value.forEach (v) ->
if 'string' isnt t = typeof v
throw new TypeError "Wanted string, got #{t}"
property = (key, value) -> util.format '.property(%j, %j)', key, value
has = (propName, propValue) -> util.format ".has('%j', '%j')"
lookup = (propName, propValue) -> ".V()" + has 'name', propValue
named = (name) -> lookup 'name', name
as = (alias) ->
expectString alias
util.format ".as(%j)", alias
create = (subjects) ->
expectOneOf subjects, 'string', 'object'
if 'string' is typeof subjects
subjects = "#{subjects}": {}
for type, instances of subjects
for name, properties of instances
q = util.format "g.addV(%j)", type
properties.name = name
for key, value of properties
q += property key, value
post q
connect = (a, dir, type, b, props) ->
expectString a, b
q = "g" + named(a) + as('a') + named(b)
q += util.format ".addE(%j).%s('a')", type, dir
if props
for key, value of props
q += property key, value
post q
connectTo = (a, type, b, props) -> connect a, 'to', type, props
connectFrom = (a, type, b, props) -> connect a, 'from', type, props
list = (regex) ->
select =
if regex
(v) -> v.properties.name.filter(({id, value}) -> value.match regex).length
else
-> true
post "g.V()"
.then (data) ->
data.filter select
Object.assign global, module.exports =
{
Client, client, RequestError, config, request
get, post, p
create, connect, connectTo, connectFrom
list
}