Class: Deidentify::HashUrl

Inherits:
Object
  • Object
show all
Defined in:
lib/deidentify/hash_url.rb

Class Method Summary collapse

Class Method Details

.calculate_hash_length(uri, length) ⇒ Object



21
22
23
24
25
# File 'lib/deidentify/hash_url.rb', line 21

def self.calculate_hash_length(uri, length)
  number_of_hashes = [uri.host, uri.path, uri.query, uri.fragment].reject(&:blank?).size

  (length - 'https:///?#'.length) / number_of_hashes
end

.call(old_url, length: 255) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/deidentify/hash_url.rb', line 5

def self.call(old_url, length: 255)
  return old_url unless old_url.present?

  uri = URI.parse(old_url)
  uri = URI.parse("http://#{old_url}") if uri.scheme.nil?

  hash_length = calculate_hash_length(uri, length)

  hash_host(uri, hash_length)
  hash_path(uri, hash_length)
  hash_query(uri, hash_length)
  hash_fragment(uri, hash_length)

  uri.to_s
end

.hash_fragment(uri, hash_length) ⇒ Object



43
44
45
# File 'lib/deidentify/hash_url.rb', line 43

def self.hash_fragment(uri, hash_length)
  uri.fragment = Deidentify::BaseHash.call(uri.fragment, length: hash_length) if uri.fragment.present?
end

.hash_host(uri, hash_length) ⇒ Object



27
28
29
# File 'lib/deidentify/hash_url.rb', line 27

def self.hash_host(uri, hash_length)
  uri.host = Deidentify::BaseHash.call(uri.host, length: hash_length)
end

.hash_path(uri, hash_length) ⇒ Object



31
32
33
# File 'lib/deidentify/hash_url.rb', line 31

def self.hash_path(uri, hash_length)
  uri.path = "/#{Deidentify::BaseHash.call(remove_slash(uri.path), length: hash_length)}" if uri.path.present?
end

.hash_query(uri, hash_length) ⇒ Object



39
40
41
# File 'lib/deidentify/hash_url.rb', line 39

def self.hash_query(uri, hash_length)
  uri.query = Deidentify::BaseHash.call(uri.query, length: hash_length) if uri.query.present?
end

.remove_slash(path) ⇒ Object



35
36
37
# File 'lib/deidentify/hash_url.rb', line 35

def self.remove_slash(path)
  path[1..]
end