Module: Architect4r::Core::NodeMethods::InstanceMethods

Defined in:
lib/architect4r/core/node_methods.rb

Instance Method Summary collapse

Instance Method Details

#create_node(properties) ⇒ Object



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

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



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

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



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

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



71
72
73
# File 'lib/architect4r/core/node_methods.rb', line 71

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

#rootObject



67
68
69
# File 'lib/architect4r/core/node_methods.rb', line 67

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

#update_node(id, properties) ⇒ Object



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

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