Class: Appsignal::Utils::Data

Inherits:
Object
  • Object
show all
Defined in:
lib/appsignal/utils/data.rb

Class Method Summary collapse

Class Method Details

.generate(body) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/appsignal/utils/data.rb', line 7

def generate(body)
  if body.is_a?(Hash)
    map_hash(body)
  elsif body.is_a?(Array)
    map_array(body)
  else
    raise TypeError, "Body of type #{body.class} should be a Hash or Array"
  end
end

.map_array(array_value) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/appsignal/utils/data.rb', line 49

def map_array(array_value)
  array = Appsignal::Extension.data_array_new
  array_value.each do |value|
    case value
    when String
      array.append_string(value)
    when Integer
      # An Integer too big for C-lang longs to fit
      bigint = 1 << 63
      if value >= bigint
        array.append_string(map_bigint(value))
      else
        array.append_integer(value)
      end
    when Float
      array.append_float(value)
    when TrueClass, FalseClass
      array.append_boolean(value)
    when NilClass
      array.append_nil
    when Hash
      array.append_data(map_hash(value))
    when Array
      array.append_data(map_array(value))
    else
      array.append_string(value.to_s)
    end
  end
  array
end

.map_bigint(value) ⇒ Object



80
81
82
# File 'lib/appsignal/utils/data.rb', line 80

def map_bigint(value)
  "bigint:#{value}"
end

.map_hash(hash_value) ⇒ Object



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
# File 'lib/appsignal/utils/data.rb', line 17

def map_hash(hash_value)
  map = Appsignal::Extension.data_map_new
  hash_value.each do |key, value|
    key = key.to_s
    case value
    when String
      map.set_string(key, value)
    when Integer
      # An Integer too big for C-lang longs to fit
      bigint = 1 << 63
      if value >= bigint
        map.set_string(key, map_bigint(value))
      else
        map.set_integer(key, value)
      end
    when Float
      map.set_float(key, value)
    when TrueClass, FalseClass
      map.set_boolean(key, value)
    when NilClass
      map.set_nil(key)
    when Hash
      map.set_data(key, map_hash(value))
    when Array
      map.set_data(key, map_array(value))
    else
      map.set_string(key, value.to_s)
    end
  end
  map
end