Module: Mu::Helper

Included in:
Mu, Command, Ddt, Homepage, HttpHelper, Muapi, Netconfig, Scale, System, MuSL::Maker::Scenario, MuSL::Maker::Send
Defined in:
lib/mu/helper.rb

Constant Summary collapse

ESCAPES =
Array.new 256 do |i|
    case i
    when 9;        "\\t".freeze
    when 13;       "\\r".freeze
    when 92;       "\\\\".freeze
    when 10;       "\\n".freeze
    when 32..126;  i.chr.freeze
    else ;         ('\x%02x' % i).freeze
    end
end

Instance Method Summary collapse

Instance Method Details

#askObject



20
21
22
# File 'lib/mu/helper.rb', line 20

def ask
    gets.strip
end

#bin2hex(input) ⇒ Object



100
101
102
# File 'lib/mu/helper.rb', line 100

def bin2hex input
     IO.read("#{input}").unpack('H*')
end

#error(msg) ⇒ Object



4
5
6
# File 'lib/mu/helper.rb', line 4

def error msg
    $stderr.puts "!! #{msg}"
end

#escape(input, escapes = nil) ⇒ Object

Takes input and a table that maps ascii codes to their representation



91
92
93
94
95
96
97
98
# File 'lib/mu/helper.rb', line 91

def escape input, escapes=nil
    escapes ||= ESCAPES
    output = []
    input.each_byte do |i|
        output << escapes[i]
    end
    output.join
end

#format_float(spaces = 2, arg = 0.0) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/mu/helper.rb', line 30

def format_float(spaces=2,arg=0.0)
  if !arg.nil?
    return sprintf("%.#{spaces}f", arg)
  else
    return ""
  end
end

#get_file_as_string_array(filename) ⇒ Object

IO.readlines



45
46
47
48
49
50
51
52
# File 'lib/mu/helper.rb', line 45

def get_file_as_string_array(filename)
 arr = []
 f = File.open(filename, "r")
 f.each_line do |line|
   arr.push line
 end
 return arr
end

#make_xml(resp) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/mu/helper.rb', line 54

def make_xml(resp)
  begin
    if (/<.+>/).match(resp)
      xmldoc = Nokogiri::XML(resp)
    else
      err_node = Nokogiri::XML::Node.new('err_node', xmldoc)
      err_node.content = resp
      xmldoc.root << err_node
    end
  rescue =>  e
    msg "Error parsing XML " + e.to_s, Logger::DEBUG
  ensure
    msg xmldoc, Logger::DEBUG
    return xmldoc
  end
end

#msg(msg, level = Logger::INFO) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/mu/helper.rb', line 8

def msg msg, level=Logger::INFO
   caller[0] =~ /`([^']*)'/ and method = $1
   if caller[0].include?("mu")
     clz = caller[0][caller[0].index("mu")..caller[0].length]
   else
     clz = caller[0]
   end

   $log.add(level, "#{method} | #{msg}")
   # $log.add(level, "(#{clz}) | #{msg}")
end

#shift(key, argv) ⇒ Object



24
25
26
27
28
# File 'lib/mu/helper.rb', line 24

def shift key, argv
    val = argv.shift
    raise "missing value for #{key}" if val.nil?
    val
end

#to_boolean(value = "false") ⇒ Object



38
39
40
41
42
# File 'lib/mu/helper.rb', line 38

def to_boolean(value="false")
   return false if value.nil?
   return true if value.downcase == "true"
   return false
end