Class: SlashPort::Exec

Inherits:
Object
  • Object
show all
Defined in:
app/models/base/exec.rb

Instance Method Summary collapse

Constructor Details

#initialize(cmd) ⇒ Exec

Returns a new instance of Exec.



2
3
4
# File 'app/models/base/exec.rb', line 2

def initialize(cmd)
  @cmd = cmd
end

Instance Method Details

#runObject



6
7
8
9
10
11
# File 'app/models/base/exec.rb', line 6

def run
  output = `#{@cmd}`
  code = $?.exitstatus

  return [output, code]
end

#to_tupleObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/models/base/exec.rb', line 13

def to_tuple
  data = []

  output, code = run
  lines = output.split(/\r?\n/)

  if lines.length == 0
    tuple = SlashPort::Tuple.new
    tuple.data["output-lines"] = lines.length
    tuple.data["exit-code"] = code
    data << tuple
  end

  lines.each do |line|
    tuple = SlashPort::Tuple.new
    tuple.data["exit-code"] = code
    begin
      tuple.data["value"] = Float(line)
    rescue ArgumentError => e
      tuple.labels["string"] = 1
      tuple.data["value"] = line
    end

    data << tuple
  end
  return data
end