Module: CommitMsgUrlShortener::ServiceHelper

Extended by:
ServiceHelper
Included in:
ServiceHelper
Defined in:
lib/commit-msg-url-shortener/service_helper.rb

Instance Method Summary collapse

Instance Method Details

#each_long_url(text, &block) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/commit-msg-url-shortener/service_helper.rb', line 23

def each_long_url text, &block
  urls = extract_urls text
  return text if urls.empty?
  new_text = text
  urls.each do |url|
    short_url = yield(url)
    unless short_url
      new_text = nil
      break
    end
    new_text = new_text.gsub(url, short_url)
  end
  new_text
end

#error_safe_request(&block) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/commit-msg-url-shortener/service_helper.rb', line 13

def error_safe_request &block
  connection_error_message = "#{CommitMsgUrlShortener::GEM_NAME} connection error! Maybe you're offline?\n"+
                             "In this case you should disable #{CommitMsgUrlShortener::GEM_NAME}."
  yield
rescue Timeout::Error => e
  puts connection_error_message
rescue SocketError => e
  puts connection_error_message
end

#extract_urls(text) ⇒ Object



8
9
10
11
# File 'lib/commit-msg-url-shortener/service_helper.rb', line 8

def extract_urls text
  regex = /(http[s]?\:\/\/[a-zA-Z0-9\-\.]+\.[\S+]+)/
  text.scan(regex).flatten
end