Module: T2Server::Util

Defined in:
lib/t2-server/util.rb

Overview

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

Class Method Summary collapse

Class Method Details

.append_to_uri_path(uri, path) ⇒ Object

:call-seq:

Util.append_to_uri_path(uri, path) -> URI

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



79
80
81
82
83
84
85
86
# File 'lib/t2-server/util.rb', line 79

def self.append_to_uri_path(uri, path)
  return uri if path == ""

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

  new_uri
end

.get_path_leaf_from_uri(uri) ⇒ Object

:call-seq:

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.



105
106
107
108
109
# File 'lib/t2-server/util.rb', line 105

def self.get_path_leaf_from_uri(uri)
  path = uri.path.split("/")[-1]

  path.nil? ? "" : path
end

.replace_uri_path(uri, path) ⇒ Object

:call-seq:

Util.replace_uri_path(uri, path) -> URI

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



93
94
95
96
97
98
# File 'lib/t2-server/util.rb', line 93

def self.replace_uri_path(uri, path)
  new_uri = uri.clone
  new_uri.path = path

  new_uri
end

.strip_path_slashes(path) ⇒ Object

:call-seq:

Util.strip_path_slashes(path) -> string

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



71
72
73
# File 'lib/t2-server/util.rb', line 71

def self.strip_path_slashes(path)
  path.gsub(/^\//, "").chomp("/")
end

.strip_uri_credentials(uri) ⇒ Object

:call-seq:

Util.strip_uri_credentials(uri) -> URI, HttpBasic

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



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/t2-server/util.rb', line 49

def self.strip_uri_credentials(uri)
  # we want to use URIs here but strings can be passed in
  uri = URI.parse(Util.strip_path_slashes(uri)) unless uri.is_a? URI

  creds = nil

  # strip username and password from the URI if present
  if uri.user != nil
    creds = T2Server::HttpBasic.new(uri.user, uri.password)

    uri = URI::HTTP.new(uri.scheme, nil, uri.host, uri.port, nil,
    uri.path, nil, nil, nil);
  end

  [uri, creds]
end