-
Notifications
You must be signed in to change notification settings - Fork 96
/
random_url.rb
36 lines (29 loc) · 993 Bytes
/
random_url.rb
1
2
3
4
5
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
module DataAnon
module Strategy
module Field
# Generates a randomized URL while maintaining the structure of the original url
#
# !!!ruby
# anonymize('fb_profile').using FieldStrategy::RandomURL.new
class RandomUrl
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
end
end
end
end