Class: ApplicationInsights::Channel::Contracts::JsonSerializable

Inherits:
Object
  • Object
show all
Defined in:
lib/application_insights/channel/contracts/json_serializable.rb

Instance Method Summary collapse

Constructor Details

#initialize(defaults, values, options) ⇒ JsonSerializable

Returns a new instance of JsonSerializable.



7
8
9
10
11
12
13
14
15
# File 'lib/application_insights/channel/contracts/json_serializable.rb', line 7

def initialize(defaults, values, options)
  @defaults = defaults
  @values = values
  if options != nil
    options.each do |key, value|
      self.send key.to_s + '=', value
    end
  end
end

Instance Method Details

#to_hObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/application_insights/channel/contracts/json_serializable.rb', line 17

def to_h
  output = {}
  @defaults.each do |key, default|
    if @values.key? key
      value = @values[key]
      value = default if value == nil
    elsif default
      value = default
    else
      next
    end

    if value.class == Array
      value_copy = []
      value.each do |item|
        item.respond_to?(:to_h) ? value_copy.push(item.to_h) : value_copy.push(item)
      end
      output[key] = value_copy if value_copy.length > 0
    elsif value.class == Hash
      value_copy = {}
      value.each do |item_key, item_value|
        (item_value.respond_to? :to_h) ? value_copy[item_key] = item_value.to_h : value_copy[item_key] = item_value
      end
      output[key] = value_copy if value_copy.length > 0
    elsif value.respond_to? :to_h
      value_copy = value.to_h
      output[key] = value_copy if value_copy.length > 0
    else
      output[key] = value
    end
  end
  output
end

#to_jsonObject



51
52
53
54
# File 'lib/application_insights/channel/contracts/json_serializable.rb', line 51

def to_json(*)
  hash = self.to_h
  hash.to_json
end