lib/redis_client/url_config.rb in redis-client-0.19.1 vs lib/redis_client/url_config.rb in redis-client-0.20.0

- old
+ new

@@ -2,30 +2,45 @@ require "uri" class RedisClient class URLConfig - DEFAULT_SCHEMA = "redis" - SSL_SCHEMA = "rediss" - attr_reader :url, :uri def initialize(url) @url = url @uri = URI(url) - unless uri.scheme == DEFAULT_SCHEMA || uri.scheme == SSL_SCHEMA - raise ArgumentError, "Invalid URL: #{url.inspect}" + @unix = false + @ssl = false + case uri.scheme + when "redis" + # expected + when "rediss" + @ssl = true + when "unix", nil + @unix = true + else + raise ArgumentError, "Unknown URL scheme: #{url.inspect}" end end def ssl? - @uri.scheme == SSL_SCHEMA + @ssl end def db - db_path = uri.path&.delete_prefix("/") - Integer(db_path) if db_path && !db_path.empty? + unless @unix + db_path = uri.path&.delete_prefix("/") + return Integer(db_path) if db_path && !db_path.empty? + end + + unless uri.query.nil? || uri.query.empty? + _, db_query = URI.decode_www_form(uri.query).find do |key, _| + key == "db" + end + return Integer(db_query) if db_query && !db_query.empty? + end end def username uri.user if uri.password && !uri.user.empty? end @@ -40,9 +55,15 @@ def host return if uri.host.nil? || uri.host.empty? uri.host.sub(/\A\[(.*)\]\z/, '\1') + end + + def path + if @unix + File.join(*[uri.host, uri.path].compact) + end end def port return unless uri.port