Module: Occi::Cli::OcciOpts::Helper

Defined in:
lib/occi/cli/occi_opts/occi_opts_helper.rb

Constant Summary collapse

ALLOWED_CONTEXT_VARS =
[:public_key, :user_data].freeze
MIXIN_REGEXP =
/^(\S+?)#(\S+)$/
CONTEXT_REGEXP =
ATTR_REGEXP = /^(\S+?)=(.+)$/
ATTR_NUM_EXP =
/num\((?<number>\d+)\)/
ATTR_BOOL_EXP =
/bool\((?<bool>true|false)\)/
ATTR_FLOAT_EXP =
/float\((?<float>\d+\.\d+)\)/
ATTR_INVALID_EXP =
/num\(.*\)|bool\(.*\)|float\(.*\)/

Class Method Summary collapse

Class Method Details

.parse_action(action) ⇒ Object


58
59
60
61
62
63
64
65
66
67
# File 'lib/occi/cli/occi_opts/occi_opts_helper.rb', line 58

def self.parse_action(action)
  raise "Unknown action format #{action.inspect}! Use SCHEME#TERM or TERM!" if action.blank?
  parts = action.split('#')

  if parts.size == 2
    Occi::Core::Action.new("#{parts[0]}#", parts[1])
  else
    Occi::Core::Action.new("action#", parts.last)
  end
end

.parse_attribute(attribute) ⇒ Object

Raises:

  • (ArgumentError)

41
42
43
44
45
46
47
48
49
# File 'lib/occi/cli/occi_opts/occi_opts_helper.rb', line 41

def self.parse_attribute(attribute)
  ary = ATTR_REGEXP.match(attribute).to_a.drop 1
  raise ArgumentError, "Attribute must always contain ATTR=VALUE pairs!" unless ary.length == 2

  # type-cast the value, if necessary
  ary[1] = parse_attribute_value(ary[1])
  
  ary
end

.parse_attribute_value(value) ⇒ Object

Raises:

  • (ArgumentError)

69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/occi/cli/occi_opts/occi_opts_helper.rb', line 69

def self.parse_attribute_value(value)
  result = value

  ATTR_NUM_EXP =~ value
  result = Regexp.last_match(:number).to_i if Regexp.last_match(:number)

  ATTR_BOOL_EXP =~ value
  result = (Regexp.last_match(:bool) == 'true' ? true : false) if Regexp.last_match(:bool)

  ATTR_FLOAT_EXP =~ value
  result = Regexp.last_match(:float).to_f if Regexp.last_match(:float)

  raise ArgumentError, "Failed to cast attribute value #{result.inspect}!" if ATTR_INVALID_EXP =~ result.to_s

  result
end

.parse_context_variable(cvar) ⇒ Object

Raises:

  • (ArgumentError)

17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/occi/cli/occi_opts/occi_opts_helper.rb', line 17

def self.parse_context_variable(cvar)
  ary = CONTEXT_REGEXP.match(cvar).to_a.drop 1
  raise ArgumentError, "Context variables must always contain ATTR=VALUE pairs!" unless ary.length == 2

  symbol = ary[0].to_sym
  unless ALLOWED_CONTEXT_VARS.include?(symbol)
    raise ArgumentError,
          "Only #{ALLOWED_CONTEXT_VARS.join(', ')} context " \
          "variables are supported! #{symbol.to_s.inspect}"
  end

  context_data = ary[1]
  if context_data.gsub!(/^file:\/\//,'')
    raise "File does not exist! #{context_data.inspect}" unless File.exist? context_data
    context_data = File.read(context_data)
  end

  if symbol == :user_data
    context_data = Base64.encode64(context_data).gsub("\n", '')
  end

  [symbol, context_data.strip]
end

.parse_mixin(mixin) ⇒ Object


51
52
53
54
55
56
# File 'lib/occi/cli/occi_opts/occi_opts_helper.rb', line 51

def self.parse_mixin(mixin)
  parts = MIXIN_REGEXP.match(mixin).to_a.drop(1)
  raise "Unknown mixin format #{mixin.inspect}! Use SCHEME#TERM or SHORT_SCHEME#TERM!" unless parts.length == 2

  Occi::Core::Mixin.new("#{parts[0]}#", parts[1])
end