Class: Yast2::RelURL
- Inherits:
-
Object
- Object
- Yast2::RelURL
- Defined in:
- library/general/src/lib/yast2/rel_url.rb
Overview
Class for working with relative URLs ("relurl://")
Instance Attribute Summary collapse
-
#base ⇒ URI
readonly
The input base URL.
-
#relative ⇒ URI
readonly
The input relative URL.
Class Method Summary collapse
-
.from_installation_repository(rel_url) ⇒ RelURL
Create RelURL object with URL relative to the installation repository.
-
.relurl?(url) ⇒ Boolean
Is the URL a relative URL?.
Instance Method Summary collapse
-
#absolute_url(path = nil) ⇒ URI
Build an absolute URL.
-
#initialize(base_url, rel_url) ⇒ RelURL
constructor
Constructor.
Constructor Details
#initialize(base_url, rel_url) ⇒ RelURL
Constructor
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
#base ⇒ URI (readonly)
Returns the input base URL.
27 28 29 |
# File 'library/general/src/lib/yast2/rel_url.rb', line 27 def base @base end |
#relative ⇒ URI (readonly)
Returns 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
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?
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.
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.(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 |