-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgremlin-endpoint.rb
166 lines (139 loc) · 4.55 KB
/
gremlin-endpoint.rb
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
require 'sinatra'
require 'rest-client'
require 'json'
# Documentation on rest client http://rubydoc.info/gems/rest-client/1.6.7/frames
rest = RestClient::Resource.new(ENV['NEO4J_URL'])
before do
content_type 'application/json'
end
post '/db/data/ext/GremlinPlugin/graphdb/execute_script' do
data = request.body.read;
begin
data = JSON.parse(data)
rescue
end
data = {:query => data } unless data.kind_of?(Hash)
rest["/db/data/ext/GremlinPlugin/graphdb/execute_script"].post data.to_json,
{:accept=>"application/json",:content_type=>"application/json"}
end
# Neo4j REST Routes
get '/' do
response = RestClient.get ENV['NEO4J_URL'] + '/db/data/', {:content_type => :json, :accept => :json}
ReplaceHostNameWithProxyHostName(response)
end
get '/db/data/node/:nodeid' do
response = RestClient.get ENV['NEO4J_URL'] + '/db/data/node/' + params[:nodeid], {:content_type => :json, :accept => :json}
ReplaceHostNameWithProxyHostName(response)
end
get '/db/data/node/:nodeid/properties' do
response = RestClient.get ENV['NEO4J_URL'] + '/db/data/node/' + params[:nodeid] + '/properties', {:content_type => :json, :accept => :json}
ReplaceHostNameWithProxyHostName(response)
end
put '/db/data/node/:nodeid/properties' do
begin
data = request.body.read;
data = JSON.parse(data)
data = {:query => data } unless data.kind_of?(Hash)
address = "/db/data/node/" + params[:nodeid] + "/properties"
response = rest[address].put data.to_json,
{:accept=>"application/json",:content_type=>"application/json"}
if response == ""
status 204
end
rescue Exception => e
response = 'HOSTRESOURCE: ' + address + ' MESSAGE: ' + e.message + ' BACKTRACE: ' + e.backtrace.inspect
if response.include? "404"
status 404
end
if response.include? "409"
status 409
end
end
end
post '/db/data/node/:nodeid/relationships' do
data = request.body.read;
begin
data = JSON.parse(data)
data = data unless data.kind_of?(Hash)
rest["/db/data/node/" + params[:nodeid] + "/relationships"].post data.to_json,
{:accept=>"application/json",:content_type=>"application/json"}
status 201
rescue Exception => e
response = 'HOSTRESOURCE: ' + address + ' MESSAGE: ' + e.message + ' BACKTRACE: ' + e.backtrace.inspect
if response.include? "404"
status 404
end
if response.include? "400"
status 400
end
end
end
get '/db/data/node/:nodeid/relationships/:relationships' do
response = RestClient.get ENV['NEO4J_URL'] + '/db/data/node/' + params[:nodeid] + '/relationships/' + params[:relationships], {:content_type => :json, :accept => :json}
ReplaceHostNameWithProxyHostName(response)
end
post '/db/data/batch' do
data = request.body.read;
begin
data = JSON.parse(data)
rescue
end
data = data unless data.kind_of?(Hash)
rest["/db/data/batch"].post data.to_json,
{:accept=>"application/json",:content_type=>"application/json"}
end
delete '/db/data/relationship/:relationshipid' do
begin
address = ENV['NEO4J_URL'] + '/db/data/relationship/' + params[:relationshipid]
response = RestClient.delete address
if response == ""
status 204
end
rescue Exception => e
response = 'HOSTRESOURCE: ' + address + ' MESSAGE: ' + e.message + ' BACKTRACE: ' + e.backtrace.inspect
if response.include? "404"
status 404
end
if response.include? "409"
status 409
end
end
end
delete '/relationship/:relationshipid' do
begin
address = ENV['NEO4J_URL'] + '/db/data/relationship/' + params[:relationshipid]
response = RestClient.delete address
if response == ""
status 204
end
rescue Exception => e
response = 'HOSTRESOURCE: ' + address + ' MESSAGE: ' + e.message + ' BACKTRACE: ' + e.backtrace.inspect
if response.include? "404"
status 404
end
if response.include? "409"
status 409
end
end
end
delete '/db/data/node/:nodeid' do
begin
address = ENV['NEO4J_URL'] + '/db/data/node/' + params[:nodeid]
response = RestClient.delete address
if response == ""
status 204
end
rescue Exception => e
response = 'HOSTRESOURCE: ' + address + ' MESSAGE: ' + e.message + ' BACKTRACE: ' + e.backtrace.inspect
if response.include? "404"
status 404
end
if response.include? "409"
status 409
end
end
end
# functions
def ReplaceHostNameWithProxyHostName(response)
response.gsub(/(http:\/\/\w+\W*.*\/db\/data)/, "http://" + ENV['APP_NAME'] + ".heroku.com/db/data")
end