Class: Observium::Agent::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/observium/agent/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ Parser

Returns a new instance of Parser.



6
7
8
9
10
11
# File 'lib/observium/agent/parser.rb', line 6

def initialize(text)
  @text = text

  validate!
  preprocess
end

Instance Attribute Details

#sectionsObject (readonly)

Returns the value of attribute sections.



4
5
6
# File 'lib/observium/agent/parser.rb', line 4

def sections
  @sections
end

Instance Method Details

#has_section?(title) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/observium/agent/parser.rb', line 36

def has_section?(title)
  @sections.include? title
end

#section(title, postprocess = true) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/observium/agent/parser.rb', line 13

def section(title, postprocess=true)
  return {} unless has_section?(title)
  
  index = @lines.index("<<<#{title}>>>")

  array = []
  @lines[(index+1)..@lines.size].each do |line|
    break if line.include? "<<<"
    array << line
  end

  return array.join("\n") unless postprocess

  hash = Hash.new
  array.each do |line|
    if m = line.match(/^(\S+)=(.*)$/)
      hash[ symbolize_field( m[1] ) ] = m[2]
    end
  end

  hash
end

#symbolize_field(field) ⇒ Object



40
41
42
43
44
# File 'lib/observium/agent/parser.rb', line 40

def symbolize_field(field)
  field.downcase!
  field.tr! "-", "_"
  field.to_sym
end