Module: Rollbar::Util::IPAnonymizer

Defined in:
lib/rollbar/util/ip_anonymizer.rb

Class Method Summary collapse

Class Method Details

.anonymize_ip(ip_string) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/rollbar/util/ip_anonymizer.rb', line 6

def self.anonymize_ip(ip_string)
  return ip_string unless Rollbar.configuration.anonymize_user_ip

  ip = IPAddr.new(ip_string)
  return anonymize_ipv6 ip if ip.ipv6?
  return anonymize_ipv4 ip if ip.ipv4?
rescue StandardError
  nil
end

.anonymize_ipv4(ip) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/rollbar/util/ip_anonymizer.rb', line 16

def self.anonymize_ipv4(ip)
  ip_parts = ip.to_s.split '.'

  ip_parts[ip_parts.count - 1] = '0'

  IPAddr.new(ip_parts.join('.')).to_s
end

.anonymize_ipv6(ip) ⇒ Object



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

def self.anonymize_ipv6(ip)
  ip_parts = ip.to_s.split ':'

  ip_string = "#{ip_parts[0..2].join(':')}:0000:0000:0000:0000:0000"

  IPAddr.new(ip_string).to_s
end