Skip to content
Closed
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
17 changes: 17 additions & 0 deletions lib/redis/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,24 @@ def ensure_connected
end
end

# convert all keys to symbols, returning new hash
# inspired by activesupport
def symbolize_keys(hash)
keys = hash.keys
ret = {}
keys.each do |key|
v = hash[key]
if v.is_a?(Hash)
v = symbolize_keys(v)
end
ret[(key.to_sym rescue key) || key] = v
end
ret
end


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

url = options[:url] || ENV["REDIS_URL"]
Expand Down
27 changes: 27 additions & 0 deletions test/internals_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,38 @@ def test_redis_current
assert_equal 1, Redis.current.client.db
end

def test_redis_current_string_opts
# set in test above
assert_equal "127.0.0.1", Redis.current.client.host
assert_equal 6380, Redis.current.client.port
assert_equal 1, Redis.current.client.db

Redis.current = Redis.new(OPTIONS.merge("port" => 6382, "db" => 2))

t = Thread.new do
assert_equal "127.0.0.1", Redis.current.client.host
assert_equal 6382, Redis.current.client.port
assert_equal 2, Redis.current.client.db
end

t.join

assert_equal "127.0.0.1", Redis.current.client.host
assert_equal 6382, Redis.current.client.port
assert_equal 2, Redis.current.client.db
end


def test_default_id_with_host_and_port
redis = Redis.new(OPTIONS.merge(:host => "host", :port => "1234", :db => 0))
assert_equal "redis://host:1234/0", redis.client.id
end

def test_default_id_with_host_and_port_string_opts
redis = Redis.new(OPTIONS.merge("host" => "host2", "port" => "1235", "db" => 1))
assert_equal "redis://host2:1235/1", redis.client.id
end

def test_default_id_with_host_and_port_and_explicit_scheme
redis = Redis.new(OPTIONS.merge(:host => "host", :port => "1234", :db => 0, :scheme => "foo"))
assert_equal "redis://host:1234/0", redis.client.id
Expand Down
2 changes: 1 addition & 1 deletion test/url_param_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def test_url_defaults_to_______________
end

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

assert_equal "foo.com", redis.client.host
assert_equal 999, redis.client.port
Expand Down