Class: Slnky::CLI::Notify

Inherits:
Base
  • Object
show all
Defined in:
lib/slnky/cli/notify.rb

Instance Method Summary collapse

Instance Method Details

#dothash(list) ⇒ Object

convert list of dot notation key.name=value into nested hash



43
44
45
46
47
48
49
50
# File 'lib/slnky/cli/notify.rb', line 43

def dothash(list)
  input_hash = list.inject({}) {|h, e| (k,v)=e.split('='); h[k]=v; h}
  input_hash.map do |main_key, main_value|
    main_key.to_s.split(".").reverse.inject(main_value) do |value, key|
      {key.to_sym => value(value)}
    end
  end.inject(&:deep_merge)
end

#executeObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/slnky/cli/notify.rb', line 20

def execute
  attributes = {}
  if file
    begin
      yaml = YAML.load_file(file)
      attributes.merge!(yaml)
    rescue => e
      puts "ERROR: reading file #{file}"
      exit(1)
    end
  end
  attributes.merge!(dothash(kvs)) if kvs
  chat = attributes.delete(:chat)
  msg = Slnky::Message.new({name: name, attributes: attributes, chat: chat})
  cfg = Slnky.config
  srv = server || cfg['slnky']['url']

  puts 'sending message:'
  puts JSON.pretty_generate(msg.to_h)
  Slnky.notify(msg, srv) unless dry_run?
end

#value(value) ⇒ Object

convert value from string to internal types



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

def value(value)
  return value unless value.is_a?(String)
  case value
    when 'true'
      true
    when 'false'
      false
    when /^\d+\.\d+\.\d+/ # ip addr
      value
    when /^\d+$/ # number
      value.to_i
    when /^[\d\.]+$/ # float
      value.to_f
    else
      value
  end
end