Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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
-------

Expand Down
37 changes: 31 additions & 6 deletions lib/gist.rb
Original file line number Diff line number Diff line change
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
7 changes: 7 additions & 0 deletions man/gist.1
Original file line number Diff line number Diff line change
Expand Up @@ -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
.
Expand Down Expand Up @@ -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"
Expand Down
11 changes: 7 additions & 4 deletions man/gist.1.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions man/gist.1.ron
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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`:
Expand Down