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.



11
12
13
# File 'lib/word_count_analyzer/hyperlink.rb', line 11

def initialize(string:)
  @string = string
end

Instance Attribute Details

#stringObject (readonly)

Returns the value of attribute string.



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

def string
  @string
end

Instance Method Details

#hyperlink?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/word_count_analyzer/hyperlink.rb', line 15

def hyperlink?
  !(string !~ URI.regexp) && string !~ NON_HYPERLINK_REGEX && !(string !~ HYPERLINK_REGEX)
end

#occurencesObject



19
20
21
22
23
24
25
# File 'lib/word_count_analyzer/hyperlink.rb', line 19

def occurences
  counter = 0
  string.scan(URI.regexp).each do |link|
    counter += 1 if link.compact.size > 1
  end
  counter
end

#replaceObject



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/word_count_analyzer/hyperlink.rb', line 27

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



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/word_count_analyzer/hyperlink.rb', line 39

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