Sha256: 093996bd14109e23112c800a15abf3858495d4623a89f7bc7ced2c0f74bcf2f8

Contents?: true

Size: 1.08 KB

Versions: 1

Compression:

Stored size: 1.08 KB

Contents

require File.join(File.dirname(__FILE__), "service")

module Charrington
  class Transform
    include Service
    attr_accessor :event
    attr_reader :top_level_keys

    Error = Class.new(StandardError)
    EventNil = Class.new(Error)
    TableNameNil = Class.new(Error)

    KEY_BLACKLIST = ['host','path','jwt']

    def initialize(event)
      raise EventNil, "Event is nil" if event.nil?
      event = event.is_a?(Hash) ? event : event.to_hash
      @event = drop_keys(event)
      @top_level_keys = @event.keys
    end

    def call
      flattened = flatten_hash(event)
      top_level_keys.each { |k| event.delete(k) }
      flattened.each_pair { |key, val| event[key] = val }
      event
    end

    private

    def drop_keys(event)
      event.delete_if {|k, _v| k.start_with?("@") || KEY_BLACKLIST.include?(k) }
    end

    def flatten_hash(hash)
      hash.each_with_object({}) do |(k, v), acc|
        if v.is_a? Hash
          flatten_hash(v).map do |h_k, h_v|
            acc["#{k}_#{h_k}"] = h_v
          end
        else
          acc[k] = v
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
logstash-output-charrington-0.2.0 lib/logstash/outputs/charrington/transform.rb