Class: WordCountAnalyzer::Hyperlink

Inherits:
Object
  • Object
show all
Defined in:
lib/word_count_analyzer/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.



9
10
11
# File 'lib/word_count_analyzer/hyperlink.rb', line 9

def initialize(string:)
  @string = string
end

Instance Attribute Details

#stringObject (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

Returns:

  • (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

#occurencesObject



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

#replaceObject



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_periodObject



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