Skip to content

Commit

Permalink
add link shortening option
Browse files Browse the repository at this point in the history
This commit gives jist 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`).

If the shortening step fails, or if git.io is down, jist will just
return the normal, unshortened version.
  • Loading branch information
dlo committed Nov 10, 2012
1 parent c8a5851 commit 8251a77
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
9 changes: 8 additions & 1 deletion bin/jist
Expand Up @@ -29,6 +29,9 @@ Anonymous gists are not associated with your GitHub account, they can be created
with "-a" even after you have used "jist --login". If you already have an access
token with the "gist" scope, you can pass that with "-t".
If you would like to shorten the resulting gist URL, use the -s flag. This will
use GitHub's URL shortener, git.io.
To copy the resulting URL to your clipboard you can use the -c option, or to just
open it directly in your browser, use -o. You can add `alias jist='jist -c'` to
your shell's rc file to configure this behaviour by default.
Expand All @@ -37,7 +40,7 @@ Instead of creating a new jist, you can update an existing one by passing its ID
or URL with "-u". For this to work, you must be logged in, and have created the
original gist with the same GitHub account.
Usage: #{executable_name} [-o|-c] [-p] [-d DESC] [-t TOKEN|-a] [-u URL] [-P] [-f NAME]* FILE*
Usage: #{executable_name} [-o|-c] [-p] [-s] [-d DESC] [-t TOKEN|-a] [-u URL] [-P] [-f NAME]* FILE*
#{executable_name} --login
EOS
Expand Down Expand Up @@ -68,6 +71,10 @@ Usage: #{executable_name} [-o|-c] [-p] [-d DESC] [-t TOKEN|-a] [-u URL] [-P] [-f
options[:anonymous] = true
end

opts.on("-s", "--shorten", "Shorten the gist URL using git.io.") do |shorten|
options[:shorten] = shorten
end

opts.on("-t", "--token OAUTH_TOKEN", "The OAuth2 access_token to use.") do |token|
options[:access_token] = token
end
Expand Down
23 changes: 22 additions & 1 deletion lib/jist.rb
Expand Up @@ -28,6 +28,7 @@ module Error; end
# @option options [String] :filename ('a.rb') the filename
# @option options [Boolean] :public (false) is this gist public
# @option options [Boolean] :anonymous (false) is this gist anonymous
# @option options [Boolean] :shorten (false) Shorten the resulting URL using git.io.
# @option options [String] :access_token (`File.read("~/.jist")`) The OAuth2 access token.
# @option options [String] :update the URL or id of a gist to update
# @option options [Boolean] :copy (false) Copy resulting URL to clipboard, if successful.
Expand All @@ -50,6 +51,7 @@ def gist(content, options = {})
# @option options [String] :description the description
# @option options [Boolean] :public (false) is this gist public
# @option options [Boolean] :anonymous (false) is this gist anonymous
# @option options [Boolean] :shorten (false) Shorten the resulting URL using git.io.
# @option options [String] :access_token (`File.read("~/.jist")`) The OAuth2 access token.
# @option options [String] :update the URL or id of a gist to update
# @option options [Boolean] :copy (false) Copy resulting URL to clipboard, if successful.
Expand Down Expand Up @@ -91,7 +93,12 @@ def multi_gist(files, options={})
begin
response = http(request)
if Net::HTTPSuccess === response
on_success(response.body, options)
response = on_success(response.body, options)
if options[:shorten]
{'html_url' => shorten(response['html_url'])}
else
response
end
else
raise "Got #{response.class} from gist: #{response.body}"
end
Expand All @@ -105,6 +112,20 @@ def multi_gist(files, options={})
raise e.extend Error
end

# Given a URL, shorten it
#
# Based on https://gist.github.com/1762136
def shorten(url)
response = Net::HTTP.post_form(URI("http://git.io/"), :url => url)
case response.code
when "201"
response['Location']
else
# If the shortener failed, just return the unshortened URL.
url
end
end

# Log the user into jist.
#
# This method asks the user for a username and password, and tries to obtain
Expand Down

0 comments on commit 8251a77

Please sign in to comment.