Module: Jerakia::Util

Defined in:
lib/jerakia/util.rb,
lib/jerakia/util/http.rb

Defined Under Namespace

Classes: Http

Class Method Summary collapse

Class Method Details

.autoload(path, mod) ⇒ Object


4
5
6
7
# File 'lib/jerakia/util.rb', line 4

def autoload(path, mod)
  Jerakia.log.debug "autoloading #{path} #{mod}"
  require "jerakia/#{path}/#{mod}"
end

.dig(data, dig_path) ⇒ Object


9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/jerakia/util.rb', line 9

def dig(data, dig_path)
  key = dig_path.shift
  if dig_path.empty?
    if data.has_key?(key)
      return data[key]
    else
      return :not_found
    end
  else
    return :not_found unless data[key].is_a?(Hash)
    return dig(data[key], dig_path)
  end
end

.walk(data) ⇒ Object


23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/jerakia/util.rb', line 23

def walk(data)
  if data.is_a?(Hash)
    walk_hash(data) do |target|
      yield target
    end
  elsif data.is_a?(Array)
    walk_array(data) do |target|
      yield target
    end
  else
    yield data
  end
end

.walk_array(data) ⇒ Object


51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/jerakia/util.rb', line 51

def walk_array(data)
  data.map! do |element|
    if element.is_a?(Hash)
      walk_hash(element) { |x| yield x }
    elsif element.is_a?(Array)
      walk_array(element) { |x| yield x }
    else
      yield element
    end
    element
  end
end

.walk_hash(data) ⇒ Object


37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/jerakia/util.rb', line 37

def walk_hash(data)
  data.each_with_object({}) do |(_k, v), h|
    if v.is_a?(Hash)
      walk_hash(v) { |x| yield x }
    elsif v.is_a?(Array)
      walk_array(v) { |x| yield x }
    else
      yield v
    end
    h
  end
  data
end