module Triplets

Triplets is a mix-in for subclasses of URI::Generic, which allows URI to use SCP-style Triplet URLs in a somewhat more meaningful way.

Attributes

host[RW]
path[RW]
scheme[RW]
user[RW]

Public Instance Methods

to_s() click to toggle source

rubocop:enable Metrics/AbcSize

# File lib/uri/triplets.rb, line 39
def to_s
  return triplet if triplet?

  rfc_uri
end
triplet?() click to toggle source

Use the same regular expressions that the parser uses to determine if this is a valid triplet.

# File lib/uri/triplets.rb, line 47
def triplet?
  # False if self matches a normal URI scheme
  rfc_uri !~ URI.parser.const_get(:SCHEME) &&
    # False unless self matches a Triplet scheme
    !!(triplet =~ URI.parser.const_get(:TRIPLET))
end

Private Instance Methods

rfc_uri() click to toggle source

@return [String] a string representation of the URI components as a valid RFC compliant URI rubocop:disable Metrics/AbcSize

# File lib/uri/triplets.rb, line 24
def rfc_uri
  str = []
  str << "#{scheme}://" if scheme
  str << "#{user}@" if user
  if port && port != self.class::DEFAULT_PORT
    host_info = [host, port].join(":")
    str << [host_info, path].join("/").squeeze("/")
  else
    str << [host, path].join("/").squeeze("/")
  end
  str.join
end
triplet() click to toggle source

@return [String] a string representation of the URI components as an SCP-style Triplet

# File lib/uri/triplets.rb, line 13
def triplet
  str = []
  str << "#{user}@" if user && !user.empty?
  str << "#{host}:#{path}".squeeze("/")
  str.join
end