Class: Consul::Async::Utilities

Inherits:
Object
  • Object
show all
Defined in:
lib/consul/async/utilities.rb

Overview

Various utilities

Class Method Summary collapse

Class Method Details

.bytes_to_h(bytes) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/consul/async/utilities.rb', line 8

def self.bytes_to_h(bytes)
  if bytes < 1024
    "#{bytes} b"
  else
    if bytes < 1_048_576
      bytes_h = bytes / 1024.0
      unit_prefix = 'K'
    elsif bytes < 1_073_741_824
      bytes_h = bytes / 1_048_576.0
      unit_prefix = 'M'
    else
      bytes_h = bytes / 1_073_741_824.0
      unit_prefix = 'G'
    end
    "#{bytes_h.round(2)} #{unit_prefix}b"
  end
end

.load_parameters_from_file(parameters_file) ⇒ Object

Loads parameters from a file, supports JSON and YAML



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/consul/async/utilities.rb', line 27

def self.load_parameters_from_file(parameters_file)
  raise "Parameters file #{parameters_file} does not exists" unless File.exist? parameters_file

  if parameters_file.downcase.end_with?('.yaml', '.yml')
    YAML.load_file(parameters_file)
  elsif parameters_file.downcase.end_with?('.json')
    JSON.parse(File.read(parameters_file))
  else
    raise "Don't know how to load parameters file #{parameters_file}: JSON and YAML supported"
  end
end

.randomObject



39
40
41
42
# File 'lib/consul/async/utilities.rb', line 39

def self.random
  @random ||= Random.new
  @random
end