lib/hieracles/formats/json.rb in hieracles-0.3.6 vs lib/hieracles/formats/json.rb in hieracles-0.4.0
- old
+ new
@@ -3,15 +3,17 @@
module Hieracles
module Formats
# format intended to be used for an api server
class Json < Hieracles::Format
- def info(_)
+ def info(args)
+ filter(@node.info, args)
@node.info.merge(alerts).to_json
end
- def facts(_)
+ def facts(args)
+ filter(@node.facts, args)
@node.facts.merge(alerts).to_json
end
def files(_)
{ 'files' => @node.files }.merge(alerts).to_json
@@ -32,14 +34,19 @@
def allparams(args)
@node.params(false).merge(alerts).to_json
end
def build_list(hash, notifications, filter)
- {
- 'notifications' => notifications,
- 'payload' => hash
- }.to_json
+ if filter[0]
+ { 'notifications' => notifications,
+ 'payload' => hash.select { |k, e| Regexp.new(filter[0]).match k }
+ }.to_json
+ else
+ { 'notifications' => notifications,
+ 'payload' => hash
+ }.to_json
+ end
end
private
def alerts
@@ -47,8 +54,17 @@
{ 'alerts' => @node.notifications.map(&:to_hash) }
else
{}
end
end
+
+ def filter(what, args)
+ if args and args[0]
+ what.select! { |k, v| Regexp.new(args[0]).match(k.to_s) }
+ else
+ what
+ end
+ end
+
end
end
end