summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorytti <saku@ytti.fi>2018-06-13 16:42:53 +0300
committerGitHub <noreply@github.com>2018-06-13 16:42:53 +0300
commit896235cf3993b9fb446541dd673c00191e713a2f (patch)
treebfacfbf6591caffa39452715942325d572aae3f9
parenta280e5d6d44cb585575efa61030bede67203cd64 (diff)
Feature string navigation for HTTP source API JSON
Allow HTTP API to place host array in arbitrary place defined as: hosts_location: a.b[0].c Also support arrays in mapping keys: key: location[0].is.here[2]
-rw-r--r--.rubocop.yml3
-rw-r--r--lib/oxidized/source/http.rb28
-rw-r--r--spec/source/http_spec.rb32
3 files changed, 47 insertions, 16 deletions
diff --git a/.rubocop.yml b/.rubocop.yml
index 20567f4..c9feb24 100644
--- a/.rubocop.yml
+++ b/.rubocop.yml
@@ -30,6 +30,9 @@ Style/FormatStringToken:
Style/RescueModifier:
Enabled: false
+Style/MultilineBlockChain:
+ Enabled: false
+
# Do not attempt to police vendored code, and exclude special cases
AllCops:
Exclude:
diff --git a/lib/oxidized/source/http.rb b/lib/oxidized/source/http.rb
index 55dcd4c..bf1e74c 100644
--- a/lib/oxidized/source/http.rb
+++ b/lib/oxidized/source/http.rb
@@ -39,21 +39,20 @@ module Oxidized
response = http.request(request)
data = JSON.parse(response.body)
+ data = string_navigate(data, @cfg.hosts_location) if @cfg.hosts_location?
data.each do |node|
next if node.empty?
# map node parameters
keys = {}
@cfg.map.each do |key, want_position|
- want_positions = want_position.split('.')
- keys[key.to_sym] = node_var_interpolate node.dig(*want_positions)
+ keys[key.to_sym] = node_var_interpolate string_navigate(node, want_position)
end
keys[:model] = map_model keys[:model] if keys.has_key? :model
# map node specific vars
vars = {}
@cfg.vars_map.each do |key, want_position|
- want_positions = want_position.split('.')
- vars[key.to_sym] = node_var_interpolate node.dig(*want_positions)
+ vars[key.to_sym] = node_var_interpolate string_navigate(node, want_position)
end
keys[:vars] = vars unless vars.empty?
@@ -61,20 +60,17 @@ module Oxidized
end
nodes
end
- 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 # foo.bar.baz (bar exist but is not hash)
- return nil
+ private
+
+ def string_navigate object, wants
+ wants.split(".").map do |want|
+ head, match, _tail = want.partition(/\[\d+\]/)
+ match.empty? ? head : [head, match[1..-2].to_i]
+ end.flatten.each do |want|
+ object = object[want] if object.respond_to? :each
end
+ object
end
end
end
diff --git a/spec/source/http_spec.rb b/spec/source/http_spec.rb
new file mode 100644
index 0000000..d3df551
--- /dev/null
+++ b/spec/source/http_spec.rb
@@ -0,0 +1,32 @@
+require 'spec_helper'
+require 'oxidized/source/http'
+
+describe Oxidized::HTTP do
+ before(:each) do
+ Oxidized.asetus = Asetus.new
+ Oxidized.setup_logger
+ end
+
+ describe "#string_navigate" do
+ h1 = {}
+ h1["inventory"] = [{ "ip" => "10.10.10.10" }]
+ h1["jotain"] = { "2" => "jotain" }
+ it "should be able to navigate multilevel-hash" do
+ http = Oxidized::HTTP.new
+ http.class.must_equal Oxidized::HTTP
+ http.send(:string_navigate, h1, "jotain.2").must_equal "jotain"
+ end
+ it "should be able to navigate multilevel-hash" do
+ Oxidized::HTTP.new.send(:string_navigate, h1, "jotain.2").must_equal "jotain"
+ end
+ it "should be able to navigate hash/array combination" do
+ Oxidized::HTTP.new.send(:string_navigate, h1, "inventory[0].ip").must_equal "10.10.10.10"
+ end
+ it "should return nil on non-existing string key" do
+ Oxidized::HTTP.new.send(:string_navigate, h1, "jotain.3").must_equal nil
+ end
+ it "should return nil on non-existing array index" do
+ Oxidized::HTTP.new.send(:string_navigate, h1, "inventory[3]").must_equal nil
+ end
+ end
+end