class Savvy::Configurators::Redis
Constants
- DEFAULT_SEPARATOR
- PATH_PATTERN
Attributes
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
@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
@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
@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
@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
@!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
@!attribute [r] host @return [String]
# File lib/savvy/configurators/redis.rb, line 58 def host check_validity! @host end
# 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
@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
@!attribute [r] password @return [String, nil]
# File lib/savvy/configurators/redis.rb, line 80 def password check_validity! @password end
@!attribute [r] port @return [Integer]
# File lib/savvy/configurators/redis.rb, line 88 def port check_validity! @port end
@!attribute [r] scheme @return [“rediss”, “redis”]
# File lib/savvy/configurators/redis.rb, line 96 def scheme check_validity! @scheme end
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
@!attribute [r] userinfo @return [String, nil]
# File lib/savvy/configurators/redis.rb, line 113 def userinfo check_validity! @userinfo end
# File lib/savvy/configurators/redis.rb, line 119 def uses_ssl? @scheme == "rediss" end
# File lib/savvy/configurators/redis.rb, line 141 def valid? @error.nil? end
Private Instance Methods
@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
# File lib/savvy/configurators/redis.rb, line 156 def check_validity! raise Savvy::RedisError, "Redis configuration is invalid: #{@error}", caller unless valid? end
@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
# 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