Module: ZkClient

Defined in:
lib/zk_client.rb,
lib/zk_client/version.rb,
lib/zk_client/zk_client.rb

Constant Summary collapse

VERSION =
"0.5.3"
ROOT =
'/'

Class Method Summary collapse

Class Method Details

.children(path) ⇒ Object



44
45
46
47
# File 'lib/zk_client/zk_client.rb', line 44

def children(path)
  key = process_path(path)
  client.get_children(path: key)[:children]
end

.clientObject



11
12
13
# File 'lib/zk_client/zk_client.rb', line 11

def client
  @@client ||= create_client
end

.closeObject



49
50
51
# File 'lib/zk_client/zk_client.rb', line 49

def close
  client.close
end

.configObject



57
58
59
60
61
# File 'lib/zk_client/zk_client.rb', line 57

def config
  if block_given?
    yield(self)
  end
end

.create(path, data) ⇒ Object



34
35
36
37
# File 'lib/zk_client/zk_client.rb', line 34

def create(path, data)
  key = process_path(path)
  resp = client.create(path: key, data: data)
end

.delete(path) ⇒ Object



39
40
41
42
# File 'lib/zk_client/zk_client.rb', line 39

def delete(path)
  key = process_path(path)
  client.delete(path: key)
end

.hostObject



71
72
73
# File 'lib/zk_client/zk_client.rb', line 71

def host
  @@host ||= "localhost"
end

.host=(host) ⇒ Object



75
76
77
# File 'lib/zk_client/zk_client.rb', line 75

def host=(host)
  @@host = host
end

.portObject



79
80
81
# File 'lib/zk_client/zk_client.rb', line 79

def port
  @@port ||= 2181
end

.port=(port) ⇒ Object



83
84
85
# File 'lib/zk_client/zk_client.rb', line 83

def port=(port)
  @@port = port.to_i
end

.read(path) ⇒ Object



15
16
17
# File 'lib/zk_client/zk_client.rb', line 15

def read(path)
  read_node(path)[:data]
end

.read_node(path) ⇒ Object



19
20
21
22
# File 'lib/zk_client/zk_client.rb', line 19

def read_node(path)
  key = process_path(path)
  client.get(path: key)
end

.reopenObject



53
54
55
# File 'lib/zk_client/zk_client.rb', line 53

def reopen
  @@client = create_client
end

.rootObject

Backward compatibility



100
101
102
# File 'lib/zk_client/zk_client.rb', line 100

def root
  root_path
end

.root_pathObject



63
64
65
# File 'lib/zk_client/zk_client.rb', line 63

def root_path
  @@root_path ||= ROOT
end

.root_path=(root) ⇒ Object



67
68
69
# File 'lib/zk_client/zk_client.rb', line 67

def root_path=(root)
  @@root_path = root
end

.uriObject



87
88
89
# File 'lib/zk_client/zk_client.rb', line 87

def uri
  "#{host}:#{port}"
end

.uri=(uri) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/zk_client/zk_client.rb', line 91

def uri=(uri)
  parsed_uri = URI(schemeify(uri))

  @@host      = parsed_uri.host
  @@port      = parsed_uri.port
  @@root_path = parsed_uri.path
end

.write(path, data) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/zk_client/zk_client.rb', line 24

def write(path, data)
  key = process_path(path)
  response = client.set(path: key, data: data)
  if !response[:stat].exists
    create(key, data)
  else
    response
  end
end