Class: DataAnon::Strategy::Field::RandomUrl

Inherits:
Object
  • Object
show all
Defined in:
lib/strategy/field/string/random_url.rb

Overview

Generates a randomized URL while maintaining the structure of the original url

anonymize('fb_profile').using FieldStrategy::RandomUrl.new

Instance Method Summary collapse

Instance Method Details

#anonymize(field) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/strategy/field/string/random_url.rb', line 12

def anonymize field

  url = field.value
  randomized_url = ""
  protocols = url.scan(/https?:\/\/|www\./)
  protocols.each do |token|
    url = url.gsub(token,"")
    randomized_url += token
  end

  marker_position = 0

  while marker_position < url.length
    special_char_index = url.index(/\W/, marker_position) || url.length
    text = url[marker_position...special_char_index]
    randomized_url += "#{DataAnon::Utils::RandomStringCharsOnly.generate(text.length)}#{url[special_char_index]}"
    marker_position = special_char_index + 1
  end

  randomized_url
end