Class: Slack::Notifier::LinkFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/slack-notifier/link_formatter.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ LinkFormatter

Returns a new instance of LinkFormatter.



13
14
15
16
17
18
19
# File 'lib/slack-notifier/link_formatter.rb', line 13

def initialize string
  @orig = if string.respond_to? :scrub
    string.scrub
  else
    string
  end
end

Class Method Details

.format(string) ⇒ Object



7
8
9
# File 'lib/slack-notifier/link_formatter.rb', line 7

def format string
  LinkFormatter.new(string).formatted
end

Instance Method Details

#formattedObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/slack-notifier/link_formatter.rb', line 21

def formatted
  @orig.gsub( html_pattern ) do |match|
    link = Regexp.last_match[1]
    text = Regexp.last_match[2]
    slack_link link, text
  end.gsub( markdown_pattern ) do |match|
    link = Regexp.last_match[2]
    text = Regexp.last_match[1]
    slack_link link, text
  end

rescue => e
  if RUBY_VERSION < '2.1' && e.message.include?('invalid byte sequence')
    raise e, "#{e.message}. Consider including the 'string-scrub' gem to strip invalid characters"
  else
    raise e
  end
end