2013-05-03 99 views

回答

2

這對我有效。

require 'net/http' 
require 'json' 

uri = URI("https://api.github.com/gists") 

payload = { 
    'description' => "My test gist", 
    'public' => true, 
    'files' => { 
    'test.txt' => { 
     'content' => "This is a test!\n\nI am making a public gist." 
    } 
    } 
} 

req = Net::HTTP::Post.new(uri.path) 
req.body = payload.to_json 

puts req.inspect 
puts req.body.inspect 

# GitHub API is strictly via HTTPS, so SSL is mandatory 
res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => true) do |http| 
    http.request(req) 
end 

puts res.inspect 
puts res.body.inspect 

結果:My test gist

+0

非常感謝:) – 2013-05-03 19:36:32

+0

有什麼辦法可以使用Net :: HTTP.post_form(...)來做這個調用嗎? – 2014-02-14 05:40:15

0

這個寶石將爲你做詭計https://github.com/defunkt/gist

爲了記錄,它確實需要net/https。

+0

我的問題是,我想只是爲了不增加整體的依賴在增加我的一個創業板的小功能。 – 2013-05-03 18:46:25

+0

http://www.travisberry.com/2011/05/create-github-gist-with-ruby/ - 這應該做的伎倆然後。 – baordog 2013-05-03 18:50:58

+0

這是Gtihub API的指南v1 – 2013-05-03 18:53:15