class SmartXmlLogger
Public Class Methods
new(forward_to, method = nil)
click to toggle source
# File lib/reactor/tools/smart_xml_logger.rb, line 5 def initialize(forward_to, method = nil) @logger = forward_to @method = method end
Public Instance Methods
configure(key, options)
click to toggle source
# File lib/reactor/tools/smart_xml_logger.rb, line 10 def configure(key, options) @configuration ||= {} @configuration[key] = options end
log(text)
click to toggle source
# File lib/reactor/tools/smart_xml_logger.rb, line 15 def log(text) return unless @logger @logger.send(@method, text) end
log_xml(key, xml)
click to toggle source
# File lib/reactor/tools/smart_xml_logger.rb, line 20 def log_xml(key, xml) return unless @logger options = @configuration[key] dom = Nokogiri::XML::Document.parse(xml) node_set = options[:xpath] ? dom.xpath(options[:xpath]) : dom self.log(if node_set.respond_to?(:each) node_set.map{|node| self.print_node(node, options[:start_indent] || 0)}.join else self.print_node(node_set, options[:start_indent] || 0) end) end
print_node(node, indent = 0)
click to toggle source
private
# File lib/reactor/tools/smart_xml_logger.rb, line 38 def print_node(node, indent = 0) return '' if node.text? empty = node.children.empty? has_text = node.children.detect{|child| child.text?} out = ' ' * indent attrs = node.attributes.values.map{|attr| %|#{attr.name}="#{(attr.value)}"|}.join(' ') attrs = " #{attrs}" if attrs.present? out << "<#{(node.name)}#{attrs}#{'/' if empty}>" if has_text out << "#{(node.text)}" else out << "\n" end node.children.each do |child| out << self.print_node(child, indent + 2) end out << ' ' * indent unless has_text || empty out << "</#{(node.name)}>\n" unless empty out end