module XMLScrubber

Constants

DEFAULT_REPLACEMENT
VERSION

Public Class Methods

call(xml, *directives) click to toggle source
# File lib/xml_scrubber.rb, line 8
def self.call(xml, *directives)
  tree = Nokogiri.XML(xml)
  tree.traverse do |node|
    Array(directives).flatten.each do |directive|
      node.content = DEFAULT_REPLACEMENT if applies_to_node?(directive, node)
    end
  end
  tree.to_s
end

Private Class Methods

applies_to_node?(directive, node) click to toggle source
# File lib/xml_scrubber.rb, line 20
def self.applies_to_node?(directive, node)
  directive_name, directive_opts = directive.to_a.flatten
  if directive_name == :name
    namespace_name = node.respond_to?(:namespace) ?
      node.namespace&.prefix : nil
    node_name = [namespace_name, node.name].compact.join(":")
    if node_name.match(directive_opts[:matches])
      node.content = DEFAULT_REPLACEMENT
    end
  else
    fail ArgumentError, "unknown directive `#{directive_name}`"
  end
  node_name.match(directive_opts[:matches])
end