2017-05-07 55 views
1

我基本上試圖用octokit github api ruby​​工具包獲取我的存儲庫名稱。我看了看文檔和他們的代碼文件中:使用用於github api的octokit ruby​​工具包獲取存儲庫名稱

# Get a single repository 
    # 
    # @see https://developer.github.com/v3/repos/#get 
    # @see https://developer.github.com/v3/licenses/#get-a-repositorys-license 
    # @param repo [Integer, String, Hash, Repository] A GitHub repository 
    # @return [Sawyer::Resource] Repository information 
    def repository(repo, options = {}) 
    get Repository.path(repo), options 
    end 
    alias :repo :repository 

    # Edit a repository 
    # 
    # @see https://developer.github.com/v3/repos/#edit 
    # @param repo [String, Hash, Repository] A GitHub repository 
    # @param options [Hash] Repository information to update 
    # @option options [String] :name Name of the repo 
    # @option options [String] :description Description of the repo 
    # @option options [String] :homepage Home page of the repo 
    # @option options [String] :private `true` makes the repository private, and `false` makes it public. 
    # @option options [String] :has_issues `true` enables issues for this repo, `false` disables issues. 
    # @option options [String] :has_wiki `true` enables wiki for this repo, `false` disables wiki. 
    # @option options [String] :has_downloads `true` enables downloads for this repo, `false` disables downloads. 
    # @option options [String] :default_branch Update the default branch for this repository. 
    # @return [Sawyer::Resource] Repository information 

據我所知,options參數是一個哈希,但對如何指定參數以獲得資源庫的名字我'還是有點困惑。這裏是我的代碼:

require 'octokit' 
require 'netrc' 

class Base 
# attr_accessor :un, :pw 

# un = username 
# pw = password 

def initialize 
    @client = Octokit::Client.new(:access_token => 
    '<access_token>') 

    print "Username you want to search?\t" 
    @username = gets.chomp.to_s 

    @user = @client.user(@username) 

    puts "#{@username} email is:\t\t#{@user.email}" 
    puts @user.repository('converse', :options => name) 
end 
end 



start = Base.new 

我acess_token我'能得到我自己或別人github上的姓名,電子郵件,組織等,但是當我使用的方法......他們總是有選擇的參數和我我很難爲此指定正確的論點。

回答

3

你需要使用repos方法,而不是user方法:

require 'octokit' 
require 'netrc' 

class Base 

    def initialize 
    @client = Octokit::Client.new(:access_token => ENV['GITHUB_API']) 

    print "Username you want to search?\t" 
    @username = ARGV[0] || gets.chomp.to_s 

    @user = @client.user(@username) 

    puts "#{@username} email is:\t\t#{@user.email}" 

    @client.repos(@username).each do |r| 
     puts r[:name] 
    end 
    end 

end 

start = Base.new 

對於可能的響應的完整列表,請參閱the GitHub API documentation

我還做了兩個小的變化:

  1. 把你的GitHub的API令牌中的環境變量(ENV['GITHUB_API']),而不是硬編碼。

  2. 在測試中,我生病了在手動輸入我的測試用戶名的,所以我使用的命令行參數與手動輸入作爲後備默認:

    @username = ARGV[0] || gets.chomp.to_s 
    
+0

非常感謝建議。這幫了很多。 :) –

相關問題