diff options
author | Saku Ytti <saku@ytti.fi> | 2017-02-13 00:08:48 +0200 |
---|---|---|
committer | Saku Ytti <saku@ytti.fi> | 2017-02-13 00:08:48 +0200 |
commit | ddbfedc97c5add661286d0f12c4ac98275dbeb82 (patch) | |
tree | 1723ba95fdce9b34ce83a4dd78d0337b695c08e1 | |
parent | a64ab46eb51661a7746f57c7dea38c923c1213aa (diff) |
add support for hierarchical keys in json
requestd in issue #695
-rw-r--r-- | lib/oxidized/source/http.rb | 29 |
1 files changed, 23 insertions, 6 deletions
diff --git a/lib/oxidized/source/http.rb b/lib/oxidized/source/http.rb index 4fd388b..48ee8a4 100644 --- a/lib/oxidized/source/http.rb +++ b/lib/oxidized/source/http.rb @@ -35,19 +35,21 @@ class HTTP < Source response = http.request(request) data = JSON.parse(response.body) - data.each do |line| - next if line.empty? + data.each do |node| + next if node.empty? # map node parameters keys = {} - @cfg.map.each do |key, position| - keys[key.to_sym] = node_var_interpolate line[position] + @cfg.map.each do |key, want_position| + want_positions = want_position.split('.') + keys[key.to_sym] = node_var_interpolate node.dig(*want_positions) end keys[:model] = map_model keys[:model] if keys.key? :model # map node specific vars vars = {} - @cfg.vars_map.each do |key, position| - vars[key.to_sym] = node_var_interpolate line[position] + @cfg.vars_map.each do |key, want_position| + want_positions = want_position.split('.') + vars[key.to_sym] = node_var_interpolate node.dig(*want_positions) end keys[:vars] = vars unless vars.empty? @@ -58,3 +60,18 @@ class HTTP < Source end end + +if RUBY_VERSION < '2.3' + class Hash + def dig(key, *rest) + value = self[key] + if value.nil? || rest.empty? + value + elsif value.respond_to?(:dig) + value.dig(*rest) + else + fail TypeError, "#{value.class} does not have #dig method" + end + end + end +end |