Module: Json2Graphite

Defined in:
lib/json2graphite.rb

Class Method Summary collapse

Class Method Details

.dump(hash, time = Time.now.to_i) ⇒ Object

Converts a hash of hashes into dot notaion used by graphite targets. Takes optional time argument, defaulting to now.



7
8
9
10
11
12
13
14
# File 'lib/json2graphite.rb', line 7

def dump(hash, time = Time.now.to_i)
  data = []
  raise "Hash was not received" unless hash.is_a? Hash
  walk_the_forrest(hash) do |target, value|
    data << "#{target} #{value} #{time}"
  end
  data
end

.dump_as_hash(hash, time = Time.now.to_i) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/json2graphite.rb', line 16

def dump_as_hash(hash, time = Time.now.to_i)
  data = []
  raise "Hash was not received" unless hash.is_a? Hash
  walk_the_forrest(hash) do |target, value|
    data << { :target => target, :value => value, :time => time }
  end
  data
end

.walk_the_forrest(obj = self, path = [], &blk) ⇒ Object

DOCUMENT THIS!!!



30
31
32
33
34
35
36
37
38
39
# File 'lib/json2graphite.rb', line 30

def walk_the_forrest (obj=self, path=[], &blk)
  obj.each do |key,value|
    case value
    when Hash
      walk_the_forrest(value, [key, *path].reverse, &blk)
    else
      blk.call("#{path.join('.')}.#{key}",value)
    end
  end
end