Class: HaproxyParser::FormatChecker

Inherits:
Object
  • Object
show all
Defined in:
lib/haproxy_parser/format_checker.rb

Constant Summary collapse

FormatError =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ FormatChecker

Returns a new instance of FormatChecker.



9
10
11
# File 'lib/haproxy_parser/format_checker.rb', line 9

def initialize(path)
  @path = path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



8
9
10
# File 'lib/haproxy_parser/format_checker.rb', line 8

def path
  @path
end

#resultObject

Returns the value of attribute result.



7
8
9
# File 'lib/haproxy_parser/format_checker.rb', line 7

def result
  @result
end

Instance Method Details

#checkObject



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/haproxy_parser/format_checker.rb', line 29

def check
  # create file whose user, group is deleted. #
  tmp_file = Tempfile.open("haproxy_parser") do |tmp_f|
    lines.each do |line|
      tmp_f.print(line)
    end
    tmp_f
  end
  status,_,stderr = systemu "haproxy -c -f #{tmp_file.path}"
  tmp_file.unlink
  @result = stderr unless status.success?
end

#linesObject



18
19
20
21
22
23
24
25
26
27
# File 'lib/haproxy_parser/format_checker.rb', line 18

def lines
  @lines ||= [].tap do |arr|
    File.open(path, "r") do |f|
      f.each_line do |line|
        next if /\s+(user|group)\s+/ =~ line
        arr << line
      end
    end
  end
end

#runObject

Raises:



13
14
15
16
# File 'lib/haproxy_parser/format_checker.rb', line 13

def run
  check
  raise(FormatError, result) if result
end