Sha256: d21c29811bad8ab3afacfc4405bf94c5cbea80bb2b87244425fcde1a164cc279

Contents?: true

Size: 1006 Bytes

Versions: 2

Compression:

Stored size: 1006 Bytes

Contents

module CommitMsgUrlShortener
  module ServiceHelper
    extend self
    require 'net/http'
    require 'net/https'
    require 'json'

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

    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

    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
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
commit-msg-url-shortener-0.0.2 lib/commit-msg-url-shortener/service_helper.rb
commit-msg-url-shortener-0.0.1 lib/commit-msg-url-shortener/service_helper.rb