Sha256: 1608981ade667453665d1cc6ea40b917588ef3194c51aba2ca23bccd3c9529d0

Contents?: true

Size: 1.23 KB

Versions: 44

Compression:

Stored size: 1.23 KB

Contents

require 'tempfile'
require 'yaml'
require 'json'
module Consul
  module Async
    class Utilities
      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

      # Loads parameters from a file, supports JSON and YAML
      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

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

Version data entries

44 entries across 44 versions & 1 rubygems

Version Path
consul-templaterb-1.23.0 lib/consul/async/utilities.rb
consul-templaterb-1.22.0 lib/consul/async/utilities.rb
consul-templaterb-1.21.8 lib/consul/async/utilities.rb
consul-templaterb-1.21.7 lib/consul/async/utilities.rb
consul-templaterb-1.21.6 lib/consul/async/utilities.rb
consul-templaterb-1.21.5 lib/consul/async/utilities.rb
consul-templaterb-1.21.4 lib/consul/async/utilities.rb
consul-templaterb-1.21.3 lib/consul/async/utilities.rb
consul-templaterb-1.21.2 lib/consul/async/utilities.rb
consul-templaterb-1.21.1 lib/consul/async/utilities.rb
consul-templaterb-1.21.0 lib/consul/async/utilities.rb
consul-templaterb-1.20.0 lib/consul/async/utilities.rb
consul-templaterb-1.19.0 lib/consul/async/utilities.rb
consul-templaterb-1.18.5 lib/consul/async/utilities.rb
consul-templaterb-1.18.4 lib/consul/async/utilities.rb
consul-templaterb-1.18.3 lib/consul/async/utilities.rb
consul-templaterb-1.18.2 lib/consul/async/utilities.rb
consul-templaterb-1.18.1 lib/consul/async/utilities.rb
consul-templaterb-1.18.0 lib/consul/async/utilities.rb
consul-templaterb-1.17.4 lib/consul/async/utilities.rb