Module: Neography::Rest::Nodes

Includes:
Helpers
Included in:
Neography::Rest
Defined in:
lib/neography/rest/nodes.rb

Instance Method Summary collapse

Methods included from Helpers

#encode, #escape, #get_id, #json_content_type, #parse_depth, #parse_direction, #parse_order, #parse_type, #parse_uniqueness

Instance Method Details

#create_empty_nodeObject



39
40
41
# File 'lib/neography/rest/nodes.rb', line 39

def create_empty_node
  @connection.post("/node")
end

#create_node(*args) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/neography/rest/nodes.rb', line 23

def create_node(*args)
  if args[0].respond_to?(:each_pair) && args[0]
    create_node_with_attributes(args[0])
  else
    create_empty_node
  end
end

#create_node_with_attributes(attributes) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/neography/rest/nodes.rb', line 31

def create_node_with_attributes(attributes)
  options = {
    :body => attributes.delete_if { |k, v| v.nil? }.to_json,
    :headers => json_content_type
  }
  @connection.post("/node", options)
end

#create_nodes(nodes) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/neography/rest/nodes.rb', line 47

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

#create_nodes_threaded(nodes) ⇒ Object



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

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, @connection.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( @connection.post("/node", {
            :body => node.to_json,
            :headers => json_content_type
          } ) )
        else
          responses.push( @connection.post("/node") )
        end
      end
      self.join
    end
  end

  created_nodes = []

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

#delete_node(id) ⇒ Object



43
44
45
# File 'lib/neography/rest/nodes.rb', line 43

def delete_node(id)
  @connection.delete("/node/%{id}" % {:id => get_id(id)})
end

#get_node(id) ⇒ Object



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

def get_node(id)
  @connection.get("/node/%{id}" % {:id => get_id(id)})
end

#get_nodes(*nodes) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/neography/rest/nodes.rb', line 10

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

#get_rootObject



18
19
20
21
# File 'lib/neography/rest/nodes.rb', line 18

def get_root
  root_node = @connection.get('/')["reference_node"]
  @connection.get("/node/%{id}" % {:id => get_id(root_node)})
end