Module: Architect4r::Core::RelationshipMethods

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

Instance Method Summary collapse

Instance Method Details

#create_relationship(start_node, end_node, type, properties = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/architect4r/core/relationship_methods.rb', line 6

def create_relationship(start_node, end_node, type, properties={})
  # POST http://localhost:7474/db/data/node/3/relationships
  # {"to" : "http://localhost:7474/db/data/node/4", "type" : "LOVES"}
  # 201: Created
  # Location: http://localhost:7474/db/data/relationship/2
  
  # Add the destination node and type to the properties
  properties = { 'to' => node_url(end_node), 'type' => type.to_s, 'data' => properties }
  
  # Send request
  response = Typhoeus::Request.post(node_url(start_node) + "/relationships", 
    :headers => { 'Accept' => 'application/json', 'Content-Type' => 'application/json' },
    :body => properties.to_json)
  
  # Evaluate response
  response.code == 201 ? JSON.parse(response.body) : nil
end

#delete_relationship(relationship) ⇒ Object



29
30
31
32
# File 'lib/architect4r/core/relationship_methods.rb', line 29

def delete_relationship(relationship)
  response = Typhoeus::Request.delete(relationship_url(relationship), :headers => { 'Accept' => 'application/json' })
  response.code == 204 ? true : false
end

#get_node_relationships(node, direction = :all, *types) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/architect4r/core/relationship_methods.rb', line 34

def get_node_relationships(node, direction = :all, *types)
  # GET http://localhost:7474/db/data/node/6/relationships/all
  # GET http://localhost:7474/db/data/node/11/relationships/in
  # GET http://localhost:7474/db/data/node/16/relationships/out
  # GET http://localhost:7474/db/data/node/21/relationships/all/LIKES&HATES
  # 200: OK
  
  # Set direction
  direction = case direction.to_s
    when 'incoming'
      'in'
    when 'outgoing'
      'out'
    else
      'all'
  end
  
  # Create typ filter
  types = types.map { |e| URI.escape(e) }.join('&')
  
  # Send request
  url = node_url(node) + "/relationships/#{direction}/#{types}"
  response = Typhoeus::Request.get(url, :headers => { 'Accept' => 'application/json' })
  response.code == 200 ? JSON.parse(response.body) : []
end

#get_relationship(relationship) ⇒ Object



24
25
26
27
# File 'lib/architect4r/core/relationship_methods.rb', line 24

def get_relationship(relationship)
  response = Typhoeus::Request.get(relationship_url(relationship), :headers => { 'Accept' => 'application/json' })
  response.code == 200 ? JSON.parse(response.body) : nil
end

#get_relationship_typesObject



70
71
72
73
74
75
# File 'lib/architect4r/core/relationship_methods.rb', line 70

def get_relationship_types
  # GET http://localhost:7474/db/data/relationship/types
  # 200: OK
  response = Typhoeus::Request.get(prepend_base_url('/relationship/types'), :headers => { 'Accept' => 'application/json' })
  response.code == 200 ? JSON.parse(response.body) : []
end

#update_relationship(id, properties) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/architect4r/core/relationship_methods.rb', line 60

def update_relationship(id, properties)
  # PUT http://localhost:7474/db/data/relationship/0/properties
  # 204: No Content
  url = relationship_url(id) + '/properties'
  response = Typhoeus::Request.put(url, 
    :headers => { 'Accept' => 'application/json', 'Content-Type' => 'application/json' },
    :body => properties.to_json)
  response.code == 204 ? true : false
end