2015-10-20 139 views
1

curl命令任何一個可以告訴我如何在紅寶石執行紅寶石

寫這個命令
curl -i -u USERNAME https://api.github.com/repos/USERNAME/REPOSITORY/forks 
+4

看看https://github.com/taf2/curb – Santhosh

+0

@Santhosh謝謝你。 –

回答

0

你可以做到這一點與Hurley

首先安裝寶石:

$ gem install hurley 

然後運行這個:

require "hurley" 
require "json" 

client = Hurley::Client.new "https://api.github.com" 

repository = { 
    username: "repo_username", 
    name: "repo_name", 
} 

authentication = { 
    username: "XXX", 
    password: "YYY", 
} 

response = client.get("/repos/#{repository[:username]}/#{repository[:name]}/forks") do |req| 
    req.header[:accept] = "application/vnd.github.preview+json" 
    req.url.user = authentication[:username] 
    req.url.password = authentication[:password] 
end 

puts JSON.parse(response.body) 
+0

謝謝@olistik –

0

看一看HttpParty這幫助您發送GETPOSTPUTDELETE請求到遠程服務器,您可以發送Authentication和發送URL DataPost data

對於您的情況下,它會增加創業板的寶石文件後:

response = HTTParty.get("https://api.github.com/repos/USERNAME/REPOSITORY/forks", 
         body:{}, 
         basic_auth: {username: 'USERNAME', password: 'PASSWORD'} 
         ) 

if response && response.body 
    # parse to json if you know that the responce is json otherwise work with the response whatever you want to do. 
    json=JSON.parse(response.body) 
end 

身體是一個哈希,如果你想在你的情況下發送任何數據的空沒有數據要發送,並Basic Auth到請發送username and password您應該用您的用戶名和密碼替換USERNAMEPASSWORD

+0

謝謝你@ mohamed-ibrahim –

0

通過避免外部依賴,也可以堅持使用普通的老式Net::HTTP

require "net/http" 

username = "USERNAME" 
password = "PASSWORD" 
repository = "REPOSITORY" 

url = "https://api.github.com/repos/#{username}/#{repository}/forks" 
uri = URI.parse(url) 
Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https") do |http| 
    request = Net::HTTP::Head.new(uri) 
    request.basic_auth(username, password) 
    response = http.request(request) 
    puts response.to_hash.inspect 
end 

,如果你有啓用TFA(雙因素身份驗證),你應該,你可以創建一個訪問令牌here所述,並使用它來代替密碼。