Class: Gitballs::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/gitballs/client.rb

Constant Summary collapse

BASE_URL =
"https://packages.ecosyste.ms/api/v1"
REGISTRY_MAP =
{
  "gem" => "rubygems.org",
  "npm" => "npmjs.org",
  "pypi" => "pypi.org",
  "cargo" => "crates.io",
  "nuget" => "nuget.org",
  "maven" => "repo1.maven.org",
  "go" => "proxy.golang.org",
  "hex" => "hex.pm",
  "packagist" => "packagist.org"
}.freeze

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



23
24
25
# File 'lib/gitballs/client.rb', line 23

def initialize
  @hydra = Typhoeus::Hydra.new
end

Instance Method Details

#download_tarball(url, destination) ⇒ Object

Raises:



40
41
42
43
44
45
# File 'lib/gitballs/client.rb', line 40

def download_tarball(url, destination)
  response = Typhoeus.get(url, followlocation: true)
  raise Error, "Failed to download #{url}: #{response.code}" unless response.success?

  File.binwrite(destination, response.body)
end

#fetch_versions(purl_string) ⇒ Object

Raises:



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/gitballs/client.rb', line 27

def fetch_versions(purl_string)
  purl = Purl.parse(purl_string)
  registry = registry_for(purl.type)
  package_name = package_name_for(purl)

  url = "#{BASE_URL}/registries/#{registry}/packages/#{package_name}/versions"
  response = Typhoeus.get(url)

  raise Error, "Failed to fetch versions: #{response.code}" unless response.success?

  JSON.parse(response.body)
end

#package_name_for(purl) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/gitballs/client.rb', line 51

def package_name_for(purl)
  if purl.namespace
    "#{purl.namespace}%2F#{purl.name}"
  else
    purl.name
  end
end

#registry_for(purl_type) ⇒ Object



47
48
49
# File 'lib/gitballs/client.rb', line 47

def registry_for(purl_type)
  REGISTRY_MAP[purl_type] || raise(Error, "Unsupported purl type: #{purl_type}")
end