Class: ErrorappNotifier::Sanitizer

Inherits:
Object
  • Object
show all
Defined in:
lib/errorapp_notifier/sanitizer.rb

Class Method Summary collapse

Class Method Details

.filter_hash(keys_to_filter, hash) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/errorapp_notifier/sanitizer.rb', line 18

def self.filter_hash(keys_to_filter, hash)
  keys_to_filter.map!{ |key| key.to_s }
  if keys_to_filter.is_a?(Array) && !keys_to_filter.empty?
    hash.each do |key, value|
      if key_match?(key, keys_to_filter)
        hash[key] = '[FILTERED]'
      elsif value.respond_to?(:to_hash)
        filter_hash(keys_to_filter, hash[key])
      end
    end
  end
  hash
end

.sanitize_hash(hash) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/errorapp_notifier/sanitizer.rb', line 3

def self.sanitize_hash(hash)
  case hash
  when Hash
    hash.inject({}) do |result, (key, value)|
      result.update(key => sanitize_hash(value))
    end
  when Array
    hash.collect{|value| sanitize_hash(value)}
  when Fixnum, String, Bignum
    hash
  else
    hash.to_s
  end
end

.sanitize_session(request) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/errorapp_notifier/sanitizer.rb', line 32

def self.sanitize_session(request)
  session_hash = {'session_id' => "", 'data' => {}}

  if request.respond_to?(:session)
    session = request.session
    session_hash['session_id'] = request.session_options ? request.session_options[:id] : nil
    session_hash['session_id'] ||= session.respond_to?(:session_id) ? session.session_id : session.instance_variable_get("@session_id")
    session_hash['data'] = session.respond_to?(:to_hash) ? session.to_hash : session.instance_variable_get("@data") || {}
    session_hash['session_id'] ||= session_hash['data'][:session_id]
    session_hash['data'].delete(:session_id)
  end

  sanitize_hash(session_hash)
end