class Observium::Agent::Parser

Attributes

sections[R]

Public Class Methods

new(text) click to toggle source
# File lib/observium/agent/parser.rb, line 6
def initialize(text)
  @text = text

  validate!
  preprocess
end

Public Instance Methods

has_section?(title) click to toggle source
# File lib/observium/agent/parser.rb, line 36
def has_section?(title)
  @sections.include? title
end
section(title, postprocess=true) click to toggle source
# 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) click to toggle source
# File lib/observium/agent/parser.rb, line 40
def symbolize_field(field)
  field.downcase!
  field.tr! "-", "_"
  field.to_sym
end

Private Instance Methods

preprocess() click to toggle source
# File lib/observium/agent/parser.rb, line 59
def preprocess
  @lines = @text.split("\n")
  @sections = @text.scan(/<<<([^>]*)>>>/).map {|s| s.first }
end
valid_text?() click to toggle source
# File lib/observium/agent/parser.rb, line 52
def valid_text?
  !@text.nil? and
  !@text.strip.empty? and
  !@text.lines.count.zero? and
  @text.lines.count != 1
end
validate!() click to toggle source
# File lib/observium/agent/parser.rb, line 48
def validate!
  valid_text? or raise Errors::ParsingFailed, "Invalid text passed to the parser"
end