module T2Server::Util

This module contains various utility methods that the library uses internally.

Public Class Methods

append_to_uri_path(uri, path) → URI click to toggle source

Appends a path to the end of the path of the given URI.

# File lib/t2-server/util.rb, line 77
def self.append_to_uri_path(uri, path)
  return uri if path == ""

  new_uri = uri.clone
  new_uri.path = "#{uri.path}/#{path}"

get_path_leaf_from_uri(uri) click to toggle source


Util.get_path_leaf_from_uri(uri) -> String

Get the final component from the path of a URI. This method returns the empty string (not nil ) if the URI does not have a path.

# File lib/t2-server/util.rb, line 103
def self.get_path_leaf_from_uri(uri)
  path = uri.path.split("/")[-1]

  path.nil? ? "" : path
replace_uri_path(uri, path) → URI click to toggle source

Replace the given URI’s path with a new one. The new path must be an absolute path (start with a slash).

# File lib/t2-server/util.rb, line 91
def self.replace_uri_path(uri, path)
  new_uri = uri.clone
  new_uri.path = path

strip_path_slashes(path) → String click to toggle source

Returns a new String with one leading and one trailing slash removed from the ends of path (if present).

# File lib/t2-server/util.rb, line 69
def self.strip_path_slashes(path)
  path.gsub(%r^\//, "").chomp("/")
strip_uri_credentials(uri) → URI, Credentials click to toggle source

Strip user credentials from an address in URI or String format and return a tuple of the URI minus the credentials and a T2Server::Credentials object.

# File lib/t2-server/util.rb, line 45
def self.strip_uri_credentials(uri)
  # we want to use URIs here but strings can be passed in
  unless uri.is_a? URI
    uri = URI.parse(Util.strip_path_slashes(uri))

  creds = nil

  # strip username and password from the URI if present
  if uri.user != nil
    creds =, uri.password)

    uri =, nil,, uri.port, nil,
    uri.path, nil, nil, nil);

  [uri, creds]