|
1 | 1 | require 'open-uri'
|
2 | 2 | require 'net/https'
|
3 | 3 | require 'optparse'
|
| 4 | +require 'json' |
| 5 | +require 'base64' |
4 | 6 |
|
5 | 7 | require 'gist/manpage' unless defined?(Gist::Manpage)
|
6 | 8 | require 'gist/version' unless defined?(Gist::Version)
|
|
23 | 25 | module Gist
|
24 | 26 | extend self
|
25 | 27 |
|
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' |
28 | 30 |
|
29 | 31 | if ENV['HTTPS_PROXY']
|
30 | 32 | PROXY = URI(ENV['HTTPS_PROXY'])
|
@@ -135,12 +137,15 @@ def write(files, private_gist = false, description = nil)
|
135 | 137 | http.ca_file = ca_cert
|
136 | 138 |
|
137 | 139 | 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) |
139 | 144 |
|
140 | 145 | response = http.start{|h| h.request(req) }
|
141 | 146 | case response
|
142 |
| - when Net::HTTPRedirection |
143 |
| - response['Location'] |
| 147 | + when Net::HTTPCreated |
| 148 | + JSON.parse(response.body)['html_url'] |
144 | 149 | else
|
145 | 150 | puts "Creating gist failed: #{response.code} #{response.message}"
|
146 | 151 | exit(false)
|
@@ -187,27 +192,28 @@ def copy(content)
|
187 | 192 | # Give an array of file information and private boolean, returns
|
188 | 193 | # an appropriate payload for POSTing to gist.github.com
|
189 | 194 | def data(files, private_gist, description)
|
| 195 | + i = 0 |
190 | 196 | 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]}} |
196 | 201 | end
|
197 | 202 | data.merge!({ 'description' => description }) unless description.nil?
|
198 |
| - data.merge(private_gist ? { 'action_button' => 'private' } : {}).merge(auth) |
| 203 | + data.merge(private_gist ? { 'public' => false } : {}) |
199 | 204 | end
|
200 | 205 |
|
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. |
202 | 207 | # http://github.com/guides/local-github-config
|
203 | 208 | def auth
|
204 | 209 | user = config("github.user")
|
205 |
| - token = config("github.token") |
| 210 | + password = config("github.password") |
206 | 211 |
|
207 | 212 | if user.to_s.empty? || token.to_s.empty?
|
208 |
| - {} |
| 213 | + nil |
209 | 214 | else
|
210 |
| - { :login => user, :token => token } |
| 215 | + auth_str = Base64.encode64("#{user}:#{password}") |
| 216 | + "Basic #{auth_str}" |
211 | 217 | end
|
212 | 218 | end
|
213 | 219 |
|
|
0 commit comments