Octokit.rb从最新版本的API中提取资产

时间:2019-02-24 21:33:44

标签: ruby rest github octokit

我想用GET /repos/:owner/:repo/releases/latest从GitHub v3 API收集browser_download_url。 问题是,我无法提取资产URL。当存储库有更多版本时,例如针对不同平台的多个版本,我将一个URL写入json文件中,而不是多个。我该如何处理?

def cache_repositories(repos)
    @url_arr = []
    @name_arr = []
    version_res = []
    @version_arr = []
    download_url_res = []
    @download_url_arr = []

    repos.each {|repo|
      @name_arr << repo.name
      @url_arr << repo.html_url


      begin
        version_res << @client.latest_release(repo.full_name)

        version_res.each do |res|
          @version_arr << res.tag_name
          download_url_res << res.assets
          download_url_res.each do |download|
              download.each do |url|
               @download_url_arr << url.browser_download_url
            end
          end
      rescue
        @version_arr << ""
        @download_url_arr << ""
      end
    }

    repo_array_unsorted = @name_arr.zip(@url_arr, @version_arr, @download_url_arr)
    repo_array = repo_array_unsorted.map {|name, url, version, download_url| {:name => name, :url => url, :version => version, :download_url => download_url}}
    repo_array.to_json
  end

我的json看起来如下:

[
    {
        "name": "bar1",
        "url": "https://github.com/foo/bar1",
        "version": "",
        "download_url": ""
    },
    {
        "name": "bar2",
        "url": "https://github.com/foo/bar2",
        "version": "",
        "download_url": ""
    },
    {
        "name": "bar3",
        "url": "https://github.com/foo/bar3",
        "version": "v21.9.10",
        "download_url": ""
    }
]

0 个答案:

没有答案