Class: OCIRegistry::URI

Inherits:
URI::Generic
  • Object
show all
Defined in:
lib/oci_registry/uri.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ URI

Returns a new instance of URI.

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/oci_registry/uri.rb', line 8

def initialize(uri)
  raise ArgumentError, 'OCIRegistry::URI->URI cannot be nil' if uri.nil?
  raise ArgumentError, 'OCIRegistry::URI->URI must be a String' unless uri.is_a?(String)

  # TODO: This addition makes this less an OCI URI library and more a CNB Builder Resource Library
  # Handle the case where the URI is just ./eol-buildpack/ which is a local path.
  @repository = uri and @scheme = 'local' and return if uri.start_with?('./')

  # Call the parent class constructor
  @parsed = ::URI.parse(uri)  # Use `::URI` to refer to Ruby's URI class

  # Ensure the scheme is docker
  unless ['docker', 'oci'].include?(@parsed.scheme)
    raise ArgumentError, 'URI must start with (docker|oci)://'
  end

  # Extract repository, tag, and digest from the URI's path
  @scheme = @parsed.scheme
  @host = @parsed.host
  @repository, tag_or_digest = @parsed.path.split('@').first.split(':')
  # Remove any leading slashes from the repository
  @repository = @repository.sub(%r{^/*}, '')
  @digest = @parsed.path.split('@')[1]
  @tag = tag_or_digest unless @digest
end

Instance Attribute Details

#digestObject

Returns the value of attribute digest.



6
7
8
# File 'lib/oci_registry/uri.rb', line 6

def digest
  @digest
end

#hostObject

Returns the value of attribute host.



6
7
8
# File 'lib/oci_registry/uri.rb', line 6

def host
  @host
end

#repositoryObject

Returns the value of attribute repository.



6
7
8
# File 'lib/oci_registry/uri.rb', line 6

def repository
  @repository
end

#schemeObject

Returns the value of attribute scheme.



6
7
8
# File 'lib/oci_registry/uri.rb', line 6

def scheme
  @scheme
end

#tagObject

Returns the value of attribute tag.



6
7
8
# File 'lib/oci_registry/uri.rb', line 6

def tag
  @tag
end

Instance Method Details

#to_sObject

Allow for heroku/ruby heroku/ruby:2.7.2 heroku/ruby@sha256:1234abcd docker://docker.io/heroku/ruby docker://docker.io/heroku/ruby:2.7.2



40
41
42
43
44
45
46
47
48
# File 'lib/oci_registry/uri.rb', line 40

def to_s
  base = ""
  base += "#{@scheme}://"  if @scheme
  base += "#{@host}/"      if @host
  base += "#{@repository}" if @repository
  base += ":#{@tag}"    if @tag
  base += "@#{@digest}" if @digest
  base
end