Class: RegexpPreview::SingleLine

Inherits:
Object
  • Object
show all
Defined in:
lib/regexp_preview/single_line.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, format, params = {}) ⇒ SingleLine

Returns a new instance of SingleLine.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/regexp_preview/single_line.rb', line 5

def initialize(file, format, params = {})
  @file = file
  @format = format
  @time_format = params[:time_format]
  @params = params

  case format
  when "regexp"
    @regexp = Regexp.new(params[:regexp])
    @time_format = nil
  when "ltsv", "json", "csv", "tsv"
    @regexp = nil
    @time_format = nil
  else # apache, nginx, etc
    definition = Fluent::TextParser::TEMPLATE_REGISTRY.lookup(format).call
    raise "Unknown format '#{format}'" unless definition
    definition.configure({}) # NOTE: SyslogParser define @regexp in configure method so call it to grab Regexp object
    @regexp = definition.patterns["format"]
    @time_format = definition.patterns["time_format"]
  end
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



3
4
5
# File 'lib/regexp_preview/single_line.rb', line 3

def file
  @file
end

#formatObject (readonly)

Returns the value of attribute format.



3
4
5
# File 'lib/regexp_preview/single_line.rb', line 3

def format
  @format
end

#paramsObject (readonly)

Returns the value of attribute params.



3
4
5
# File 'lib/regexp_preview/single_line.rb', line 3

def params
  @params
end

#regexpObject (readonly)

Returns the value of attribute regexp.



3
4
5
# File 'lib/regexp_preview/single_line.rb', line 3

def regexp
  @regexp
end

#time_formatObject (readonly)

Returns the value of attribute time_format.



3
4
5
# File 'lib/regexp_preview/single_line.rb', line 3

def time_format
  @time_format
end

Instance Method Details

#matches_jsonObject



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/regexp_preview/single_line.rb', line 27

def matches_json
  {
    params: {
      setting: {
        # NOTE: regexp and time_format are used when format == 'apache' || 'nginx' || etc.
        regexp: regexp.try(:source),
        time_format: time_format,
      }
    },
    matches: matches.compact,
  }
end