Class: ConfidentialInfoRedactor::Hyperlink

Inherits:
Object
  • Object
show all
Defined in:
lib/confidential_info_redactor/hyperlink.rb

Constant Summary collapse

/\A\w+:$/
/(http|https|www)(\.|:)/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string:) ⇒ Hyperlink

Returns a new instance of Hyperlink.



11
12
13
# File 'lib/confidential_info_redactor/hyperlink.rb', line 11

def initialize(string:)
  @string = string
end

Instance Attribute Details

#stringObject (readonly)

Returns the value of attribute string.



10
11
12
# File 'lib/confidential_info_redactor/hyperlink.rb', line 10

def string
  @string
end

Instance Method Details

#hyperlink?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/confidential_info_redactor/hyperlink.rb', line 15

def hyperlink?
  !(string !~ URI.regexp) && string !~ NON_HYPERLINK_REGEX && !(string !~ HYPERLINK_REGEX)
end

#replaceObject



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/confidential_info_redactor/hyperlink.rb', line 19

def replace
  new_string = string.dup
  string.split(/\s+/).each do |token|
    if !(token !~ URI.regexp) && token !~ NON_HYPERLINK_REGEX && !(token !~ HYPERLINK_REGEX) && token.include?('">')
      new_string = new_string.gsub(/#{Regexp.escape(token.split('">')[0].gsub(/\.\z/, ''))}/, ' <redacted> ')
    elsif !(token !~ URI.regexp) && token !~ NON_HYPERLINK_REGEX && !(token !~ HYPERLINK_REGEX)
      new_string = new_string.gsub(/#{Regexp.escape(token.gsub(/\.\z/, ''))}/, ' <redacted> ')
    end
  end
  new_string
end