Skip to content

Commit

Permalink
add shorten option for link shortening, closes #117
Browse files Browse the repository at this point in the history
This commit gives gist the new ability to shorten gist URLs using
GitHub's URL shortener, git.io. It adds two options on the command
line (`-s` and `--shorten`) along with a gitconfig config called
`shorten`.

If the shortening step fails, or if git.io is down, gist will just
return the normal, unshortened version.
  • Loading branch information
dlo committed Nov 10, 2012
1 parent 3aacc1f commit eb0fe84
Showing 1 changed file with 31 additions and 6 deletions.
37 changes: 31 additions & 6 deletions lib/gist.rb
Expand Up @@ -26,8 +26,9 @@
module Gist
extend self

GIST_URL = 'https://api.github.com/gists/%s'
CREATE_URL = 'https://api.github.com/gists'
GIST_URL = 'https://api.github.com/gists/%s'
CREATE_URL = 'https://api.github.com/gists'
SHORTEN_URL = 'http://git.io/'

if ENV['HTTPS_PROXY']
PROXY = URI(ENV['HTTPS_PROXY'])
Expand All @@ -45,6 +46,7 @@ def execute(*args)
gist_filename = nil
gist_extension = defaults["extension"]
browse_enabled = defaults["browse"]
shorten = defaults["shorten"]
description = nil

opts = OptionParser.new do |opts|
Expand All @@ -68,6 +70,10 @@ def execute(*args)
browse_enabled = o
end

opts.on('-s','--shorten', 'Shorten gist URL') do |s|
shorten = s
end

opts.on('-m', '--man', 'Print manual') do
Gist::Manpage.display("gist")
end
Expand Down Expand Up @@ -113,7 +119,7 @@ def execute(*args)
files = [{:input => input, :extension => gist_extension}]
end

url = write(files, private_gist, description)
url = write(files, private_gist, description, shorten)
browse(url) if browse_enabled
puts copy(url)
rescue => e
Expand All @@ -123,7 +129,7 @@ def execute(*args)
end

# Create a gist on gist.github.com
def write(files, private_gist = false, description = nil)
def write(files, private_gist = false, description = nil, shorten = false)
url = URI.parse(CREATE_URL)

if PROXY_HOST
Expand All @@ -148,13 +154,30 @@ def write(files, private_gist = false, description = nil)
response = http.start{|h| h.request(req) }
case response
when Net::HTTPCreated
JSON.parse(response.body)['html_url']
url = JSON.parse(response.body)['html_url']
if shorten
shorten(url)
else
url
end
else
puts "Creating gist failed: #{response.code} #{response.message}"
exit(false)
end
end

# Given a URL, shorten it
def shorten(url)
response = Net::HTTP.post_form(URI(SHORTEN_URL), :url => url)
case response.code
when "201"
response['Location']
else
# If the shortener failed, just return the unshortened URL.
url
end
end

# Given a gist id, returns its content.
def read(gist_id)
data = JSON.parse(open(GIST_URL % gist_id).read)
Expand Down Expand Up @@ -238,13 +261,15 @@ def auth
#
# gist.private - boolean
# gist.extension - string
# gist.shorten - boolean
def defaults
extension = config("gist.extension")

return {
"private" => config("gist.private"),
"browse" => config("gist.browse"),
"extension" => extension
"extension" => extension,
"shorten" => config("gist.shorten"),
}
end

Expand Down

0 comments on commit eb0fe84

Please sign in to comment.