Module: Vendi::Utils

Included in:
Machine
Defined in:
lib/vendi/utils.rb

Overview

general utility methods

Instance Method Summary collapse

Instance Method Details

#as_ada(quantity) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/vendi/utils.rb', line 14

def as_ada(quantity)
  res = quantity.to_f / 1_000_000
  str_res = format('%.7f', res)
  last = str_res[-1]
  final = ''
  until last == '.'
    str_res.chop!
    if last == '0'
      final = str_res
      break unless final[-1] == '0'
    end
    last = str_res[-1]
  end
  final.chop! if final[-1] == '.'
  "#{final}"
end

#eventually(label, &block) ⇒ Object

wait until action passed as &block returns true or TIMEOUT is reached



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/vendi/utils.rb', line 33

def eventually(label, &block)
  current_time = Time.now
  timeout_treshold = current_time + Vendi::TIMEOUT
  while (block.call == false) && (current_time <= timeout_treshold)
    sleep 5
    current_time = Time.now
  end
  if current_time > timeout_treshold
    @logger.error "Action '#{label}' did not resolve within timeout: #{Vendi::TIMEOUT}s"
    false
  else
    true
  end
end

#from_json(file) ⇒ Object



6
7
8
# File 'lib/vendi/utils.rb', line 6

def from_json(file)
  JSON.parse(File.read(file), { symbolize_names: true })
end

#to_json(file, hash) ⇒ Object



10
11
12
# File 'lib/vendi/utils.rb', line 10

def to_json(file, hash)
  File.write(file, JSON.pretty_generate(hash))
end