class Fluent::XmlFilter

Public Instance Methods

configure(conf) click to toggle source
Calls superclass method
# File lib/fluent/plugin/filter_xml_simple.rb, line 13
def configure(conf)
  super

  raise ConfigError, "'Fields' is required" if self.fields.nil?

  self.fields = self.fields.split(',')

  raise ConfigError, "'Fields' must contain at least one key" if self.fields.length < 1
end
filter(tag, time, record) click to toggle source
# File lib/fluent/plugin/filter_xml_simple.rb, line 35
def filter(tag, time, record)
  self.fields.each { |field|
    if record.key?(field)
      field_name = field

      if self.field_name_postfix
        field_name = [field, self.field_name_postfix].join '_'
      end

      hash = @parser.parse(record[field])
      hash = self.try_convert_times ? convert_times(hash) : hash

      $log.debug "Hash from XML: #{hash.to_s}"

      record[field_name] = hash
    end
  }

  record
end
shutdown() click to toggle source
Calls superclass method
# File lib/fluent/plugin/filter_xml_simple.rb, line 29
def shutdown
  super

  @parser = nil
end
start() click to toggle source
Calls superclass method
# File lib/fluent/plugin/filter_xml_simple.rb, line 23
def start
  super

  @parser = Nori.new(:advanced_typecasting => false)
end

Private Instance Methods

convert_times(hash) click to toggle source
# File lib/fluent/plugin/filter_xml_simple.rb, line 70
def convert_times(hash)
  hash.map { |key, value|
    [key, convert_to_time(value)]
  }.to_h
end
convert_times_array(array) click to toggle source
# File lib/fluent/plugin/filter_xml_simple.rb, line 76
def convert_times_array(array)
  array.map { |value|
    convert_to_time(value)
  }
end
convert_to_time(value) click to toggle source
# File lib/fluent/plugin/filter_xml_simple.rb, line 58
def convert_to_time(value)
  if value.class == Hash
    convert_times(value)
  elsif value.class == Array
    convert_times_array(value)
  else
    try_to_convert(value) { |x|
      (self.time_format ? Time.strptime(x, self.time_format) : Time.parse(x)).to_i
    }
  end
end
try_to_convert(value, &block) click to toggle source
# File lib/fluent/plugin/filter_xml_simple.rb, line 82
def try_to_convert(value, &block)
  block.call(value)
rescue Exception => e
  $log.debug "Cannot convert time: #{e.message}\nTrace: #{e.backtrace.to_s}"

  value
end