Module: Rollbar::Util

Defined in:
lib/rollbar/util.rb,
lib/rollbar/util/hash.rb,
lib/rollbar/util/ip_obfuscator.rb

Defined Under Namespace

Modules: Hash, IPObfuscator

Class Method Summary collapse

Class Method Details

.deep_copy(obj) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rollbar/util.rb', line 51

def self.deep_copy(obj)
  if obj.is_a?(::Hash)
    result = obj.clone
    obj.each { |k, v| result[k] = deep_copy(v)}
    result
  elsif obj.is_a?(Array)
    result = obj.clone
    result.clear
    obj.each { |v| result << deep_copy(v)}
    result
  else
    obj
  end
end

.deep_merge(hash1, hash2) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/rollbar/util.rb', line 66

def self.deep_merge(hash1, hash2)
  hash1 ||= {}
  hash2 ||= {}

  hash2.each_key do |k|
    if hash1[k].is_a?(::Hash) && hash2[k].is_a?(::Hash)
      hash1[k] = deep_merge(hash1[k], hash2[k])
    elsif hash1[k].is_a?(Array) && hash2[k].is_a?(Array)
      hash1[k] += deep_copy(hash2[k])
    elsif hash2[k]
      hash1[k] = deep_copy(hash2[k])
    end
  end

  hash1
end

.enforce_valid_utf8(payload) ⇒ Object



95
96
97
98
99
# File 'lib/rollbar/util.rb', line 95

def self.enforce_valid_utf8(payload)
  normalizer = lambda { |object| Encoding.encode(object) }

  Util.iterate_and_update(payload, normalizer)
end

.iterate_and_update(obj, block) ⇒ Object



5
6
7
8
9
10
11
12
13
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/rollbar/util.rb', line 5

def self.iterate_and_update(obj, block)
  return if obj.frozen?
  if obj.is_a?(Array)
    for i in 0 ... obj.size
      value = obj[i]

      if value.is_a?(::Hash) || value.is_a?(Array)
        self.iterate_and_update(value, block)
      else
        obj[i] = block.call(value)
      end
    end
  else
    key_updates = []

    obj.each do |k, v|
      new_key = nil

      if v.is_a?(::Hash) || v.is_a?(Array)
        self.iterate_and_update(v, block)
        new_key = block.call(k)
      else
        new_key = block.call(k)
        obj[k] = block.call(v)
      end

      key_updates.push([k, new_key]) if new_key != k
    end

    key_updates.each do |old_key, new_key|
      obj[new_key] = obj[old_key]
      obj.delete(old_key)
    end
  end
end

.iterate_and_update_hash(hash, block) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/rollbar/util.rb', line 41

def self.iterate_and_update_hash(hash, block)
  hash.each do |k, v|
    if v.is_a?(::Hash)
      self.iterate_and_update_hash(v, block)
    else
      hash[k] = block.call(k, v)
    end
  end
end

.truncate(str, length) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/rollbar/util.rb', line 83

def self.truncate(str, length)
  ellipsis = '...'

  return str if str.length <= length || str.length <= ellipsis.length

  str.unpack('U*').slice(0, length - ellipsis.length).pack('U*') + ellipsis
end

.uuid_rollbar_url(data, configuration) ⇒ Object



91
92
93
# File 'lib/rollbar/util.rb', line 91

def self.uuid_rollbar_url(data, configuration)
  "#{configuration.web_base}/instance/uuid?uuid=#{data[:uuid]}"
end