Skip to content

Commit 528ebb2

Browse files
committed
typos, also fix reading
1 parent 30613ab commit 528ebb2

File tree

4 files changed

+73
-54
lines changed

4 files changed

+73
-54
lines changed

gist

Lines changed: 57 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,18 @@ require 'open-uri'
8686
require 'net/https'
8787
require 'optparse'
8888

89+
require 'rubygems'
90+
require 'json'
91+
require 'base64'
92+
8993
require 'gist/manpage' unless defined?(Gist::Manpage)
9094
require 'gist/version' unless defined?(Gist::Version)
9195

9296
module Gist
9397
extend self
9498

95-
GIST_URL = 'https://gist.github.com/%s.txt'
96-
CREATE_URL = 'https://gist.github.com/gists'
99+
GIST_URL = 'https://api.github.com/gists/%s'
100+
CREATE_URL = 'https://api.github.com/gists'
97101

98102
if ENV['HTTPS_PROXY']
99103
PROXY = URI(ENV['HTTPS_PROXY'])
@@ -110,7 +114,7 @@ module Gist
110114
gist_filename = nil
111115
gist_extension = defaults["extension"]
112116
browse_enabled = defaults["browse"]
113-
copy = defaults["copy"]
117+
description = nil
114118

115119
opts = OptionParser.new do |opts|
116120
opts.banner = "Usage: gist [options] [filename or stdin] [filename] ...\n" +
@@ -125,6 +129,10 @@ module Gist
125129
gist_extension = '.' + extension
126130
end
127131

132+
opts.on('-d','--description DESCRIPTION', 'Set description of the new gist') do |d|
133+
description = d
134+
end
135+
128136
opts.on('-o','--[no-]open', 'Open gist in browser') do |o|
129137
browse_enabled = o
130138
end
@@ -142,15 +150,12 @@ module Gist
142150
puts opts
143151
exit
144152
end
145-
146-
opts.on('-c', '--[no-]copy', 'Copy gist URL to clipboard automatically') do |c|
147-
copy = c
148-
end
149153
end
150154

151-
opts.parse!(args)
152-
153155
begin
156+
157+
opts.parse!(args)
158+
154159
if $stdin.tty? && args[0] != '-'
155160

156161
if args.empty?
@@ -163,7 +168,7 @@ module Gist
163168

164169
files.push({
165170
:input => File.read(file),
166-
:filename => File.basename(file),
171+
:filename => file,
167172
:extension => (File.extname(file) if file.include?('.'))
168173
})
169174
end
@@ -173,17 +178,16 @@ module Gist
173178
files = [{:input => input, :extension => gist_extension}]
174179
end
175180

176-
url = write(files, private_gist)
181+
url = write(files, private_gist, description)
177182
browse(url) if browse_enabled
178-
copy(url) if copy
179-
$stdout.tty? ? puts(url) : print(url)
183+
puts copy(url)
180184
rescue => e
181185
warn e
182186
puts opts
183187
end
184188
end
185189

186-
def write(files, private_gist = false)
190+
def write(files, private_gist = false, description = nil)
187191
url = URI.parse(CREATE_URL)
188192

189193
if PROXY_HOST
@@ -198,25 +202,34 @@ module Gist
198202
http.ca_file = ca_cert
199203

200204
req = Net::HTTP::Post.new(url.path)
201-
req.form_data = data(files, private_gist)
205+
req.body = JSON.generate(data(files, private_gist, description))
206+
p req.body
207+
208+
if auth_header = auth()
209+
req.add_field('Authorization', auth_header)
210+
end
202211

203212
response = http.start{|h| h.request(req) }
204213
case response
205-
when Net::HTTPRedirection
206-
response['Location']
214+
when Net::HTTPCreated
215+
JSON.parse(response.body)['html_url']
207216
else
217+
p response.body
208218
puts "Creating gist failed: #{response.code} #{response.message}"
209219
exit(false)
210220
end
211221
end
212222

213223
def read(gist_id)
214-
open(GIST_URL % gist_id).read
224+
data = JSON.parse(open(GIST_URL % gist_id).read)
225+
data["files"].map{|name, content| content['content'] }.join("\n\n")
215226
end
216227

217228
def browse(url)
218229
if RUBY_PLATFORM =~ /darwin/
219230
`open #{url}`
231+
elsif RUBY_PLATFORM =~ /linux/
232+
`#{ENV['BROWSER']} #{url}`
220233
elsif ENV['OS'] == 'Windows_NT' or
221234
RUBY_PLATFORM =~ /djgpp|(cyg|ms|bcc)win|mingw|wince/i
222235
`start "" "#{url}"`
@@ -241,51 +254,46 @@ module Gist
241254
end
242255

243256
private
244-
def data(files, private_gist)
245-
data = {}
257+
def data(files, private_gist, description)
258+
i = 0
259+
file_data = {}
246260
files.each do |file|
247-
i = data.size + 1
248-
data["file_ext[gistfile#{i}]"] = file[:extension] ? file[:extension] : '.txt'
249-
data["file_name[gistfile#{i}]"] = file[:filename]
250-
data["file_contents[gistfile#{i}]"] = file[:input]
261+
i = i + 1
262+
filename = file[:filename] ? file[:filename] : "gistfile#{i}"
263+
file_data[filename] = {:content => file[:input]}
251264
end
252-
data.merge(private_gist ? { 'action_button' => 'private' } : {}).merge(auth)
265+
266+
data = {"files" => file_data}
267+
data.merge!({ 'description' => description }) unless description.nil?
268+
data.merge!({ 'public' => false }) if private_gist
269+
data
253270
end
254271

255272
def auth
256273
user = config("github.user")
257-
token = config("github.token")
274+
password = config("github.password")
258275

259-
if user.to_s.empty? || token.to_s.empty?
260-
{}
276+
if user.to_s.empty? || password.to_s.empty?
277+
nil
261278
else
262-
{ :login => user, :token => token }
279+
auth_str = Base64.encode64("#{user}:#{password}")
280+
"Basic #{auth_str}"
263281
end
264282
end
265283

266284
def defaults
267285
extension = config("gist.extension")
268-
extension = nil if extension && extension.empty?
269-
270-
copy = config("gist.copy")
271-
if copy.nil?
272-
copy = true
273-
else
274-
# match optparse boolean true states
275-
copy = copy =~ /^(true)|(on)|(\+)/
276-
end
277286

278287
return {
279288
"private" => config("gist.private"),
280289
"browse" => config("gist.browse"),
281-
"extension" => extension,
282-
"copy" => copy,
290+
"extension" => extension
283291
}
284292
end
285293

286294
def config(key)
287295
env_key = ENV[key.upcase.gsub(/\./, '_')]
288-
return env_key if env_key and not env_key.empty?
296+
return env_key if env_key and not env_key.strip.empty?
289297

290298
str_to_bool `git config --global #{key}`.strip
291299
end
@@ -330,7 +338,7 @@ __END__
330338
.\" generated with Ronn/v0.7.3
331339
.\" http://github.com/rtomayko/ronn/tree/0.7.3
332340
.
333-
.TH "GIST" "1" "April 2011" "GITHUB" "Gist Manual"
341+
.TH "GIST" "1" "June 2011" "GITHUB" "Gist Manual"
334342
.
335343
.SH "NAME"
336344
\fBgist\fR \- gist on the command line
@@ -348,7 +356,7 @@ If standard input is supplied, it will be used as the content of the new gist\.
348356
Once your gist is successfully created, the URL will be copied to your clipboard\. If you are on OS X, \fBgist\fR will open the gist in your browser, too\.
349357
.
350358
.SH "OPTIONS"
351-
\fBgist\fR\'s default mode of operation is to read content from standard input and create a public, text gist from it, tied to your GitHub account if you user and token are provided (see \fBCONFIGURATION\fR)\.
359+
\fBgist\fR\'s default mode of operation is to read content from standard input and create a public, text gist without description from it, tied to your GitHub account if you user and token are provided (see \fBCONFIGURATION\fR)\.
352360
.
353361
.P
354362
These options can be used to change this behavior:
@@ -359,7 +367,11 @@ Create a private gist instead of a public gist\.
359367
.
360368
.TP
361369
\fB\-t\fR, \fB\-\-type\fR
362-
Set the file extension explicitly\. Passing a type of \fBrb\fR ensure the gist is created as a Ruby file\.
370+
Set the file extension explicitly\. Passing a type of \fBrb\fR ensures the gist is created as a Ruby file\.
371+
.
372+
.TP
373+
\fB\-d\fR, \fB\-\-description\fR
374+
Set a description\.
363375
.
364376
.TP
365377
\fB\-o\fR, \fB\-\-[no\-]open\fR
@@ -380,7 +392,7 @@ Display this man page\.
380392
There are two ways to set GitHub user and token info:
381393
.
382394
.IP "\(bu" 4
383-
Using env vars GITHUB_USER and GITHUB_TOKEN
395+
Using environment vars GITHUB_USER and GITHUB_TOKEN
384396
.
385397
.IP
386398
$ export GITHUB_USER=johndoe

lib/gist.rb

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
require 'open-uri'
22
require 'net/https'
33
require 'optparse'
4+
5+
require 'rubygems'
46
require 'json'
57
require 'base64'
68

@@ -141,6 +143,7 @@ def write(files, private_gist = false, description = nil)
141143

142144
if auth_header = auth()
143145
req.add_field('Authorization', auth_header)
146+
end
144147

145148
response = http.start{|h| h.request(req) }
146149
case response
@@ -154,7 +157,8 @@ def write(files, private_gist = false, description = nil)
154157

155158
# Given a gist id, returns its content.
156159
def read(gist_id)
157-
open(GIST_URL % gist_id).read
160+
data = JSON.parse(open(GIST_URL % gist_id).read)
161+
data["files"].map{|name, content| content['content'] }.join("\n\n")
158162
end
159163

160164
# Given a url, tries to open it in your browser.
@@ -193,14 +197,17 @@ def copy(content)
193197
# an appropriate payload for POSTing to gist.github.com
194198
def data(files, private_gist, description)
195199
i = 0
196-
data = {}
197-
data["files"] = files.map do |file|
200+
file_data = {}
201+
files.each do |file|
198202
i = i + 1
199203
filename = file[:filename] ? file[:filename] : "gistfile#{i}"
200-
{filename => {:content => file[:input]}}
204+
file_data[filename] = {:content => file[:input]}
201205
end
206+
207+
data = {"files" => file_data}
202208
data.merge!({ 'description' => description }) unless description.nil?
203-
data.merge(private_gist ? { 'public' => false } : {})
209+
data.merge!({ 'public' => false }) if private_gist
210+
data
204211
end
205212

206213
# Returns a basic auth string of the user's GitHub credentials if set.
@@ -209,7 +216,7 @@ def auth
209216
user = config("github.user")
210217
password = config("github.password")
211218

212-
if user.to_s.empty? || token.to_s.empty?
219+
if user.to_s.empty? || password.to_s.empty?
213220
nil
214221
else
215222
auth_str = Base64.encode64("#{user}:#{password}")

man/gist.1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.\" generated with Ronn/v0.7.3
22
.\" http://github.com/rtomayko/ronn/tree/0.7.3
33
.
4-
.TH "GIST" "1" "June 2011" "GITHUB" "Gist Manual"
4+
.TH "GIST" "1" "March 2012" "GITHUB" "Gist Manual"
55
.
66
.SH "NAME"
77
\fBgist\fR \- gist on the command line

man/gist.1.html

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)