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



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

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



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/slnky/cli/notify.rb', line 19

def execute
  Slnky::Config.configure('cli')
  attributes = {}
  attributes.merge!(yaml_file(file)) if file
  attributes.merge!(dothash(kvs)) if kvs
  chat = attributes.delete(:chat)
  msg = Slnky::Message.new({name: name, attributes: attributes, chat: chat})

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

#value(value) ⇒ Object

convert value from string to internal types



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

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

#yaml_file(file) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/slnky/cli/notify.rb', line 32

def yaml_file(file)
  begin
    YAML.load_file(file)
  rescue => e
    puts "ERROR: reading file #{file}"
    exit(1)
  end
end