Skip to content

Commit

Permalink
When connecting, allow extra parameter 'password'. Fixes #10
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Wille committed Feb 2, 2016
1 parent 5b1c555 commit 223233a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
12 changes: 12 additions & 0 deletions spec/password_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require "./spec_helper"
# This spec is commented out because it requires a Redis instance
# with password authentication.
#
# describe Redis do
# describe ".new with password" do
# it "connects with a password" do
# redis = Redis.new(host: "localhost", port: 6379, password: "foobared")
# redis.set("foo", "bar")
# end
# end
# end
10 changes: 8 additions & 2 deletions src/redis.cr
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class Redis
# * host - the host to connect to
# * port - the port to connect to
# * unixsocket - instead of using TCP, you can connect to Redis via a Unix domain socket by passing its path here (e.g. "/tmp/redis.sock")
# * password - the password for authentication against the server. This is a convenience which saves you the extra call to the Redis `auth` command.
#
# Example:
#
Expand All @@ -71,9 +72,13 @@ class Redis
# redis = Redis.new(unixsocket: "/tmp/redis.sock")
# ...
# ```
def initialize(host = "localhost", port = 6379, unixsocket = nil)
def initialize(host = "localhost", port = 6379, unixsocket = nil, password = nil)
@connection = Connection.new(host, port, unixsocket)
@strategy = Redis::Strategy::SingleStatement.new(@connection)

if password
auth(password)
end
end

# Opens a Redis connection, yields the given block with a Redis object and closes the connection.
Expand All @@ -82,6 +87,7 @@ class Redis
# * host - the host to connect to
# * port - the port to connect to
# * unixsocket - instead of using TCP, you can connect to Redis via a Unix domain socket by passing its path here (e.g. "/tmp/redis.sock")
# * password - the password for authentication against the server. This is a convenience which saves you the extra call to the Redis `auth` command.
#
# Example:
#
Expand All @@ -90,7 +96,7 @@ class Redis
# redis.incr("counter")
# end
# ```
def self.open(host = "localhost", port = 6379, unixsocket = nil)
def self.open(host = "localhost", port = 6379, unixsocket = nil, password = nil)
redis = Redis.new(host, port, unixsocket)
begin
yield(redis)
Expand Down

0 comments on commit 223233a

Please sign in to comment.