Skip to content

Commit 58d091b

Browse files
author
sam7wx
committed
Added an option to set description
1 parent a116fb0 commit 58d091b

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

lib/gist.rb

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ def execute(*args)
4242
gist_filename = nil
4343
gist_extension = defaults["extension"]
4444
browse_enabled = defaults["browse"]
45+
description = nil
4546

4647
opts = OptionParser.new do |opts|
4748
opts.banner = "Usage: gist [options] [filename or stdin] [filename] ...\n" +
@@ -56,6 +57,10 @@ def execute(*args)
5657
gist_extension = '.' + extension
5758
end
5859

60+
opts.on('-d','--description DESCRIPTION', 'Set description of the new gist') do |d|
61+
description = d
62+
end
63+
5964
opts.on('-o','--[no-]open', 'Open gist in browser') do |o|
6065
browse_enabled = o
6166
end
@@ -104,7 +109,7 @@ def execute(*args)
104109
files = [{:input => input, :extension => gist_extension}]
105110
end
106111

107-
url = write(files, private_gist)
112+
url = write(files, private_gist, description)
108113
browse(url) if browse_enabled
109114
puts copy(url)
110115
rescue => e
@@ -114,7 +119,7 @@ def execute(*args)
114119
end
115120

116121
# Create a gist on gist.github.com
117-
def write(files, private_gist = false)
122+
def write(files, private_gist = false, description = nil)
118123
url = URI.parse(CREATE_URL)
119124

120125
if PROXY_HOST
@@ -129,7 +134,7 @@ def write(files, private_gist = false)
129134
http.ca_file = ca_cert
130135

131136
req = Net::HTTP::Post.new(url.path)
132-
req.form_data = data(files, private_gist)
137+
req.form_data = data(files, private_gist, description)
133138

134139
response = http.start{|h| h.request(req) }
135140
case response
@@ -180,14 +185,15 @@ def copy(content)
180185
private
181186
# Give an array of file information and private boolean, returns
182187
# an appropriate payload for POSTing to gist.github.com
183-
def data(files, private_gist)
188+
def data(files, private_gist, description)
184189
data = {}
185190
files.each do |file|
186191
i = data.size + 1
187192
data["file_ext[gistfile#{i}]"] = file[:extension] ? file[:extension] : '.txt'
188193
data["file_name[gistfile#{i}]"] = file[:filename]
189194
data["file_contents[gistfile#{i}]"] = file[:input]
190195
end
196+
data.merge!({ 'description' => description }) unless description.nil?
191197
data.merge(private_gist ? { 'action_button' => 'private' } : {}).merge(auth)
192198
end
193199

0 commit comments

Comments
 (0)