class Slack::Notifier::LinkFormatter

Public Class Methods

format(string) click to toggle source
# File lib/slack-notifier/link_formatter.rb, line 7
def format string
  LinkFormatter.new(string).formatted
end
new(string) click to toggle source
# File lib/slack-notifier/link_formatter.rb, line 13
def initialize string
  @orig = if string.respond_to? :scrub
    string.scrub
  else
    string
  end
end

Public Instance Methods

formatted() click to toggle source
# 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

Private Instance Methods

html_pattern() click to toggle source

rubular.com/r/19cNXW5qbH

# File lib/slack-notifier/link_formatter.rb, line 51
def html_pattern
  / <a (?:.*?) href=['"](.+?)['"] (?:.*?)> (.+?) <\/a> /x
end
markdown_pattern() click to toggle source

rubular.com/r/guJbTK6x1f

# File lib/slack-notifier/link_formatter.rb, line 56
def markdown_pattern
  /\[ ([^\[\]]*?) \] \( ((https?:\/\/.*?) | (mailto:.*?)) \) /x
end