Module: Kontena::Util
- Included in:
- Cli::Certificate::DomainAuthorization::ListCommand, Cli::Certificate::ListCommand, Cli::Containers::ListCommand, Cli::Master::Token::ListCommand, Cli::MasterCommand, Cli::Plugins::InstallCommand, Cli::Plugins::UninstallCommand, Cli::Stacks::YAML::Reader, Cli::Stacks::YAML::ServiceExtender, Cli::Vault::ListCommand, Cli::Volumes::ListCommand
- Defined in:
- lib/kontena/util.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
- .included(base) ⇒ Object
- .stringify_keys(obj) ⇒ Object
- .stringify_keys!(obj) ⇒ Object
- .symbolize_keys(obj) ⇒ Object
- .symbolize_keys!(obj) ⇒ Object
- .which(cmd) ⇒ Object
Instance Method Summary collapse
- #longest_string_in_array(array) ⇒ Object
-
#safe_dig(hash, *keys) ⇒ Object
Compatibility between ruby_dig and Ruby 2.3.
- #seconds_to_human(seconds) ⇒ Object
- #time_ago(time) ⇒ Object
- #time_until(seconds) ⇒ Object
Class Method Details
.included(base) ⇒ Object
3 4 5 |
# File 'lib/kontena/util.rb', line 3 def self.included(base) base.extend(ClassMethods) end |
.stringify_keys(obj) ⇒ Object
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/kontena/util.rb', line 31 def stringify_keys(obj) case obj when Hash obj.map { |k,v| [k.to_s, stringify_keys(v)] }.to_h when Array obj.map { |v| stringify_keys(v) } else obj end end |
.stringify_keys!(obj) ⇒ Object
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/kontena/util.rb', line 43 def stringify_keys!(obj) case obj when Hash obj.keys.each { |k| obj[k.to_s] = stringify_keys!(obj.delete(k)) } when Array obj.map! { |v| stringify_keys!(v) } else end obj end |
.symbolize_keys(obj) ⇒ Object
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/kontena/util.rb', line 7 def symbolize_keys(obj) case obj when Hash obj.map { |k,v| [k.to_sym, symbolize_keys(v)] }.to_h when Array obj.map { |v| symbolize_keys(v) } else obj end end |
.symbolize_keys!(obj) ⇒ Object
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/kontena/util.rb', line 19 def symbolize_keys!(obj) case obj when Hash obj.keys.each { |k| obj[k.to_sym] = symbolize_keys!(obj.delete(k)) } when Array obj.map! { |v| symbolize_keys!(v) } else end obj end |
.which(cmd) ⇒ Object
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/kontena/util.rb', line 56 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
103 104 105 |
# File 'lib/kontena/util.rb', line 103 def longest_string_in_array(array) array.max_by(&:length).length 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.
73 74 75 76 77 |
# File 'lib/kontena/util.rb', line 73 def safe_dig(hash, *keys) hash.dig(*keys) rescue TypeError nil end |
#seconds_to_human(seconds) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/kontena/util.rb', line 90 def seconds_to_human(seconds) if seconds > 60 * 60 * 24 result = "#{seconds / 60 / 60 / 24} days" elsif seconds > 60 * 60 result = "#{seconds / 60 / 60} hours" elsif seconds > 60 result = "#{seconds / 60} minutes" else result = "#{seconds} seconds" end result.start_with?('1 ') ? result[0..-2] : result end |
#time_ago(time) ⇒ Object
79 80 81 82 83 84 |
# File 'lib/kontena/util.rb', line 79 def time_ago(time) now = Time.now.to_i time = time.kind_of?(Integer) ? time : DateTime.parse(time).to_time.to_i diff = now - time seconds_to_human(diff) + ' ago' end |
#time_until(seconds) ⇒ Object
86 87 88 |
# File 'lib/kontena/util.rb', line 86 def time_until(seconds) 'in ' + seconds_to_human(seconds) end |