Class: Neography::Rest::Nodes

Inherits:
Object
  • Object
show all
Extended by:
Paths
Includes:
Helpers
Defined in:
lib/neography/rest/nodes.rb

Instance Method Summary collapse

Methods included from Paths

add_path, build_path, encode

Methods included from Helpers

#get_id, #json_content_type, #parse_direction

Constructor Details

#initialize(connection) ⇒ Nodes

Returns a new instance of Nodes.



10
11
12
# File 'lib/neography/rest/nodes.rb', line 10

def initialize(connection)
  @connection = connection
end

Instance Method Details

#create(*args) ⇒ Object



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

def create(*args)
  if args[0].respond_to?(:each_pair) && args[0]
    create_with_attributes(args[0])
  else
    create_empty
  end
end

#create_emptyObject



47
48
49
# File 'lib/neography/rest/nodes.rb', line 47

def create_empty
  @connection.post(index_path)
end

#create_multiple(nodes) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/neography/rest/nodes.rb', line 55

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

#create_multiple_threaded(nodes) ⇒ Object



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

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

  created_nodes = []

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

#create_with_attributes(attributes) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/neography/rest/nodes.rb', line 39

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

#delete(id) ⇒ Object



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

def delete(id)
  @connection.delete(base_path(:id => get_id(id)))
end

#get(id) ⇒ Object



14
15
16
# File 'lib/neography/rest/nodes.rb', line 14

def get(id)
  @connection.get(base_path(:id => get_id(id)))
end

#get_each(*nodes) ⇒ Object



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

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

#rootObject



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

def root
  root_node = @connection.get('/')["reference_node"]
  @connection.get(base_path(:id => get_id(root_node)))
end