Skip to content

Commit 30613ab

Browse files
committed
Update lib/gist.rb
1 parent e96203a commit 30613ab

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

lib/gist.rb

Lines changed: 21 additions & 15 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+
require 'json'
5+
require 'base64'
46

57
require 'gist/manpage' unless defined?(Gist::Manpage)
68
require 'gist/version' unless defined?(Gist::Version)
@@ -23,8 +25,8 @@
2325
module Gist
2426
extend self
2527

26-
GIST_URL = 'https://gist.github.com/%s.txt'
27-
CREATE_URL = 'https://gist.github.com/gists'
28+
GIST_URL = 'https://api.github.com/gists/%s'
29+
CREATE_URL = 'https://api.github.com/gists'
2830

2931
if ENV['HTTPS_PROXY']
3032
PROXY = URI(ENV['HTTPS_PROXY'])
@@ -135,12 +137,15 @@ def write(files, private_gist = false, description = nil)
135137
http.ca_file = ca_cert
136138

137139
req = Net::HTTP::Post.new(url.path)
138-
req.form_data = data(files, private_gist, description)
140+
req.body = JSON.generate(data(files, private_gist, description))
141+
142+
if auth_header = auth()
143+
req.add_field('Authorization', auth_header)
139144

140145
response = http.start{|h| h.request(req) }
141146
case response
142-
when Net::HTTPRedirection
143-
response['Location']
147+
when Net::HTTPCreated
148+
JSON.parse(response.body)['html_url']
144149
else
145150
puts "Creating gist failed: #{response.code} #{response.message}"
146151
exit(false)
@@ -187,27 +192,28 @@ def copy(content)
187192
# Give an array of file information and private boolean, returns
188193
# an appropriate payload for POSTing to gist.github.com
189194
def data(files, private_gist, description)
195+
i = 0
190196
data = {}
191-
files.each do |file|
192-
i = data.size + 1
193-
data["file_ext[gistfile#{i}]"] = file[:extension] ? file[:extension] : '.txt'
194-
data["file_name[gistfile#{i}]"] = file[:filename]
195-
data["file_contents[gistfile#{i}]"] = file[:input]
197+
data["files"] = files.map do |file|
198+
i = i + 1
199+
filename = file[:filename] ? file[:filename] : "gistfile#{i}"
200+
{filename => {:content => file[:input]}}
196201
end
197202
data.merge!({ 'description' => description }) unless description.nil?
198-
data.merge(private_gist ? { 'action_button' => 'private' } : {}).merge(auth)
203+
data.merge(private_gist ? { 'public' => false } : {})
199204
end
200205

201-
# Returns a hash of the user's GitHub credentials if set.
206+
# Returns a basic auth string of the user's GitHub credentials if set.
202207
# http://github.com/guides/local-github-config
203208
def auth
204209
user = config("github.user")
205-
token = config("github.token")
210+
password = config("github.password")
206211

207212
if user.to_s.empty? || token.to_s.empty?
208-
{}
213+
nil
209214
else
210-
{ :login => user, :token => token }
215+
auth_str = Base64.encode64("#{user}:#{password}")
216+
"Basic #{auth_str}"
211217
end
212218
end
213219

0 commit comments

Comments
 (0)