Module: Rollbar::Util

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

Overview

:nodoc:

Defined Under Namespace

Modules: Hash, IPAnonymizer, IPObfuscator

Class Method Summary collapse

Class Method Details

.clone_obj(obj) ⇒ Object



72
73
74
75
76
77
78
79
80
81
# File 'lib/rollbar/util.rb', line 72

def self.clone_obj(obj)
  case obj
  when ::Hash
    obj.dup
  when Array
    obj.dup.clear
  else
    obj
  end
end

.count_method_in_stack(method_symbol, file_path = '') ⇒ Object



129
130
131
# File 'lib/rollbar/util.rb', line 129

def self.count_method_in_stack(method_symbol, file_path = '')
  caller.grep(/#{file_path}.*#{method_symbol}/).count
end

.deep_copy(obj, copied = {}) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/rollbar/util.rb', line 50

def self.deep_copy(obj, copied = {})
  copied.compare_by_identity

  # if we've already made a copy, return it.
  return copied[obj] if copied[obj]

  result = clone_obj(obj)

  # Memoize the cloned object before recursive calls to #deep_copy below.
  # This is the point of doing the work in two steps.
  copied[obj] = result

  case obj
  when ::Hash
    obj.each { |k, v| result[k] = deep_copy(v, copied) }
  when Array
    obj.each { |v| result << deep_copy(v, copied) }
  end

  result
end

.deep_merge(hash1, hash2, merged = {}) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/rollbar/util.rb', line 83

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

  # If we've already merged these two objects, return hash1 now.
  return hash1 if merged[hash1] && merged[hash1].include?(hash2.object_id)

  merged[hash1] ||= []
  merged[hash1] << hash2.object_id

  perform_deep_merge(hash1, hash2, merged)

  hash1
end

.enforce_valid_utf8(payload) ⇒ Object



123
124
125
126
127
# File 'lib/rollbar/util.rb', line 123

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

  Util.iterate_and_update(payload, normalizer)
end

.iterate_and_update(obj, block, seen = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/rollbar/util.rb', line 9

def self.iterate_and_update(obj, block, seen = {})
  seen.compare_by_identity
  return if obj.frozen? || seen[obj]

  seen[obj] = true

  if obj.is_a?(Array)
    iterate_and_update_array(obj, block, seen)
  else
    iterate_and_update_hash(obj, block, seen)
  end
end

.iterate_and_update_array(array, block, seen) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/rollbar/util.rb', line 22

def self.iterate_and_update_array(array, block, seen)
  array.each_with_index do |value, i|
    if value.is_a?(::Hash) || value.is_a?(Array)
      iterate_and_update(value, block, seen)
    else
      array[i] = block.call(value)
    end
  end
end

.iterate_and_update_hash(obj, block, seen) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rollbar/util.rb', line 32

def self.iterate_and_update_hash(obj, block, seen)
  obj.keys.each do |k| # rubocop:disable Style/HashEachMethods
    v = obj[k]
    new_key = block.call(k)

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

    if new_key != k
      obj[new_key] = obj[k]
      obj.delete(k)
    end
  end
end

.iterate_and_update_with_block(obj, &block) ⇒ Object



5
6
7
# File 'lib/rollbar/util.rb', line 5

def self.iterate_and_update_with_block(obj, &block)
  iterate_and_update(obj, block)
end

.method_in_stack(method_symbol, file_path = '') ⇒ Object



133
134
135
# File 'lib/rollbar/util.rb', line 133

def self.method_in_stack(method_symbol, file_path = '')
  count_method_in_stack(method_symbol, file_path) > 0
end

.method_in_stack_twice(method_symbol, file_path = '') ⇒ Object



137
138
139
# File 'lib/rollbar/util.rb', line 137

def self.method_in_stack_twice(method_symbol, file_path = '')
  count_method_in_stack(method_symbol, file_path) > 1
end

.perform_deep_merge(hash1, hash2, merged) ⇒ Object

rubocop:disable Metrics/AbcSize



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/rollbar/util.rb', line 99

def self.perform_deep_merge(hash1, hash2, merged) # rubocop:disable Metrics/AbcSize
  hash2.each_key do |k|
    if hash1[k].is_a?(::Hash) && hash2[k].is_a?(::Hash)
      hash1[k] = deep_merge(hash1[k], hash2[k], merged)
    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
end

.truncate(str, length) ⇒ Object



111
112
113
114
115
116
117
# File 'lib/rollbar/util.rb', line 111

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



119
120
121
# File 'lib/rollbar/util.rb', line 119

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