Method: Codebot::Shortener::Github#shorten_url

Defined in:
lib/codebot/shortener.rb

#shorten_url(url) ⇒ String

Shortens a URL with GitHub’s git.io URL shortener. The domain must belong to GitHub.

Parameters:

  • the long URL

Returns:

  • the shortened URL, or the original URL if an error occurred.



13
14
15
16
17
18
19
20
21
# File 'lib/codebot/shortener.rb', line 13

def shorten_url(url)
  return url if url.to_s.empty?

  uri = URI('https://git.io')
  res = Net::HTTP.post_form uri, 'url' => url.to_s
  res['location'] || url.to_s
rescue StandardError
  url.to_s
end