Module: Architect4r::Core::RelationshipMethods::InstanceMethods

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

Instance Method Summary collapse

Instance Method Details

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



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

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



31
32
33
34
# File 'lib/architect4r/core/relationship_methods.rb', line 31

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



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

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



26
27
28
29
# File 'lib/architect4r/core/relationship_methods.rb', line 26

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



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

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



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

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