Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add link shortening with --shorten / -s option #120

Closed
wants to merge 2 commits into from

1 participant

@dlo

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.

dlo added some commits
@dlo dlo 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.
eb0fe84
@dlo dlo add documentation for new --shorten command 821578c
@ConradIrwin ConradIrwin closed this pull request from a commit
@ConradIrwin ConradIrwin Merge jist with gist!
After some time as a fork, and a lot of vetting from users, we have
decided to merge the projects together.

The key change is to use OAuth tokens for authentication instead of
requiring you to hard-code your password. Please use `gist --login`
to exchange your username and password for a token.

Close #137  Gist API now requires user-agent be set, and some versions of Net::HTTP don't 3 ↑
Close #136  SSL error (w/ Homebrew & Ruby 2.0.0-p0 from rbenv)
Close #135  Moved authentication over to OAuth tokens. 1 ↑
Close #134  Trying to use gist inside a directory with dashes in the name on a file inside a subdirectory of that results in 422 errors.... 3
Close #133  SSL cert cached in /tmp
Close #132  --open doesn't works
Close #131  Replace slashes with dashes to avoid 422 errors ↑
Close #130  FEATURE: Multiple API Providers 2 ↑
Close #125  Preventing gist from throwing errors when xclip is installed but X11 isn't present (forwarding or otherwise) ↑
Close #120  Add link shortening with --shorten / -s option ↑
Close #118  Use tokens instead of username / password 4
Close #117  Integrate with git.io
Close #116  man page typos & feature request
Close #115  Creating gist failed: 500 Internal Server Error 1
Close #112  Refactor reading from stdin so --type option works 3 ↑
Close #111  Allow gist to work against different githubs 2 ↑
Close #109  fix error on windows. ↑
Close #108  set Content-Type. 5 ↑
Close #107  Add update support. 1 ↑
Close #106  Add anonymous gisting. 1 ↑
Close #103  fix some bugs.  make gist rock in vim/editor via "highlight lines - :!gist" ↑
Close #98  SSL... 23
Close #96  Creating gist failed: 422 Unprocessable Entity 2
Close #95  Fixes the extension issue for -t syntax highlighting, and the filename for stdin 5 ↑
Close #94  show JSON response when create fails ↑
Close #93  use file basename by default 4 ↑
Close #91  Support using an oauth token rather than a username+password 7 ↑
Close #90  3.1.0 fails to authenticate 3
Close #88  Current (3.1.0) no longer respects -t flag 3
Close #85  Copy to clipboard in Windows ↑
Close #84  use oauth now that there are no tokens 14
Close #78  Strip directory from arguments 2
Close #76  Some README.markdown nitpicks...
Close #75  Does not open in browser by default — documentation disagrees 3
Close #74  Set description from CLI (for issue #73) 1 ↑
Close #73  Should be able to set gist description from commandline 1
Close #69  Failed gist creation should raise instead of puts and exit.
Close #67  Use the github api and allow gist url override. 1 ↑
Close #65  Handle lowercased versions of HTTP_PROXY and HTTPS_PROXY. 1 ↑
Close #64  When generating the man page (-m/--man), skip the usage instructions ↑
Close #61  Instructions for setting github.token to a command don't work for me 1
Close #58  support for anonymous gists
Close #53  Add support for GitHub:FI 4
Close #25  Support for updating gists [feature] 1
a05092f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 10, 2012
  1. @dlo

    add shorten option for link shortening, closes #117

    dlo authored
    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.
  2. @dlo
This page is out of date. Refresh to see the latest.
View
3  README.markdown
@@ -97,6 +97,9 @@ control the default behavior of gist(1).
* gist.browse - boolean (yes or no) - Whether to open the gist in your
browser after creation. Default: yes
+* gist.shorten - boolean (yes or no) - Determines whether to shorten the
+ resulting gist using http://git.io/. Default: no
+
Proxies
-------
View
37 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
View
7 man/gist.1
@@ -37,6 +37,10 @@ Set the file extension explicitly\. Passing a type of \fBrb\fR ensures the gist
Set a description\.
.
.TP
+\fB\-s\fR, \fB\-\-shorten\fR
+Shorten the resulting gist URL using http://git\.io/\.
+.
+.TP
\fB\-o\fR, \fB\-\-[no\-]open\fR
Open the gist in your browser after creation\. Or don\'t\. Defaults to \-\-open
.
@@ -112,6 +116,9 @@ gist\.extension \- string \- Default extension for gists you create\.
.IP "\(bu" 4
gist\.browse \- boolean (yes or no) \- Whether to open the gist in your browser after creation\. Default: yes
.
+.IP "\(bu" 4
+gist\.shorten \- boolean (yes or no) \- Determines whether to shorten the resulting gist using http://git\.io/\. Default: no
+.
.IP "" 0
.
.SH "ENVIRONMENT"
View
11 man/gist.1.html
@@ -109,6 +109,7 @@ <h2 id="OPTIONS">OPTIONS</h2>
<dt><code>-t</code>, <code>--type</code></dt><dd><p>Set the file extension explicitly. Passing a type of <code>rb</code> ensures
the gist is created as a Ruby file.</p></dd>
<dt><code>-d</code>, <code>--description</code></dt><dd><p>Set a description.</p></dd>
+<dt><code>-s</code>, <code>--shorten</code></dt><dd><p>Shorten the resulting gist URL using http://git.io/.</p></dd>
<dt><code>-o</code>, <code>--[no-]open</code></dt><dd><p>Open the gist in your browser after creation. Or don't. Defaults
to --open</p></dd>
</dl>
@@ -146,7 +147,7 @@ <h2 id="AUTHENTICATION">AUTHENTICATION</h2>
<pre><code>$ git config --global github.user &lt;username>
</code></pre>
-<p>See <a href="http://github.com/guides/local-github-config" data-bare-link="true">http://github.com/guides/local-github-config</a> for more
+<p>See <a data-bare-link="true" href="http://github.com/guides/local-github-config">http://github.com/guides/local-github-config</a> for more
information.</p>
<h2 id="CONFIGURATION">CONFIGURATION</h2>
@@ -160,6 +161,8 @@ <h2 id="CONFIGURATION">CONFIGURATION</h2>
<li><p>gist.extension - string - Default extension for gists you create.</p></li>
<li><p>gist.browse - boolean (yes or no) - Whether to open the gist in your
browser after creation. Default: yes</p></li>
+<li><p>gist.shorten - boolean (yes or no) - Determines whether to shorten the
+resulting gist using http://git.io/. Default: no</p></li>
</ul>
@@ -185,7 +188,7 @@ <h2 id="EXAMPLES">EXAMPLES</h2>
<h2 id="BUGS">BUGS</h2>
-<p><a href="http://github.com/defunkt/gist/issues" data-bare-link="true">http://github.com/defunkt/gist/issues</a></p>
+<p><a data-bare-link="true" href="http://github.com/defunkt/gist/issues">http://github.com/defunkt/gist/issues</a></p>
<h2 id="AUTHOR">AUTHOR</h2>
@@ -194,8 +197,8 @@ <h2 id="AUTHOR">AUTHOR</h2>
<h2 id="SEE-ALSO">SEE ALSO</h2>
<p><span class="man-ref">hub<span class="s">(1)</span></span>, <span class="man-ref">git<span class="s">(1)</span></span>, <span class="man-ref">git-clone<span class="s">(1)</span></span>,
-<a href="http://github.com" data-bare-link="true">http://github.com</a>,
-<a href="http://github.com/defunkt/gist" data-bare-link="true">http://github.com/defunkt/gist</a></p>
+<a data-bare-link="true" href="http://github.com">http://github.com</a>,
+<a data-bare-link="true" href="http://github.com/defunkt/gist">http://github.com/defunkt/gist</a></p>
<ol class='man-decor man-foot man foot'>
View
6 man/gist.1.ron
@@ -38,6 +38,9 @@ These options can be used to change this behavior:
* `-d`, `--description`:
Set a description.
+ * `-s`, `--shorten`:
+ Shorten the resulting gist URL using http://git.io/.
+
* `-o`, `--[no-]open`:
Open the gist in your browser after creation. Or don't. Defaults
to --open
@@ -86,6 +89,9 @@ control the default behavior of gist(1).
* gist.browse - boolean (yes or no) - Whether to open the gist in your
browser after creation. Default: yes
+* gist.shorten - boolean (yes or no) - Determines whether to shorten the
+ resulting gist using http://git.io/. Default: no
+
## ENVIRONMENT
The following environment variables affect the execution of `gist`:
Something went wrong with that request. Please try again.