class Slnky::CLI::Notify

Public Instance Methods

dothash(list) click to toggle source

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

# 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
execute() click to toggle source
# 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) click to toggle source

convert value from string to internal types

# 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) click to toggle source
# 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