imperium 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 70b5342210c6ee5d4e7876d0899b224b36092bbb
4
- data.tar.gz: 660c0bba85f85516ead3f5c8b00fe3ad7bb437fd
3
+ metadata.gz: 2dda117c132b075ad9351a9940055f2f53d29d36
4
+ data.tar.gz: a8ef9b691469f852dd0bbd67bbd0f3f654351565
5
5
  SHA512:
6
- metadata.gz: 699233998e7e26e9036f2362b4a5ca6eff394e258935966609dde0215b6283dada530510c4ca537ca1a3d474c3739234e69133b7117095b3eb5722edbb8eef7a
7
- data.tar.gz: f52e244277ad058e270ab111ebd287a6959ff9c2a65168d4bf399ebfee05c0ef0d93df5f26950400ac85acd985431b544a845d8a8b5cbd4af076097e3c2e0178
6
+ metadata.gz: fbb5a5c2fcd8b83edfa988b6b76d8e242bb95a056633a528685e6e00762ee376d42373196741fdd731210953e9a39f0d86729847093e9b27ff8bcbe3c934d66a
7
+ data.tar.gz: 8ca0ecaa2c8afbb24d02589d9667a0a97066a07a825a56816d3668d71bc2c88e9a17c854ec1a0b7ba21eebbcca1312039329d00aa05ac033ff3fc269dd23ce1d
@@ -38,10 +38,14 @@ module Imperium
38
38
  end
39
39
 
40
40
  MERGING_FUNC = -> (_, old, new) {
41
- if old.is_a?(Hash) && new.is_a?(Hash)
41
+ old_is_hash = old.is_a?(Hash)
42
+ new_is_hash = new.is_a?(Hash)
43
+ if old_is_hash && new_is_hash
42
44
  old.merge(new, &MERGING_FUNC)
45
+ elsif new_is_hash
46
+ new.merge(nil => old)
43
47
  else
44
- new
48
+ old.merge(nil => new)
45
49
  end
46
50
  }
47
51
  private_constant :MERGING_FUNC
@@ -49,6 +53,10 @@ module Imperium
49
53
  # Extracts the values from the response and smashes them into a simple
50
54
  # object depending on options provided on the request.
51
55
  #
56
+ # If a value exists exactly at the prefix or exactly at a nested prefix
57
+ # with additional nested values the hash representing the nested values
58
+ # will have a `nil` key added to contain the un-nested value.
59
+ #
52
60
  # @example A nested hash constructed from recursively found values
53
61
  # # Given a response including the following values (metadata ommitted for clarity):
54
62
  # # [
@@ -58,6 +66,18 @@ module Imperium
58
66
  # response = Imperium::KV.get('foo/bar/baz', :recurse)
59
67
  # response.values # => {'first' => 'qux', 'second' => {'deep' => 'purple'}}
60
68
  #
69
+ # @example A nested hash constructed from recusively found values and repeated keys
70
+ # # Given a response including the following values (metadata ommitted for clarity):
71
+ # # [
72
+ # # {"Key" => "foo/bar", "Value" => "b25l\n"},
73
+ # # {"Key" => "foo/bar/baz", "Value" => "dHdv\n"}
74
+ # # {"Key" => "foo/bar/baz/qux", "Value" => "dGhyZWU=\n"}
75
+ # # ]
76
+ # response = Imperium::KV.get('foo/bar', :recurse)
77
+ # response.values # => {
78
+ # nil => 'one',
79
+ # 'baz' => {nil => 'two', qux' => 'three'},
80
+ # }
61
81
  # @return [String] When the matching key is found without the `recurse`
62
82
  # option as well as when a single value is found with the recurse option
63
83
  # and the key exactly matches the prefix.
@@ -74,12 +94,7 @@ module Imperium
74
94
  found_objects.first.value
75
95
  else
76
96
  found_objects.inject({}) do |hash, obj|
77
- if prefix.empty?
78
- unprefixed_key = obj.key
79
- else
80
- unprefixed_key = obj.key[prefix.length + 1..-1]
81
- end
82
- key_parts = unprefixed_key.split('/')
97
+ key_parts = extract_key_parts(obj.key)
83
98
  hash.merge(construct_nested_hash(key_parts, obj.value), &MERGING_FUNC)
84
99
  end
85
100
  end
@@ -98,5 +113,19 @@ module Imperium
98
113
  {key => construct_nested_hash(key_parts, value)}
99
114
  end
100
115
  end
116
+
117
+ def extract_key_parts(key)
118
+ unprefixed_key = remove_prefix(key)
119
+ (unprefixed_key.nil? ? [nil] : unprefixed_key.split('/'))
120
+ end
121
+
122
+ def remove_prefix(key)
123
+ prefix.empty? ? key : key[(prefix.length + 1)..-1]
124
+ if prefix.empty?
125
+ unprefixed_key = key
126
+ else
127
+ unprefixed_key = key[prefix.length + 1..-1]
128
+ end
129
+ end
101
130
  end
102
131
  end
@@ -1,3 +1,3 @@
1
1
  module Imperium
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: imperium
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tyler Pickett
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-09-15 00:00:00.000000000 Z
11
+ date: 2017-09-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: addressable