Class: Codebot::Shortener::Github

Inherits:
Object
  • Object
show all
Defined in:
lib/codebot/shortener.rb

Overview

Shortens URLs using Github shortener

Instance Method Summary collapse

Instance Method Details

#shorten_url(url) ⇒ String

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

Parameters:

  • url (String)

    the long URL

Returns:

  • (String)

    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