使用 github api 的 octokit ruby 工具包获取存储库名称

obtaining repository name using octokit ruby toolkit for github api

我基本上是在尝试使用 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

我知道选项参数是一个散列,但我仍然对如何指定参数以获取存储库名称感到困惑。这是我的代码:

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 姓名、电子邮件、组织等,但是当我使用方法时...它们总是有选项参数和我很难为此指定正确的参数。

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