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



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

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



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

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



99
100
101
102
103
# File 'lib/rollbar/util.rb', line 99

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



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
40
41
# File 'lib/rollbar/util.rb', line 6

def self.iterate_and_update(obj, block)
  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

      if new_key != k
        key_updates.push([k, new_key])
      end
    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



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

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



85
86
87
88
89
90
91
92
93
# File 'lib/rollbar/util.rb', line 85

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

  if str.length <= length or str.length <= ellipsis.length
    return str
  end

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

.uuid_rollbar_url(data, configuration) ⇒ Object



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

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