From eb0fe84f25d70ffd1706c4c644d7c97c33b26658 Mon Sep 17 00:00:00 2001 From: Dan Loewenherz Date: Sat, 10 Nov 2012 10:15:55 -0800 Subject: [PATCH] add shorten option for link shortening, closes #117 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. --- lib/gist.rb | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/lib/gist.rb b/lib/gist.rb index d0be256..f2562aa 100644 --- a/lib/gist.rb +++ b/lib/gist.rb @@ -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']) @@ -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| @@ -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 @@ -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 @@ -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 @@ -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) @@ -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