Class: Riemann::Tools::Opensearch::JsonMapper

Inherits:
Object
  • Object
show all
Defined in:
lib/riemann/tools/opensearch.rb

Direct Known Subclasses

Allocation, Health

Instance Method Summary collapse

Constructor Details

#initialize(values) ⇒ JsonMapper

Returns a new instance of JsonMapper.



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/riemann/tools/opensearch.rb', line 10

def initialize(values)
  values.each do |k, v|
    components = k.split(".")
    last_component = components.pop

    s = self
    components.each do |c|
      s = s.send(c)
    end
    s.send(:"#{last_component}=", normalize(v))
  end
end

Instance Method Details

#normalize(value) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/riemann/tools/opensearch.rb', line 23

def normalize(value)
  case value
  when "true" then true
  when "false" then false
  when /\A\d+\z/ then value.to_i
  when /\A\d+\.\d+\z/ then value.to_f
  when /\A\d+\.\d+%\z/ then value.to_f * 0.01
  when "-" then nil
  else value
  end
end