Class: Yast2::RelURL

Inherits:
Object
  • Object
show all
Defined in:
library/general/src/lib/yast2/rel_url.rb

Overview

Class for working with relative URLs ("relurl://")

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url, rel_url) ⇒ RelURL

Constructor

Parameters:

  • base_url (String, URI)

    the base URL

  • rel_url (String, URI)

    the relative URL, it should use the "relurl://" schema otherwise the base URL is ignored



59
60
61
62
63
64
65
# File 'library/general/src/lib/yast2/rel_url.rb', line 59

def initialize(base_url, rel_url)
  @base = URI(base_url).dup
  @relative = URI(rel_url).dup

  preprocess_url(base)
  preprocess_url(relative)
end

Instance Attribute Details

#baseURI (readonly)

Returns the input base URL.

Returns:

  • (URI)

    the input base URL



27
28
29
# File 'library/general/src/lib/yast2/rel_url.rb', line 27

def base
  @base
end

#relativeURI (readonly)

Returns the input relative URL.

Returns:

  • (URI)

    the input relative URL



30
31
32
# File 'library/general/src/lib/yast2/rel_url.rb', line 30

def relative
  @relative
end

Class Method Details

.from_installation_repository(rel_url) ⇒ RelURL

Note:

Works properly only during installation/upgrade, do not use in an installed system.

Create RelURL object with URL relative to the installation repository

Parameters:

  • rel_url (String, URI)

    the relative URL, if non-relative URL is used then the result is this URL

Returns:



48
49
50
51
52
# File 'library/general/src/lib/yast2/rel_url.rb', line 48

def self.from_installation_repository(rel_url)
  Yast.import "InstURL"
  base_url = Yast::InstURL.installInf2Url("")
  new(base_url, rel_url)
end

.relurl?(url) ⇒ Boolean

Is the URL a relative URL?

Parameters:

  • url (String, URI)

    the URL

Returns:

  • (Boolean)

    true if the URL uses the "relurl" schema, otherwise false



36
37
38
# File 'library/general/src/lib/yast2/rel_url.rb', line 36

def self.relurl?(url)
  URI(url).scheme == "relurl"
end

Instance Method Details

#absolute_url(path = nil) ⇒ URI

Note:

It internally uses the Ruby File.expand_path function which

Build an absolute URL

also evaluates the parent directory path ("../") so it is possible to go up in the tree using the "relurl://../foo" or the "../foo" path parameter.

Parameters:

  • path (String, nil) (defaults to: nil)

    optional URL subpath

Returns:

  • (URI)

    the absolute URL



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'library/general/src/lib/yast2/rel_url.rb', line 76

def absolute_url(path = nil)
  if (!relative.to_s.empty? && !RelURL.relurl?(relative)) || base.to_s.empty?
    ret = relative.dup
    relative_url = URI("")
  else
    ret = base.dup
    relative_url = relative.dup
  end

  relative_path = relative_url.path
  relative_path = File.join(relative_path, path) if path && !path.empty?

  base_path = ret.path
  if !base_path.empty? || !relative_path.empty?
    # the path would be expanded from the current working directory
    # by File.expand_path if the base path is not absolute
    base_path.prepend("/") if !base_path.start_with?("/")

    # escape the "~" character, it is treated as a home directory name by File.expand_path,
    # moreover it raises ArgumentError if that user does not exist in the system
    relative_path.gsub!("~", "%7E")
    # the relative path really needs to be relative, remove the leading slash(es)
    relative_path.sub!(/\A\/+/, "")

    absolute_path = File.expand_path(relative_path, base_path)
    # URI::FTP escapes the initial "/" to "%2F" which we do not want here
    absolute_path.sub!(/\A\/+/, "") if ret.scheme == "ftp"

    ret.path = absolute_path
  end

  ret
end