Module: Metrix::JsonMetric

Included in:
ElasticSearch, Mongodb
Defined in:
lib/metrix/json_metric.rb

Instance Method Summary collapse

Instance Method Details

#attributesObject



5
6
7
# File 'lib/metrix/json_metric.rb', line 5

def attributes
  @attributes ||= JSON.load(@raw)
end

#extract(attributes, prefix = nil) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/metrix/json_metric.rb', line 9

def extract(attributes, prefix = nil)
  attributes.inject({}) do |hash, (k, v)|
    path = [prefix, k].compact.join(".")
    case v
    when Hash
      hash.merge!(extract(v, path))
    when Array
      v.each_with_index do |array_value, i|
        hash.merge!(extract(v, "#{path}i"))
      end
    when Numeric
      hash[path] = v
    end
    hash
  end
end