class Savvy::Configurators::Redis

Constants

DEFAULT_SEPARATOR
PATH_PATTERN

Attributes

namespace_prefix[R]

This is the namespace portion of a redis URI that is possibly set by the environment and will prefix the Savvy-generated namespace.

@return [String, nil]

Public Class Methods

new(config: Savvy.config) click to toggle source

@param [Savvy::Config] config

# File lib/savvy/configurators/redis.rb, line 16
def initialize(config: Savvy.config)
  @config = config

  @db = 0
  @namespace_prefix = nil

  parse_url!
end

Public Instance Methods

build_connection(*namespace_parts) click to toggle source

@return [Redis::Namespace]

# File lib/savvy/configurators/redis.rb, line 135
def build_connection(*namespace_parts)
  raise Savvy::RedisError, 'Requires redis-namespace gem' unless defined?(::Redis::Namespace)

  ::Redis::Namespace.new(namespace(*namespace_parts), redis: ::Redis.new(url: url))
end
build_connection_pool(*namespace_parts, size: 5, timeout: 5) click to toggle source

@return [ConnectionPool]

# File lib/savvy/configurators/redis.rb, line 126
def build_connection_pool(*namespace_parts, size: 5, timeout: 5)
  raise Savvy::RedisError, 'Requires connection_pool gem' unless defined?(ConnectionPool)

  ::ConnectionPool.new size: size, timeout: timeout do
    build_connection(*namespace_parts)
  end
end
config_hash(*parts, without_namespace: false) click to toggle source

@param [<String>] parts @param [Boolean] without_namespace return a configuration hash only as defined @return [{Symbol => Object}]

# File lib/savvy/configurators/redis.rb, line 28
def config_hash(*parts, without_namespace: false)
  check_validity!

  {
    host: @host,
    port: @port,
    db:   @db,
    scheme: @scheme,
    password: @password,
    ssl: uses_ssl?,
  }.tap do |h|
    ns = without_namespace ? @namespace_prefix : namespace(*parts)

    h[:namespace] = ns unless ns.nil?
  end
end
Also aliased as: to_h
db() click to toggle source

@!attribute [r] db The database number used by redis. @return [Integer]

# File lib/savvy/configurators/redis.rb, line 50
def db
  check_validity!

  @db
end
has_ssl?()
Alias for: uses_ssl?
host() click to toggle source

@!attribute [r] host @return [String]

# File lib/savvy/configurators/redis.rb, line 58
def host
  check_validity!

  @host
end
namespace(*parts, separator: DEFAULT_SEPARATOR) click to toggle source
# File lib/savvy/configurators/redis.rb, line 64
def namespace(*parts, separator: DEFAULT_SEPARATOR)
  check_validity!

  @config.build_namespace(*parts, prefix: @namespace_prefix, separator: separator)
end
namespaced_url(*parts) click to toggle source

@param [<String>] parts parts to add to the namespace @return [String]

# File lib/savvy/configurators/redis.rb, line 72
def namespaced_url(*parts)
  check_validity!

  build_redis_url *parts
end
password() click to toggle source

@!attribute [r] password @return [String, nil]

# File lib/savvy/configurators/redis.rb, line 80
def password
  check_validity!

  @password
end
port() click to toggle source

@!attribute [r] port @return [Integer]

# File lib/savvy/configurators/redis.rb, line 88
def port
  check_validity!

  @port
end
scheme() click to toggle source

@!attribute [r] scheme @return [“rediss”, “redis”]

# File lib/savvy/configurators/redis.rb, line 96
def scheme
  check_validity!

  @scheme
end
to_h(*parts, without_namespace: false)
Alias for: config_hash
url() click to toggle source

The url as configured by the environment (sans namespace)

@return [String]

# File lib/savvy/configurators/redis.rb, line 105
def url
  check_validity!

  build_redis_url without_namespace: true
end
userinfo() click to toggle source

@!attribute [r] userinfo @return [String, nil]

# File lib/savvy/configurators/redis.rb, line 113
def userinfo
  check_validity!

  @userinfo
end
uses_ssl?() click to toggle source
# File lib/savvy/configurators/redis.rb, line 119
def uses_ssl?
  @scheme == "rediss"
end
Also aliased as: has_ssl?
valid?() click to toggle source
# File lib/savvy/configurators/redis.rb, line 141
def valid?
  @error.nil?
end

Private Instance Methods

build_redis_url(*parts, without_namespace: false) click to toggle source

@return [String]

# File lib/savvy/configurators/redis.rb, line 148
def build_redis_url(*parts, without_namespace: false)
  ns = without_namespace ? @namespace_prefix : namespace(*parts)

  path = ?/ + [db, ns].compact.join(?/)

  URI.join(@base_uri, path).to_s
end
check_validity!() click to toggle source
# File lib/savvy/configurators/redis.rb, line 156
def check_validity!
  raise Savvy::RedisError, "Redis configuration is invalid: #{@error}", caller unless valid?
end
parse_url!() click to toggle source

@return [void]

# File lib/savvy/configurators/redis.rb, line 161
def parse_url!
  @provided_url = read_url_from_config.freeze

  @parsed_url = URI.parse @provided_url

  @scheme = @parsed_url.scheme == "rediss" ? "rediss" : "redis"
  @host = @parsed_url.host
  @port = @parsed_url.port
  @password = @parsed_url.password
  @userinfo = @parsed_url.userinfo

  path_match = PATH_PATTERN.match @parsed_url.path

  if path_match
    captures =
      if path_match.respond_to?(:named_captures)
        path_match.named_captures
      else
        Hash[path_match.names.zip(path_match.captures)]
      end

    @db = Integer(captures['db'])

    @namespace_prefix = captures['namespace_prefix']
  end

  @base_uri = URI::Generic.new @scheme, @userinfo, @host, @port, nil, '', nil, nil, nil
rescue URI::InvalidURIError => e
  @error = "could not parse redis URL (#{@provided_url})"
end
read_url_from_config() click to toggle source
# File lib/savvy/configurators/redis.rb, line 192
def read_url_from_config
  @config.read_from_env *@config.redis_env_vars, fallback: @config.redis_default_url
end