json2graphite 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/bin/json2graphite +1 -1
  2. data/lib/json2graphite.rb +31 -24
  3. metadata +2 -2
data/bin/json2graphite CHANGED
@@ -8,7 +8,7 @@ def run
8
8
  body = ''
9
9
  body += STDIN.read
10
10
  hash = JSON.parse(body)
11
- data = Json2Graphite.to_graphite(hash)
11
+ data = hash.to_graphite
12
12
  puts data.join("\n")
13
13
  end
14
14
 
data/lib/json2graphite.rb CHANGED
@@ -1,40 +1,47 @@
1
1
  module Json2Graphite
2
2
  module_function
3
3
 
4
- # basically reaches into a hash tree and grabs the end value, joining the
5
- # keys along the way returning the block.
4
+ # Converts a hash of hashes into dot notaion used by graphite targets. Takes
5
+ # optional time argument, defaulting to now.
6
6
 
7
- def walk_the_forrest (obj=self, path=[], &blk)
8
- obj.each do |key,value|
9
- case value
10
- when Hash
11
- walk_the_forrest(value, [*path, key], &blk)
12
- else
13
- blk.call("#{[*path, key].join('.')}",value)
14
- end
15
- end
16
- end
17
-
18
- # Converts a hash of hashes into dot notaion used by graphite targets and
19
- # outputs to STDOUT. Takes optional time argument, defaulting to now.
20
-
21
- def to_graphite (hash, time=Time.now.to_i)
7
+ def dump(hash, time = Time.now.to_i)
22
8
  data = []
23
9
  raise "Hash was not received" unless hash.is_a? Hash
24
- walk_the_forrest(hash) {|target, value|
10
+ walk_the_forrest(hash) do |target, value|
25
11
  data << "#{target} #{value} #{time}"
26
- }
12
+ end
27
13
  data
28
14
  end
29
15
 
30
- # Return an array of hashes, each hash a single graphite target, value, and time
31
- def get_graphite (hash, time=Time.now.to_i)
32
- raise "Hash was not received" unless hash.is_a? Hash
16
+ def dump_as_hash(hash, time = Time.now.to_i)
33
17
  data = []
34
- walk_the_forrest(hash) {|target, value|
18
+ raise "Hash was not received" unless hash.is_a? Hash
19
+ walk_the_forrest(hash) do |target, value|
35
20
  data << { :target => target, :value => value, :time => time }
36
- }
21
+ end
37
22
  data
38
23
  end
39
24
 
25
+ # For backward compatibility with other things
26
+ alias_method :to_graphite, :dump
27
+ alias_method :get_graphite, :dump_as_hash
28
+
29
+ # DOCUMENT THIS!!!
30
+ def walk_the_forrest (obj=self, path=[], &blk)
31
+ obj.each do |key,value|
32
+ case value
33
+ when Hash
34
+ walk_the_forrest(value, [key, *path].reverse, &blk)
35
+ else
36
+ blk.call("#{path.join('.')}.#{key}",value)
37
+ end
38
+ end
39
+ end
40
+ end
41
+
42
+ class Hash
43
+
44
+ def to_graphite
45
+ Json2Graphite.dump(self)
46
+ end
40
47
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json2graphite
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-20 00:00:00.000000000 Z
12
+ date: 2012-12-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json