Class: NeoRest::Node

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/NeoRest/node.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = nil) ⇒ Node

Returns a new instance of Node.



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/NeoRest/node.rb', line 12

def initialize( hash = nil )
  @table = {}
  if hash
    @neo_id = hash["self"].split('/').last
    @neo_url = hash["self"]

    for key, value in hash["data"]
      @table[key.to_sym] = value
      new_ostruct_member(key)
    end
  end
end

Instance Attribute Details

#neo_idObject (readonly)

Returns the value of attribute neo_id.



10
11
12
# File 'lib/NeoRest/node.rb', line 10

def neo_id
  @neo_id
end

#neo_urlObject (readonly)

Returns the value of attribute neo_url.



10
11
12
# File 'lib/NeoRest/node.rb', line 10

def neo_url
  @neo_url
end

Class Method Details

.create_new(node_data) ⇒ Object



66
67
68
# File 'lib/NeoRest/node.rb', line 66

def create_new node_data
  NeoRest::Node.new( JSON.parse( RestClient.post( "#{$neo_base_url}/node", node_data.to_json, :content_type => :json, :accept => :json ) ) )
end

.delete(node) ⇒ Object



83
84
85
86
# File 'lib/NeoRest/node.rb', line 83

def delete node
  node = node.neo_id if node.respond_to?('neo_id')
  RestClient.delete( "#{$neo_base_url}/node/#{node}" )
end

.load(node_id) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/NeoRest/node.rb', line 70

def load node_id
  RestClient.get( "#{$neo_base_url}/node/#{node_id}", :accept => :json ){ |response, request, result, &block|
    case response.code
      when 200
        NeoRest::Node.new( JSON.parse( response ) )
      when 404
        return nil
      else
        response.return!(request, result, &block)
    end
  }
end

Instance Method Details

#add_relationship_from(node, type, meta_data = {}) ⇒ Object



55
56
57
# File 'lib/NeoRest/node.rb', line 55

def add_relationship_from node, type,  = {}
  NeoRest::Relationship.new( JSON.parse( RestClient.post( "#{$neo_base_url}/node/#{node.neo_id}/relationships", { :to => "#{$base}/node/#{neo_id}", :type => type, :data =>  }.to_json, :content_type => :json, :accept => :json ) ) )
end

#add_relationship_to(node, type, meta_data = {}) ⇒ Object



51
52
53
# File 'lib/NeoRest/node.rb', line 51

def add_relationship_to node, type,  = {}
  NeoRest::Relationship.new( JSON.parse( RestClient.post( "#{$neo_base_url}/node/#{neo_id}/relationships", { :to => "#{$base}/node/#{node.neo_id}", :type => type, :data =>  }.to_json, :content_type => :json, :accept => :json ) ) )
end

#add_to_index(index_name, key, value) ⇒ Object



59
60
61
62
# File 'lib/NeoRest/node.rb', line 59

def add_to_index index_name, key, value
  RestClient.post( "#{$neo_base_url}/index/node/#{index_name}/#{key}/#{URI.escape(value)}", ("#{$base}/node/#{neo_id}").to_json, :content_type => :json, :accept => :json )
  NeoRest::Index.new index_name, key, value
end

#deleteObject



39
40
41
# File 'lib/NeoRest/node.rb', line 39

def delete
  NeoRest::Node.delete neo_id
end

#get_relationships(direction = :all) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/NeoRest/node.rb', line 43

def get_relationships direction = :all
  relationsships_json = JSON.parse( RestClient.get( "#{$neo_base_url}/node/#{neo_id}/relationships/#{direction}", :accept => :json ) )
  relationsships = []
  relationsships_json.each { | relationship | relationsships << NeoRest::Relationship.new( relationship ) }
  return relationsships unless block_given?
  relationsships.each { | relationship | yield relationship }
end

#update(properties = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/NeoRest/node.rb', line 25

def update properties = {}
  if not properties.empty?
    @table.each { | key, value | delete_field( key ) }
    @table.clear
    
    for key, value in properties
      @table[key.to_sym] = value
      new_ostruct_member(key)
    end
  end
        
  RestClient.put( "#{$neo_base_url}/node/#{neo_id}/properties", @table.to_json, :content_type => :json, :accept => :json )
end