Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# 3.0.2 (unreleased)

* Allow string keys in options hash passed to `Redis.new` or
`Redis.connect`.

* Fix uncaught error triggering unrelated error (synchrony driver).

See f7ffd5f1a628029691084de69e5b46699bb8b96d and #248.


# 3.0.1

* Fix reconnect logic not kicking in on a write error.
Expand Down
16 changes: 15 additions & 1 deletion lib/redis/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ class Redis
class Client

DEFAULTS = {
:url => lambda { ENV["REDIS_URL"] },
:scheme => "redis",
:host => "127.0.0.1",
:port => 6379,
:path => nil,
:timeout => 5.0,
:password => nil,
:db => 0,
:driver => nil,
:id => nil,
}

def scheme
Expand Down Expand Up @@ -295,8 +298,19 @@ def ensure_connected

def _parse_options(options)
defaults = DEFAULTS.dup
options = options.dup

url = options[:url] || ENV["REDIS_URL"]
defaults.keys.each do |key|
# Fill in defaults if needed
if defaults[key].respond_to?(:call)
defaults[key] = defaults[key].call
end

# Symbolize only keys that are needed
options[key] = options[key.to_s] if options.has_key?(key.to_s)
end

url = options[:url] || defaults[:url]

# Override defaults from URL if given
if url
Expand Down
26 changes: 26 additions & 0 deletions test/url_param_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ def test_allows_to_pass_in_a_url
assert_equal "secr3t", redis.client.password
end

def test_allows_to_pass_in_a_url_with_string_key
redis = Redis.new "url" => "redis://:secr3t@foo.com:999/2"

assert_equal "foo.com", redis.client.host
assert_equal 999, redis.client.port
assert_equal 2, redis.client.db
assert_equal "secr3t", redis.client.password
end

def test_override_url_if_path_option_is_passed
redis = Redis.new :url => "redis://:secr3t@foo.com/foo:999/2", :path => "/tmp/redis.sock"

Expand All @@ -32,6 +41,14 @@ def test_override_url_if_path_option_is_passed
assert_equal nil, redis.client.port
end

def test_override_url_if_path_option_is_passed_with_string_key
redis = Redis.new :url => "redis://:secr3t@foo.com/foo:999/2", "path" => "/tmp/redis.sock"

assert_equal "/tmp/redis.sock", redis.client.path
assert_equal nil, redis.client.host
assert_equal nil, redis.client.port
end

def test_overrides_url_if_another_connection_option_is_passed
redis = Redis.new :url => "redis://:secr3t@foo.com:999/2", :port => 1000

Expand All @@ -41,6 +58,15 @@ def test_overrides_url_if_another_connection_option_is_passed
assert_equal "secr3t", redis.client.password
end

def test_overrides_url_if_another_connection_option_is_passed_with_string_key
redis = Redis.new :url => "redis://:secr3t@foo.com:999/2", "port" => 1000

assert_equal "foo.com", redis.client.host
assert_equal 1000, redis.client.port
assert_equal 2, redis.client.db
assert_equal "secr3t", redis.client.password
end

def test_does_not_modify_the_passed_options
options = { :url => "redis://:secr3t@foo.com:999/2" }

Expand Down