class Endpoint
Represents a way to connect to a remote Redis server.
Definitions
def self.for(scheme, hostname, credentials: nil, port: nil, database: nil, **options)
Construct an endpoint with a specified scheme, hostname, optional path, and options.
Signature
-
parameter
scheme
String
The scheme to use, e.g. "redis" or "rediss".
-
parameter
hostname
String
The hostname to connect to (or bind to).
-
parameter
options
Hash
Additional options, passed to
#initialize
.
Implementation
def self.for(scheme, hostname, credentials: nil, port: nil, database: nil, **options)
uri_klass = SCHEMES.fetch(scheme.downcase) do
raise ArgumentError, "Unsupported scheme: #{scheme.inspect}"
end
if database
path = "/#{database}"
end
self.new(
uri_klass.new(scheme, credentials&.join(":"), hostname, port, nil, path, nil, nil, nil).normalize,
**options
)
end
def self.[](object)
Coerce the given object into an endpoint.
Signature
-
parameter
url
String | Endpoint
The URL or endpoint to convert.
Implementation
def self.[](object)
if object.is_a?(self)
return object
else
self.parse(object.to_s)
end
end
def initialize(url, endpoint = nil, **options)
Create a new endpoint.
Signature
-
parameter
url
URI
The URL to connect to.
-
parameter
endpoint
Endpoint
The underlying endpoint to use.
-
option
scheme
String
The scheme to use, e.g. "redis" or "rediss".
-
option
hostname
String
The hostname to connect to (or bind to), overrides the URL hostname (used for SNI).
-
option
port
Integer
The port to bind to, overrides the URL port.
-
option
ssl_context
OpenSSL::SSL::SSLContext
The SSL context to use for secure connections.
Implementation
def initialize(url, endpoint = nil, **options)
super(**options)
raise ArgumentError, "URL must be absolute (include scheme, host): #{url}" unless url.absolute?
@url = url
if endpoint
@endpoint = self.build_endpoint(endpoint)
else
@endpoint = nil
end
end
def hostname
The hostname is the server we are connecting to:
Implementation
def hostname
@options[:hostname] || @url.hostname
end
def ssl_verify_mode
We don't try to validate peer certificates when talking to localhost because they would always be self-signed.
Implementation
def ssl_verify_mode
if self.localhost?
OpenSSL::SSL::VERIFY_NONE
else
OpenSSL::SSL::VERIFY_PEER
end
end