class Consul::Client::OfflineConnection
Public Instance Methods
delete(keypath, opts={})
click to toggle source
# File lib/consul/client/offline_connection.rb, line 18 def delete(keypath, opts={}) #do nothing, lets not delete our local files true end
read_obj(keypath, opts={})
click to toggle source
# File lib/consul/client/offline_connection.rb, line 7 def read_obj(keypath, opts={}) keyname = File.basename(keypath) response = http_get(keypath, opts) if response.length == 1 && response.keys.first == keyname response[keyname] else response end end
write(keypath,data=nil)
click to toggle source
# File lib/consul/client/offline_connection.rb, line 23 def write(keypath,data=nil) data = encode(data) filename = File.join(local_path, path(keypath) + ".yaml") dirname = File.dirname(filename) unless File.directory?(dirname) FileUtils.mkdir_p(dirname) end File.open(filename, 'w') { |f| f.write(data) } Consul::Response.new(Consul::Client::FakeNetHttpResponse.new) end
Private Instance Methods
files(keypath, opts)
click to toggle source
# File lib/consul/client/offline_connection.rb, line 37 def files(keypath, opts) if opts[:recursive] recursive_files = Dir.glob(File.join(local_path, path(keypath) + '*', '**', '*.yaml')) path_files = Dir.glob(File.join(local_path, path(keypath) + '*.yaml')) recursive_files | path_files else [File.join(local_path, path(keypath) + ".yaml")] end end
http_get(keypath, opts)
click to toggle source
# File lib/consul/client/offline_connection.rb, line 48 def http_get(keypath, opts) key_data = files(keypath, opts).map do |filename| {}.tap do |data| data['LockIndex'] = 0 data['Key'] = filename.sub(/^#{File.dirname(File.join(local_path, path(keypath)))}/, '').chomp('.yaml') data['Flags'] = 0 data['Value'] = Base64.encode64(File.read(filename)) data['CreateIndex'] = 0 data['ModifyIndex'] = 0 end end Consul::Response.new(Consul::Client::FakeNetHttpResponse.new(Psych.dump(key_data))) end
local_path()
click to toggle source
# File lib/consul/client/offline_connection.rb, line 63 def local_path ENV['CONSUL_CLIENT_YAML_PATH'] || "/tmp/consul_client" end