Class: Fluent::TextParser::JSONTransformParser

Inherits:
Object
  • Object
show all
Includes:
Configurable
Defined in:
lib/fluent/plugin/parser_json_transform_ex.rb

Constant Summary collapse

DEFAULTS =
[ 'nothing', 'flatten' ]
DEFAULT_CLASS_NAME =
'JSONTransformer'

Instance Method Summary collapse

Instance Method Details

#call(text) ⇒ Object



31
32
33
34
# File 'lib/fluent/plugin/parser_json_transform_ex.rb', line 31

def call(text)
  raw_json = JSON.parse(text)
  return nil, @transformer.transform(raw_json)
end

#configure(conf) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/fluent/plugin/parser_json_transform_ex.rb', line 12

def configure(conf)
  @transform_script = conf['transform_script']

  if DEFAULTS.include?(@transform_script)
    @transform_script = "#{__dir__}/../../transform/#{@transform_script}.rb"
    className = DEFAULT_CLASS_NAME
  elsif @transform_script == 'custom'
    @transform_script = conf['script_path']
    className = conf['class_name'] || DEFAULT_CLASS_NAME
  end

  require @transform_script
  begin
    @transformer = Object.const_get(className).new
  rescue NameError
    @transformer = Object.const_get(DEFAULT_CLASS_NAME).new
  end
end

#parse(text) ⇒ Object



36
37
38
39
# File 'lib/fluent/plugin/parser_json_transform_ex.rb', line 36

def parse(text)
  raw_json = JSON.parse(text)
  return nil, @transformer.transform(raw_json)
end