Module: Architect4r::Core::NodeMethods

Extended by:
ActiveSupport::Concern
Included in:
Server
Defined in:
lib/architect4r/core/node_methods.rb

Instance Method Summary collapse

Instance Method Details

#create_node(properties) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/architect4r/core/node_methods.rb', line 6

def create_node(properties)
  # Send request
  response = Typhoeus::Request.post(prepend_base_url('/node'), 
    :headers => { 'Accept' => 'application/json', 'Content-Type' => 'application/json' },
    :body => properties.to_json)
  
  # Evaluate response
  response.code == 201 ? JSON.parse(response.body) : nil
end

#delete_node(id) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/architect4r/core/node_methods.rb', line 54

def delete_node(id)
  # Delete all relationships
  get_node_relationships(id, :all).each do |rel|
    delete_relationship(rel)
  end
  
  # Delete node itself
  response = Typhoeus::Request.delete(node_url(id), :headers => { 'Accept' => 'application/json' })
  response.code == 204 ? true : false
end

#get_node(id) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/architect4r/core/node_methods.rb', line 16

def get_node(id)
  # Example response for a node
  # extensions: {}
  # 
  # paged_traverse: http://localhost:7475/db/data/node/0/paged/traverse/{returnType}{?pageSize,leaseTime}
  # self: http://localhost:7475/db/data/node/0
  # property: http://localhost:7475/db/data/node/0/properties/{key}
  # data: {}
  # 
  # incoming_typed_relationships: http://localhost:7475/db/data/node/0/relationships/in/{-list|&|types}
  # outgoing_typed_relationships: http://localhost:7475/db/data/node/0/relationships/out/{-list|&|types}
  # incoming_relationships: http://localhost:7475/db/data/node/0/relationships/in
  # all_relationships: http://localhost:7475/db/data/node/0/relationships/all
  # create_relationship: http://localhost:7475/db/data/node/0/relationships
  # traverse: http://localhost:7475/db/data/node/0/traverse/{returnType}
  # properties: http://localhost:7475/db/data/node/0/properties
  # all_typed_relationships: http://localhost:7475/db/data/node/0/relationships/all/{-list|&|types}
  # outgoing_relationships: http://localhost:7475/db/data/node/0/relationships/out
  
  # Handle cases where id might be a url
  
  response = Typhoeus::Request.get(node_url(id), :headers => { 'Accept' => 'application/json' })
  response.code == 200 ? JSON.parse(response.body) : nil
end

#node_id(input) ⇒ Object



69
70
71
# File 'lib/architect4r/core/node_methods.rb', line 69

def node_id(input)
  node_url(input).match(/node\/(\d+)$/i)[1].to_i
end

#rootObject



65
66
67
# File 'lib/architect4r/core/node_methods.rb', line 65

def root
  get_node(get('/')['reference_node'])
end

#update_node(id, properties) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/architect4r/core/node_methods.rb', line 41

def update_node(id, properties)
  # Handle urls
  url = id.to_i == 0 ? id : node_url(id)

  # Append the properties
  url += "/properties"

  response = Typhoeus::Request.put(url, 
    :headers => { 'Accept' => 'application/json', 'Content-Type' => 'application/json' },
    :body => properties.to_json)
  response.code == 204 ? true : false
end