offline_consul-client 0.1.2 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/consul/client/version.rb +1 -1
- data/lib/consul/client.rb +34 -20
- data/lib/consul/data.rb +12 -3
- data/lib/consul/response.rb +3 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7a224f7ff4d5849b3da62357983eff37d340dd34
|
4
|
+
data.tar.gz: e81df1790630c58fbe9351895be6e3e01f66c2b9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 76b3156624259716fd1f1716fbc519b3470f4ea62a66b45c18ae77dededac9af5a7ce10374d0145882db441ed70de977e4ce4883551cfe18ce118265f1198a9a
|
7
|
+
data.tar.gz: b06c3496e34238d6aa3ddd024285c9fc4ef454396638e94880627129943862318ef995ae0268eb412e7f2d474d2187c6889979e829b61c68038f948cac8a1592
|
data/lib/consul/client.rb
CHANGED
@@ -14,38 +14,52 @@ module Consul
|
|
14
14
|
@@config ||= Config.new
|
15
15
|
end
|
16
16
|
|
17
|
-
def self.online_read(keypath,
|
18
|
-
response = http.get("/kv" + path(keypath) + (
|
19
|
-
keyname = File.basename(keypath)
|
20
|
-
if response.length == 1 && response.keys.first == keyname
|
21
|
-
response = response.data[keyname]
|
22
|
-
end
|
23
|
-
decode_r(response)
|
17
|
+
def self.online_read(keypath, opts={})
|
18
|
+
response = http.get("/kv" + path(keypath) + (opts[:recursive] ? '?recurse' : ''))
|
24
19
|
end
|
25
20
|
|
26
|
-
def self.offline_read(keypath,
|
27
|
-
files = if
|
21
|
+
def self.offline_read(keypath, opts={})
|
22
|
+
files = if opts[:recursive]
|
28
23
|
Dir.glob(File.join(local_path, path(keypath) + '*'))
|
29
24
|
else
|
30
25
|
[File.join(local_path, path(keypath) + ".yaml")]
|
31
26
|
end
|
32
27
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
28
|
+
key_data = files.map do |filename|
|
29
|
+
{}.tap do |data|
|
30
|
+
data['LockIndex'] = 0
|
31
|
+
data['Key'] = File.basename(filename).sub(/\.yaml$/, '')
|
32
|
+
data['Flags'] = 0
|
33
|
+
data['Value'] = Base64.encode64(File.read(filename))
|
34
|
+
data['CreateIndex'] = 0
|
35
|
+
data['ModifyIndex'] = 0
|
36
|
+
end
|
39
37
|
end
|
40
|
-
|
38
|
+
|
39
|
+
response = Response.new(FakeNetHttpResponse.new(Psych.dump(key_data)))
|
41
40
|
end
|
42
41
|
|
43
|
-
def self.read(keypath,
|
44
|
-
|
45
|
-
|
42
|
+
def self.read(keypath, opts={})
|
43
|
+
obj = read_obj(keypath, opts)
|
44
|
+
if obj.respond_to?(:value)
|
45
|
+
obj.value
|
46
46
|
else
|
47
|
-
|
47
|
+
Hash[obj.map{|k,v| [k, v.value] }]
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def self.read_obj(keypath, opts={})
|
52
|
+
response = if is_online?
|
53
|
+
online_read(keypath, opts)
|
54
|
+
else
|
55
|
+
offline_read(keypath, opts)
|
56
|
+
end
|
57
|
+
|
58
|
+
keyname = File.basename(keypath)
|
59
|
+
if response.length == 1 && response.keys.first == keyname
|
60
|
+
response = response[keyname]
|
48
61
|
end
|
62
|
+
response
|
49
63
|
end
|
50
64
|
|
51
65
|
def self.online_write(keypath,data=nil,path_mimic=false)
|
data/lib/consul/data.rb
CHANGED
@@ -1,22 +1,31 @@
|
|
1
1
|
require 'base64'
|
2
|
+
require 'forwardable'
|
2
3
|
|
3
4
|
module Consul
|
4
5
|
class Data
|
6
|
+
extend Forwardable
|
7
|
+
|
8
|
+
def_delegators :value, :to_a, :to_h, :to_i, :to_f, :to_s
|
5
9
|
|
6
10
|
def initialize(response_hash)
|
7
11
|
@response = response_hash
|
8
12
|
end
|
9
13
|
|
10
14
|
def value
|
11
|
-
Base64.decode64(hash_map('Value'))
|
15
|
+
text = Base64.decode64(hash_map('Value'))
|
16
|
+
begin
|
17
|
+
decoded = Consul::Client.decode(text)
|
18
|
+
rescue Psych::DisallowedClass, Psych::SyntaxError
|
19
|
+
end
|
20
|
+
decoded || text
|
12
21
|
end
|
13
22
|
|
14
23
|
def flags
|
15
24
|
hash_map(__callee__)
|
16
25
|
end
|
17
|
-
[:lock_index, :create_index, :modify_index, :session].each{ |a| alias_method a, :flags }
|
26
|
+
[:key, :lock_index, :create_index, :modify_index, :session].each{ |a| alias_method a, :flags }
|
18
27
|
|
19
|
-
private
|
28
|
+
private
|
20
29
|
def hash_map(key)
|
21
30
|
@response[key.to_s.split('_').map{ |w| w.capitalize }.join]
|
22
31
|
end
|
data/lib/consul/response.rb
CHANGED
@@ -9,7 +9,8 @@ module Consul
|
|
9
9
|
|
10
10
|
attr_reader :response, :data
|
11
11
|
def_delegators :response, :body, :code, :code_type
|
12
|
-
def_delegators :data, :keys, :values, :each, :to_a, :length
|
12
|
+
def_delegators :data, :keys, :values, :each, :to_a, :length, :map, :[]
|
13
|
+
def_delegators :to_ruby, :to_h, :to_a
|
13
14
|
|
14
15
|
class KeyError < RuntimeError; end
|
15
16
|
|
@@ -47,7 +48,7 @@ module Consul
|
|
47
48
|
elsif response.code_type == Net::HTTPNotFound
|
48
49
|
raise KeyError, "Requested key not found"
|
49
50
|
else
|
50
|
-
raise "
|
51
|
+
raise "Request failed"
|
51
52
|
end
|
52
53
|
end
|
53
54
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: offline_consul-client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jason Scholl
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-07-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|