Class: Neography::Rest

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/neography/rest.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = ENV['NEO4J_URL'] || {}) ⇒ Rest

Returns a new instance of Rest.



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
# File 'lib/neography/rest.rb', line 7

def initialize(options=ENV['NEO4J_URL'] || {})
  init = {:protocol       => Neography::Config.protocol, 
          :server         => Neography::Config.server, 
          :port           => Neography::Config.port, 
          :directory      => Neography::Config.directory, 
          :log_file       => Neography::Config.log_file, 
          :log_enabled    => Neography::Config.log_enabled, 
          :max_threads    => Neography::Config.max_threads,
          :authentication => Neography::Config.authentication,
          :username       => Neography::Config.username,
          :password       => Neography::Config.password}

  unless options.respond_to?(:each_pair)
    url = URI.parse(options)
    options = Hash.new
    options[:protocol] = url.scheme + "://"
    options[:server] = url.host
    options[:port] = url.port
    options[:directory] = url.path
    options[:username] = url.user
    options[:password] = url.password
    options[:authentication] = 'basic' unless url.user.nil?
  end

  init.merge!(options)

  @protocol       = init[:protocol]
  @server         = init[:server]
  @port           = init[:port]
  @directory      = init[:directory]
  @log_file       = init[:log_file]
  @log_enabled    = init[:log_enabled]
  @logger         = Logger.new(@log_file) if @log_enabled
  @max_threads    = init[:max_threads]
  @authentication = Hash.new
  @authentication = {"#{init[:authentication]}_auth".to_sym => {:username => init[:username], :password => init[:password]}} unless init[:authentication].empty?
end

Instance Attribute Details

#authenticationObject

Returns the value of attribute authentication.



5
6
7
# File 'lib/neography/rest.rb', line 5

def authentication
  @authentication
end

#directoryObject

Returns the value of attribute directory.



5
6
7
# File 'lib/neography/rest.rb', line 5

def directory
  @directory
end

#log_enabledObject

Returns the value of attribute log_enabled.



5
6
7
# File 'lib/neography/rest.rb', line 5

def log_enabled
  @log_enabled
end

#log_fileObject

Returns the value of attribute log_file.



5
6
7
# File 'lib/neography/rest.rb', line 5

def log_file
  @log_file
end

#loggerObject

Returns the value of attribute logger.



5
6
7
# File 'lib/neography/rest.rb', line 5

def logger
  @logger
end

#max_threadsObject

Returns the value of attribute max_threads.



5
6
7
# File 'lib/neography/rest.rb', line 5

def max_threads
  @max_threads
end

#passwordObject

Returns the value of attribute password.



5
6
7
# File 'lib/neography/rest.rb', line 5

def password
  @password
end

#portObject

Returns the value of attribute port.



5
6
7
# File 'lib/neography/rest.rb', line 5

def port
  @port
end

#protocolObject

Returns the value of attribute protocol.



5
6
7
# File 'lib/neography/rest.rb', line 5

def protocol
  @protocol
end

#serverObject

Returns the value of attribute server.



5
6
7
# File 'lib/neography/rest.rb', line 5

def server
  @server
end

#usernameObject

Returns the value of attribute username.



5
6
7
# File 'lib/neography/rest.rb', line 5

def username
  @username
end

Instance Method Details

#add_node_to_index(index, key, value, id) ⇒ Object Also known as: add_to_index



262
263
264
265
# File 'lib/neography/rest.rb', line 262

def add_node_to_index(index, key, value, id)
  options = { :body => ({:uri =>  self.configuration + "/node/#{get_id(id)}", :key => key, :value => value }).to_json, :headers => {'Content-Type' => 'application/json'} } 
  post("/index/node/#{index}", options)
end

#add_relationship_to_index(index, key, value, id) ⇒ Object



309
310
311
312
# File 'lib/neography/rest.rb', line 309

def add_relationship_to_index(index, key, value, id)
  options = { :body => ({:uri => self.configuration + "/relationship/#{get_id(id)}", :key => key, :value => value}).to_json, :headers => {'Content-Type' => 'application/json'} } 
  post("/index/relationship/#{index}", options)
end

#batch(*args) ⇒ Object



368
369
370
371
372
373
374
375
# File 'lib/neography/rest.rb', line 368

def batch(*args)
  batch = []
  Array(args).each_with_index do |c,i|
    batch << {:id => i}.merge(get_batch(c))
  end
   options = { :body => batch.to_json, :headers => {'Content-Type' => 'application/json'} } 
   post("/batch", options)
end

#configurationObject



52
53
54
# File 'lib/neography/rest.rb', line 52

def configuration
  @protocol + @server + ':' + @port.to_s + @directory + "/db/data"
end

#configure(protocol, server, port, directory) ⇒ Object



45
46
47
48
49
50
# File 'lib/neography/rest.rb', line 45

def configure(protocol, server, port, directory)
  @protocol = protocol
  @server = server
  @port = port 
  @directory = directory
end

#create_node(*args) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/neography/rest.rb', line 60

def create_node(*args)
  if args[0].respond_to?(:each_pair) && args[0] 
    options = { :body => args[0].delete_if { |k, v| v.nil? }.to_json, :headers => {'Content-Type' => 'application/json'} } 
    post("/node", options) 
  else
    post("/node") 
  end
end

#create_node_index(name, type = "exact", provider = "lucene") ⇒ Object



257
258
259
260
# File 'lib/neography/rest.rb', line 257

def create_node_index(name, type = "exact", provider = "lucene")
  options = { :body => ({:name => name, :config => {:type => type, :provider => provider}}).to_json, :headers => {'Content-Type' => 'application/json'} } 
  post("/index/node", options)
end

#create_nodes(nodes) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/neography/rest.rb', line 69

def create_nodes(nodes)
  nodes = Array.new(nodes) if nodes.kind_of? Fixnum
  created_nodes = Array.new
  nodes.each do |node|
      created_nodes <<  create_node(node)
  end
  created_nodes
end

#create_nodes_threaded(nodes) ⇒ Object



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
# File 'lib/neography/rest.rb', line 78

def create_nodes_threaded(nodes)
  nodes = Array.new(nodes) if nodes.kind_of? Fixnum

  node_queue = Queue.new
  thread_pool = []
  responses = Queue.new

  nodes.each do |node|
    node_queue.push node
  end

  [nodes.size, @max_threads].min.times do
    thread_pool << Thread.new do
      until node_queue.empty? do
        node = node_queue.pop
        if node.respond_to?(:each_pair) 
          responses.push( post("/node", { :body => node.to_json, :headers => {'Content-Type' => 'application/json'} } ) )
        else
          responses.push( post("/node") )
        end
      end 
      self.join
    end
  end

  created_nodes = Array.new

  while created_nodes.size < nodes.size 
    created_nodes << responses.pop
  end
  created_nodes
end

#create_relationship(type, from, to, props = nil) ⇒ Object



170
171
172
173
# File 'lib/neography/rest.rb', line 170

def create_relationship(type, from, to, props = nil)
   options = { :body => {:to => self.configuration + "/node/#{get_id(to)}", :data => props, :type => type }.to_json, :headers => {'Content-Type' => 'application/json'} } 
   post("/node/#{get_id(from)}/relationships", options)
end

#create_relationship_index(name, type = "exact", provider = "lucene") ⇒ Object



304
305
306
307
# File 'lib/neography/rest.rb', line 304

def create_relationship_index(name, type = "exact", provider = "lucene")
  options = { :body => ({:name => name, :config => {:type => type, :provider => provider}}).to_json, :headers => {'Content-Type' => 'application/json'} } 
  post("/index/relationship", options)
end

#create_unique_node(index, key, value, props = {}) ⇒ Object



267
268
269
270
# File 'lib/neography/rest.rb', line 267

def create_unique_node(index, key, value, props={})
  options = { :body => ({:properties=>props, :key => key, :value => value }).to_json, :headers => {'Content-Type' => 'application/json'} } 
  post("/index/node/#{index}?unique", options) 
end

#create_unique_relationship(index, key, value, type, from, to) ⇒ Object



175
176
177
178
179
180
181
# File 'lib/neography/rest.rb', line 175

def create_unique_relationship(index, key, value, type, from, to)
  body = {:key=>key,:value=>value, :type => type }
  body[:start] = self.configuration + "/node/#{get_id(from)}"
  body[:end] = self.configuration + "/node/#{get_id(to)}"
  options = { :body => body.to_json, :headers => {'Content-Type' => 'application/json'} } 
  post("/index/relationship/#{index}?unique", options) 
end

#delete_node(id) ⇒ Object



166
167
168
# File 'lib/neography/rest.rb', line 166

def delete_node(id)
  delete("/node/#{get_id(id)}")
end

#delete_node!(id) ⇒ Object



247
248
249
250
251
# File 'lib/neography/rest.rb', line 247

def delete_node!(id)
  relationships = get_node_relationships(get_id(id))
  relationships.each { |r| delete_relationship(r["self"].split('/').last) } unless relationships.nil?
  delete("/node/#{get_id(id)}")
end

#delete_relationship(id) ⇒ Object



231
232
233
# File 'lib/neography/rest.rb', line 231

def delete_relationship(id)
  delete("/relationship/#{get_id(id)}")
end

#execute_query(query, params = {}) ⇒ Object



357
358
359
360
# File 'lib/neography/rest.rb', line 357

def execute_query(query, params = {})
    options = { :body => {:query => query, :params => params}.to_json, :headers => {'Content-Type' => 'application/json'} }
    result = post("/ext/CypherPlugin/graphdb/execute_query", options)
end

#execute_script(script, params = {}) ⇒ Object



362
363
364
365
366
# File 'lib/neography/rest.rb', line 362

def execute_script(script, params = {})
  options = { :body => {:script => script, :params => params}.to_json , :headers => {'Content-Type' => 'application/json'} }
  result = post("/ext/GremlinPlugin/graphdb/execute_script", options)
  result == "null" ? nil : result
end

#find_node_index(*args) ⇒ Object



286
287
288
289
290
291
292
293
# File 'lib/neography/rest.rb', line 286

def find_node_index(*args)
  case args.size
    when 3 then index = get("/index/node/#{args[0]}/#{args[1]}?query=#{args[2]}") || Array.new
    when 2 then index = get("/index/node/#{args[0]}?query=#{args[1]}") || Array.new
  end
  return nil if index.empty?
  index
end

#find_relationship_index(*args) ⇒ Object



328
329
330
331
332
333
334
335
# File 'lib/neography/rest.rb', line 328

def find_relationship_index(*args)
  case args.size
    when 3 then index = get("/index/relationship/#{args[0]}/#{args[1]}?query=#{args[2]}") || Array.new
    when 2 then index = get("/index/relationship/#{args[0]}?query=#{args[1]}") || Array.new
  end
  return nil if index.empty?
  index
end

#get_node(id) ⇒ Object

This is not yet implemented in the REST API

def get_all_node
  puts "get all nodes"
  get("/nodes/")
end


118
119
120
# File 'lib/neography/rest.rb', line 118

def get_node(id)
  get("/node/#{get_id(id)}")
end

#get_node_index(index, key, value) ⇒ Object Also known as: get_index



280
281
282
283
284
# File 'lib/neography/rest.rb', line 280

def get_node_index(index, key, value)
  index = get("/index/node/#{index}/#{key}/#{value}") || Array.new
  return nil if index.empty?
  index
end

#get_node_properties(id, properties = nil) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/neography/rest.rb', line 135

def get_node_properties(id, properties = nil)
  if properties.nil?
    get("/node/#{get_id(id)}/properties")
  else
    node_properties = Hash.new 
    Array(properties).each do |property| 
      value = get("/node/#{get_id(id)}/properties/#{property}")
      node_properties[property] = value unless value.nil?
    end
    return nil if node_properties.empty?
    node_properties
  end
end

#get_node_relationships(id, dir = nil, types = nil) ⇒ Object



235
236
237
238
239
240
241
242
243
244
245
# File 'lib/neography/rest.rb', line 235

def get_node_relationships(id, dir=nil, types=nil)
  dir = get_dir(dir)

  if types.nil?
    node_relationships = get("/node/#{get_id(id)}/relationships/#{dir}") || Array.new
  else
    node_relationships = get("/node/#{get_id(id)}/relationships/#{dir}/#{Array(types).join('&')}") || Array.new
  end
  return nil if node_relationships.empty?
  node_relationships
end

#get_nodes(*nodes) ⇒ Object



122
123
124
125
126
127
128
# File 'lib/neography/rest.rb', line 122

def get_nodes(*nodes)
  gotten_nodes = Array.new
  Array(nodes).flatten.each do |node|
      gotten_nodes <<  get_node(node)
  end
  gotten_nodes
end

#get_path(from, to, relationships, depth = 1, algorithm = "shortestPath") ⇒ Object



347
348
349
350
# File 'lib/neography/rest.rb', line 347

def get_path(from, to, relationships, depth=1, algorithm="shortestPath")
  options = { :body => {"to" => self.configuration + "/node/#{get_id(to)}", "relationships" => relationships, "max_depth" => depth, "algorithm" => get_algorithm(algorithm) }.to_json, :headers => {'Content-Type' => 'application/json'} } 
  path = post("/node/#{get_id(from)}/path", options) || Hash.new
end

#get_paths(from, to, relationships, depth = 1, algorithm = "allPaths") ⇒ Object



352
353
354
355
# File 'lib/neography/rest.rb', line 352

def get_paths(from, to, relationships, depth=1, algorithm="allPaths")
  options = { :body => {"to" => self.configuration + "/node/#{get_id(to)}", "relationships" => relationships, "max_depth" => depth, "algorithm" => get_algorithm(algorithm) }.to_json, :headers => {'Content-Type' => 'application/json'} } 
  paths = post("/node/#{get_id(from)}/paths", options) || Array.new
end

#get_relationship(id) ⇒ Object



183
184
185
# File 'lib/neography/rest.rb', line 183

def get_relationship(id)
  get("/relationship/#{get_id(id)}")
end

#get_relationship_end_node(rel) ⇒ Object



191
192
193
# File 'lib/neography/rest.rb', line 191

def get_relationship_end_node(rel)
  get_node(rel["end"])
end

#get_relationship_index(index, key, value) ⇒ Object



322
323
324
325
326
# File 'lib/neography/rest.rb', line 322

def get_relationship_index(index, key, value)
  index = get("/index/relationship/#{index}/#{key}/#{value}") || Array.new
  return nil if index.empty?
  index
end

#get_relationship_properties(id, properties = nil) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/neography/rest.rb', line 200

def get_relationship_properties(id, properties = nil)
  if properties.nil?
    get("/relationship/#{get_id(id)}/properties")
  else
    relationship_properties = Hash.new 
    Array(properties).each do |property| 
      value = get("/relationship/#{get_id(id)}/properties/#{property}")
      relationship_properties[property] = value unless value.nil?
    end
    return nil if relationship_properties.empty?
    relationship_properties
  end
end

#get_relationship_start_node(rel) ⇒ Object



187
188
189
# File 'lib/neography/rest.rb', line 187

def get_relationship_start_node(rel)
  get_node(rel["start"])
end

#get_rootObject



56
57
58
# File 'lib/neography/rest.rb', line 56

def get_root
  get("/node/#{get_id(get('/')["reference_node"])}") 
end

#list_node_indexesObject Also known as: list_indexes



253
254
255
# File 'lib/neography/rest.rb', line 253

def list_node_indexes
  get("/index/node")
end

#list_relationship_indexesObject



300
301
302
# File 'lib/neography/rest.rb', line 300

def list_relationship_indexes
  get("/index/relationship")
end

#remove_node_from_index(*args) ⇒ Object Also known as: remove_from_index



272
273
274
275
276
277
278
# File 'lib/neography/rest.rb', line 272

def remove_node_from_index(*args)
  case args.size
    when 4 then delete("/index/node/#{args[0]}/#{args[1]}/#{args[2]}/#{get_id(args[3])}")
    when 3 then delete("/index/node/#{args[0]}/#{args[1]}/#{get_id(args[2])}")
    when 2 then delete("/index/node/#{args[0]}/#{get_id(args[1])}")
  end
end

#remove_node_properties(id, properties = nil) ⇒ Object



149
150
151
152
153
154
155
156
157
# File 'lib/neography/rest.rb', line 149

def remove_node_properties(id, properties = nil)
  if properties.nil?
    delete("/node/#{get_id(id)}/properties")
  else 
    Array(properties).each do |property| 
      delete("/node/#{get_id(id)}/properties/#{property}") 
    end
  end
end

#remove_relationship_from_index(*args) ⇒ Object



314
315
316
317
318
319
320
# File 'lib/neography/rest.rb', line 314

def remove_relationship_from_index(*args)
  case args.size
    when 4 then delete("/index/relationship/#{args[0]}/#{args[1]}/#{args[2]}/#{get_id(args[3])}")
    when 3 then delete("/index/relationship/#{args[0]}/#{args[1]}/#{get_id(args[2])}")
    when 2 then delete("/index/relationship/#{args[0]}/#{get_id(args[1])}")
  end
end

#remove_relationship_properties(id, properties = nil) ⇒ Object



214
215
216
217
218
219
220
221
222
# File 'lib/neography/rest.rb', line 214

def remove_relationship_properties(id, properties = nil)
  if properties.nil?
    delete("/relationship/#{get_id(id)}/properties")
  else 
    Array(properties).each do |property| 
      delete("/relationship/#{get_id(id)}/properties/#{property}")
    end
  end
end

#reset_node_properties(id, properties) ⇒ Object



130
131
132
133
# File 'lib/neography/rest.rb', line 130

def reset_node_properties(id, properties)
  options = { :body => properties.to_json, :headers => {'Content-Type' => 'application/json'} } 
  put("/node/#{get_id(id)}/properties", options)
end

#reset_relationship_properties(id, properties) ⇒ Object



195
196
197
198
# File 'lib/neography/rest.rb', line 195

def reset_relationship_properties(id, properties)
  options = { :body => properties.to_json, :headers => {'Content-Type' => 'application/json'} } 
  put("/relationship/#{get_id(id)}/properties", options)
end

#set_node_properties(id, properties) ⇒ Object



159
160
161
162
163
164
# File 'lib/neography/rest.rb', line 159

def set_node_properties(id, properties)
    properties.each do |key, value| 
      options = { :body => value.to_json, :headers => {'Content-Type' => 'application/json'} } 
      put("/node/#{get_id(id)}/properties/#{key}", options) 
    end
end

#set_relationship_properties(id, properties) ⇒ Object



224
225
226
227
228
229
# File 'lib/neography/rest.rb', line 224

def set_relationship_properties(id, properties)
    properties.each do |key, value| 
      options = { :body => value.to_json, :headers => {'Content-Type' => 'application/json'} } 
      put("/relationship/#{get_id(id)}/properties/#{key}", options)
    end
end

#traverse(id, return_type, description) ⇒ Object



337
338
339
340
341
342
343
344
345
# File 'lib/neography/rest.rb', line 337

def traverse(id, return_type, description)
  options = { :body => {"order" => get_order(description["order"]), 
                        "uniqueness" => get_uniqueness(description["uniqueness"]), 
                        "relationships" => description["relationships"], 
                        "prune_evaluator" => description["prune evaluator"], 
                        "return_filter" => description["return filter"], 
                        "max_depth" => get_depth(description["depth"]), }.to_json, :headers => {'Content-Type' => 'application/json'} } 
  traversal = post("/node/#{get_id(id)}/traverse/#{get_type(return_type)}", options) || Array.new
end