Class: Architect4r::Server

Inherits:
Object
  • Object
show all
Includes:
Core::CypherMethods, Core::NodeMethods, Core::RelationshipMethods
Defined in:
lib/architect4r/server.rb

Instance Method Summary collapse

Constructor Details

#initialize(config = nil) ⇒ Server

Returns a new instance of Server.



12
13
14
# File 'lib/architect4r/server.rb', line 12

def initialize(config=nil)
  @server_config = config
end

Instance Method Details

#configurationObject



16
17
18
# File 'lib/architect4r/server.rb', line 16

def configuration
  @configuration ||= Core::Configuration.new(@server_config)
end

#convert_if_possible(object_hash) ⇒ Object

Build a node or relationship from the hash neo4j returns.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/architect4r/server.rb', line 63

def convert_if_possible(object_hash)
  if klass = object_hash.is_a?(Hash) && object_hash['data']
    if model_string = object_hash['data']['architect4r_type']
      data = begin
        eval("#{model_string}.send(:build_from_database, object_hash)")
      rescue => ex
        data
      end
    elsif object_hash['self'].match(/node\/\d+$/i)
      data = GenericNode.send(:build_from_database, object_hash)
    end
  else
    data = object_hash
  end
  data
end

#deleteObject



37
38
39
40
# File 'lib/architect4r/server.rb', line 37

def delete
  response = Typhoeus::Request.delete(prepend_base_url(url), :params => params)
  response.success? ? JSON.parse(response.body) : nil
end

#get(url, options = {}) ⇒ Object

Basic rest actions



22
23
24
25
# File 'lib/architect4r/server.rb', line 22

def get(url, options = {})
  response = Typhoeus::Request.get(prepend_base_url(url), :headers => { 'Accept' => 'application/json' })
  response.success? ? JSON.parse(response.body) : nil
end

#node_url(url_or_id) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/architect4r/server.rb', line 42

def node_url(url_or_id)
  if url_or_id.is_a?(Hash)
    url_or_id['self'].to_s
  elsif url_or_id.to_s != '0' and url_or_id.to_i == 0
    url_or_id.to_s
  else
    prepend_base_url("/node/#{url_or_id.to_i}")
  end
end

#post(url, params = {}) ⇒ Object



27
28
29
30
# File 'lib/architect4r/server.rb', line 27

def post(url, params = {})
  response = Typhoeus::Request.post(prepend_base_url(url), :params => params)
  response.success? ? JSON.parse(response.body) : nil
end

#put(url, params = {}) ⇒ Object



32
33
34
35
# File 'lib/architect4r/server.rb', line 32

def put(url, params = {})
  response = Typhoeus::Request.put(prepend_base_url(url), :params => params)
  response.success? ? JSON.parse(response.body) : nil
end

#relationship_url(url_or_id) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/architect4r/server.rb', line 52

def relationship_url(url_or_id)
  if url_or_id.is_a?(Hash)
    url_or_id['self'].to_s
  elsif url_or_id.to_s != '0' and url_or_id.to_i == 0
    url_or_id.to_s
  else
    prepend_base_url("/relationship/#{url_or_id.to_i}")
  end
end