1

我基本上是在尝试使用 octokit github api 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 名称、电子邮件、组织等,但是当我使用方法时……它们总是有选项参数,我很难为此指定正确的参数。

4

1 回答 1

5

您需要使用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

有关可能响应的完整列表,请参阅GitHub API 文档

我还做了两个小改动:

  1. 将您的 GitHub API 令牌放入环境变量 ( ENV['GITHUB_API']) 中,而不是对其进行硬编码。

  2. 在测试中,我厌倦了手动输入我的测试用户名,所以我使用命令行参数作为默认值,手动输入作为后备:

    @username = ARGV[0] || gets.chomp.to_s
    
于 2017-05-08T16:57:51.380 回答