Module: Kontena::Util

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
# File 'lib/kontena/util.rb', line 3

def self.included(base)
    base.extend(ClassMethods)
end

.which(cmd) ⇒ Object

Parameters:



8
9
10
11
12
13
14
15
16
17
# File 'lib/kontena/util.rb', line 8

def which(cmd)
  exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
  ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
    exts.each { |ext|
      exe = File.join(path, "#{cmd}#{ext}")
      return exe if File.executable?(exe) && !File.directory?(exe)
    }
  end
  return nil
end

Instance Method Details

#longest_string_in_array(array) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/kontena/util.rb', line 46

def longest_string_in_array(array)
  longest = 0
  array.each do |item|
    longest = item.length if item.length > longest
  end

  longest
end

#safe_dig(hash, *keys) ⇒ Object

Compatibility between ruby_dig and Ruby 2.3. Ruby_dig returns nil when trying to dig into a string, Ruby 2.3 dig raises TypeError.

Parameters:

  • source_hash (Hash)
  • list_of_keys (*keys)


25
26
27
28
29
# File 'lib/kontena/util.rb', line 25

def safe_dig(hash, *keys)
  hash.dig(*keys)
rescue TypeError
  nil
end

#time_ago(time) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/kontena/util.rb', line 31

def time_ago(time)
  now = Time.now.to_i
  time = DateTime.parse(time).to_time.to_i
  diff = now - time
  if diff > 60 * 60 * 24
    "#{diff / 60 / 60 / 24} days"
  elsif diff > 60 * 60
    "#{diff / 60 / 60} hours"
  elsif diff > 60
    "#{diff / 60} minutes"
  else
    "#{diff} seconds"
  end
end