class Consul::Client::Connection
Public Instance Methods
config()
click to toggle source
# File lib/consul/client/connection.rb, line 7 def config @config ||= Config.new end
decode(value)
click to toggle source
# File lib/consul/client/connection.rb, line 75 def decode(value) Psych.safe_load(value, [Symbol]) end
decode_r(data)
click to toggle source
# File lib/consul/client/connection.rb, line 67 def decode_r(data) if data.respond_to?(:keys) Hash[ data.map{ |k,v| [k,decode_r(v)] } ] else decode(data.value) end end
delete(keypath, opts={})
click to toggle source
# File lib/consul/client/connection.rb, line 34 def delete(keypath, opts={}) http.delete("/kv" + path(keypath) + (opts[:recursive] ? '?recurse' : '')).value end
encode(value)
click to toggle source
# File lib/consul/client/connection.rb, line 79 def encode(value) encoded = Psych.dump(value) if decode(encoded) != value raise TypeError, "Unable to relaibly decode #{value.class}" end encoded end
encode_r(data)
click to toggle source
# File lib/consul/client/connection.rb, line 87 def encode_r(data) if data.respond_to?(:keys) Hash[ data.map{ |k,v| [k, encode_r(v)] } ] else encode(data) end end
http()
click to toggle source
def has_lock?(keypath)
read("/kv/" + keypath).session == session_id
end
# File lib/consul/client/connection.rb, line 59 def http @http ||= Consul::Http.new end
path(keypath)
click to toggle source
# File lib/consul/client/connection.rb, line 63 def path(keypath) File.join("/" + config.namespace, keypath).gsub(%r{//}, '/') end
read(keypath, opts={})
click to toggle source
# File lib/consul/client/connection.rb, line 21 def read(keypath, opts={}) response = read_obj(keypath, opts) read_value_r(response) end
read_obj(keypath, opts={})
click to toggle source
# File lib/consul/client/connection.rb, line 11 def read_obj(keypath, opts={}) keyname = File.basename(keypath) response = http.get("/kv" + path(keypath) + (opts[:recursive] ? '?recurse' : '')) if response.length == 1 && response.keys.first == keyname response[keyname] else response end end
read_value_r(response)
click to toggle source
# File lib/consul/client/connection.rb, line 26 def read_value_r(response) if response.respond_to?(:keys) Hash[ response.map{ |k,v| [k,read_value_r(v)] } ] else response.value end end
write(keypath,data=nil,path_mimic=false)
click to toggle source
# File lib/consul/client/connection.rb, line 38 def write(keypath,data=nil,path_mimic=false) data = encode(data) http.put("/kv" + path(keypath), data) end