Module: Naplug::Helpers::JSON_Thresholds::ClassMethods

Defined in:
lib/naplug/helpers/json_thresholds.rb

Instance Method Summary collapse

Instance Method Details

#hashify_json_thresholds(*threstag) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/naplug/helpers/json_thresholds.rb', line 14

def hashify_json_thresholds(*threstag)
  tag, thresholds_json, thresholds_hash = case threstag.size
                                            when 0, 1
                                             [nil, threstag[0], {}]
                                            else
                                             [threstag[1],threstag[0], {}]
                                          end
  plug = nil
  thresholds_proc = Proc.new do |json_element|
    case
      when (json_element.is_a? String and json_element.match(/\d*:\d*:\d*:\d*/))
        case tag.nil?
          when true
            thresholds_hash[plug] = Hash[Status.states.zip json_element.split(':',-1).map { |v| v.nil? ? nil : v.to_i } ]
          else
            thresholds_hash[tag] = Hash[plug, Hash[Status.states.zip json_element.split(':',-1).map { |v| v.nil? ? nil : v.to_i } ]]
        end
      when Symbol
        plug = json_element
      else
        nil
    end
  end
  JSON.recurse_proc(JSON.parse(thresholds_json, :symbolize_names => true),&thresholds_proc) if thresholds_json
  thresholds_hash
end