2017-05-07 96 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

非常感谢建议。这帮了很多。 :) –

相关问题