Module: PuppetLanguageServer::UriHelper

Defined in:
lib/puppet-languageserver/uri_helper.rb

Class Method Summary collapse

Class Method Details

.build_file_uri(path) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/puppet-languageserver/uri_helper.rb', line 8

def self.build_file_uri(path)
  if path.nil?
    nil
  else
    "file://#{Puppet::Util.uri_encode(path.start_with?('/') ? path : "/#{path}")}"
  end
end

.relative_uri_path(root_uri, uri, case_sensitive = true) ⇒ String

Compares two URIs and returns the relative path

Parameters:

  • root_uri (String)

    The root URI to compare to

  • uri (String)

    The URI to compare to the root

  • case_sensitive (Boolean) (defaults to: true)

    Whether the path comparison is case senstive or not. Default is true

Returns:

  • (String)

    Returns the relative path string if the URI is indeed a child of the root, otherwise returns nil



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/puppet-languageserver/uri_helper.rb', line 26

def self.relative_uri_path(root_uri, uri, case_sensitive = true)
  actual_root = URI(root_uri)
  actual_uri = URI(uri)
  return nil unless actual_root.scheme == actual_uri.scheme

  # CGI.unescape doesn't handle space rules properly in uri paths
  # URI::parser.unescape does, but returns strings in their original encoding
  # Mostly safe here as we're only worried about file based URIs
  parser = URI::DEFAULT_PARSER
  root_path = parser.unescape(actual_root.path)
  uri_path = parser.unescape(actual_uri.path)
  if case_sensitive
    return nil unless uri_path.slice(0, root_path.length) == root_path
  else
    return nil unless uri_path.slice(0, root_path.length).casecmp(root_path).zero?
  end

  uri_path.slice(root_path.length..-1)
end

.uri_path(uri_string) ⇒ Object



16
17
18
# File 'lib/puppet-languageserver/uri_helper.rb', line 16

def self.uri_path(uri_string)
  uri_string.nil? ? nil : Puppet::Util.uri_to_path(URI(uri_string))
end