Class: ConfidentialInfoRedactor::Hyperlink
- Inherits:
-
Object
- Object
- ConfidentialInfoRedactor::Hyperlink
- Defined in:
- lib/confidential_info_redactor/hyperlink.rb
Constant Summary collapse
- NON_HYPERLINK_REGEX =
/\A\w+:$/
- HYPERLINK_REGEX =
Rubular: rubular.com/r/fXa4lp0gfS
/(http|https|www)(\.|:)/
Instance Attribute Summary collapse
-
#string ⇒ Object
readonly
Returns the value of attribute string.
Instance Method Summary collapse
- #hyperlink? ⇒ Boolean
-
#initialize(string:) ⇒ Hyperlink
constructor
A new instance of Hyperlink.
- #replace ⇒ Object
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
#string ⇒ Object (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
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 |
#replace ⇒ Object
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 |