Class: StringTools::HTML::LinksRemoveScrubber
- Inherits:
-
Object
- Object
- StringTools::HTML::LinksRemoveScrubber
- Defined in:
- lib/string_tools/html.rb
Instance Method Summary collapse
- #call(node) ⇒ Object
- #done_changes? ⇒ Boolean
-
#initialize(options) ⇒ LinksRemoveScrubber
constructor
A new instance of LinksRemoveScrubber.
- #whitelisted?(domain) ⇒ Boolean
Constructor Details
#initialize(options) ⇒ LinksRemoveScrubber
Returns a new instance of LinksRemoveScrubber.
57 58 59 60 61 |
# File 'lib/string_tools/html.rb', line 57 def initialize() @whitelist = .fetch(:whitelist) @remove_without_host = .fetch(:remove_without_host, true) @is_have_done_changes = false end |
Instance Method Details
#call(node) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/string_tools/html.rb', line 67 def call(node) href = node['href'] return if href.blank? uri = Addressable::URI.parse(href).normalize if !uri.host replace_with_content node if @remove_without_host elsif !whitelisted?(SimpleIDN.to_unicode(uri.host)) replace_with_content node end rescue Addressable::URI::InvalidURIError replace_with_content node end |
#done_changes? ⇒ Boolean
63 64 65 |
# File 'lib/string_tools/html.rb', line 63 def done_changes? @is_have_done_changes end |
#whitelisted?(domain) ⇒ Boolean
80 81 82 83 84 85 86 87 88 89 |
# File 'lib/string_tools/html.rb', line 80 def whitelisted?(domain) host_parts = domain.split('.'.freeze) host = host_parts[-1] # com, ru ... (host_parts.length - 2).downto(0) do |i| subdomain = host_parts[i] host = "#{subdomain}.#{host}" return true if @whitelist.include? host end false end |