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 Method Summary collapse
- #hyperlink?(text) ⇒ Boolean
- #occurrences(text) ⇒ Object
- #replace(text) ⇒ Object
- #replace_split_at_period(text) ⇒ Object
Instance Method Details
#hyperlink?(text) ⇒ Boolean
10 11 12 |
# File 'lib/word_count_analyzer/hyperlink.rb', line 10 def hyperlink?(text) !(text !~ URI.regexp) && text !~ NON_HYPERLINK_REGEX && !(text !~ HYPERLINK_REGEX) end |
#occurrences(text) ⇒ Object
14 15 16 |
# File 'lib/word_count_analyzer/hyperlink.rb', line 14 def occurrences(text) text.scan(URI.regexp).map { |link| link.compact.size > 1 ? 1 : 0 }.inject(0) { |sum, x| sum + x } end |
#replace(text) ⇒ Object
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/word_count_analyzer/hyperlink.rb', line 18 def replace(text) text.split(/\s+/).each do |token| if !(token !~ URI.regexp) && token !~ NON_HYPERLINK_REGEX && !(token !~ HYPERLINK_REGEX) && token.include?('">') text = text.gsub(/#{Regexp.escape(token.split('">')[0])}/, ' wslinkword ') elsif !(token !~ URI.regexp) && token !~ NON_HYPERLINK_REGEX && !(token !~ HYPERLINK_REGEX) text = text.gsub(/#{Regexp.escape(token)}/, ' wslinkword ') end end text end |
#replace_split_at_period(text) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/word_count_analyzer/hyperlink.rb', line 29 def replace_split_at_period(text) text.split(/\s+/).each do |token| if !(token !~ URI.regexp) && token !~ NON_HYPERLINK_REGEX && !(token !~ HYPERLINK_REGEX) && token.include?('">') text.gsub!(/#{Regexp.escape(token.split('">')[0])}/) do |match| match.split('.').join(' ') end elsif !(token !~ URI.regexp) && token !~ NON_HYPERLINK_REGEX && !(token !~ HYPERLINK_REGEX) text.gsub!(/#{Regexp.escape(token)}/) do |match| match.split('.').join(' ') end end end text end |