Class: WordCountAnalyzer::Hyperlink
- Inherits:
-
Object
- Object
- WordCountAnalyzer::Hyperlink
- Defined in:
- lib/word_count_analyzer/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.
- #occurences ⇒ Object
- #replace ⇒ Object
- #replace_split_at_period ⇒ Object
Constructor Details
#initialize(string:) ⇒ Hyperlink
Returns a new instance of Hyperlink.
9 10 11 |
# File 'lib/word_count_analyzer/hyperlink.rb', line 9 def initialize(string:) @string = string end |
Instance Attribute Details
#string ⇒ Object (readonly)
Returns the value of attribute string.
8 9 10 |
# File 'lib/word_count_analyzer/hyperlink.rb', line 8 def string @string end |
Instance Method Details
#hyperlink? ⇒ Boolean
13 14 15 |
# File 'lib/word_count_analyzer/hyperlink.rb', line 13 def hyperlink? !(string !~ URI.regexp) && string !~ NON_HYPERLINK_REGEX && !(string !~ HYPERLINK_REGEX) end |
#occurences ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/word_count_analyzer/hyperlink.rb', line 17 def occurences counter = 0 string.scan(URI.regexp).each do |link| counter += 1 if link.compact.size > 1 end counter end |
#replace ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/word_count_analyzer/hyperlink.rb', line 25 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])}/, ' wslinkword ') elsif !(token !~ URI.regexp) && token !~ NON_HYPERLINK_REGEX && !(token !~ HYPERLINK_REGEX) new_string = new_string.gsub(/#{Regexp.escape(token)}/, ' wslinkword ') end end new_string end |
#replace_split_at_period ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/word_count_analyzer/hyperlink.rb', line 37 def replace_split_at_period 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.gsub!(/#{Regexp.escape(token.split('">')[0])}/) do |match| match.split('.').join(' ') end elsif !(token !~ URI.regexp) && token !~ NON_HYPERLINK_REGEX && !(token !~ HYPERLINK_REGEX) new_string.gsub!(/#{Regexp.escape(token)}/) do |match| match.split('.').join(' ') end end end new_string end |