Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add handling of path option in redis client #42

Merged
merged 1 commit into from
Apr 17, 2015
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
14 changes: 14 additions & 0 deletions lib/logstash-logger/device/redis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ class Redis < Connectable
def initialize(opts)
super
@list = opts.delete(:list) || DEFAULT_LIST

normalize_path(opts)

@redis_options = opts

@batch_events = opts.fetch(:batch_events, 50)
Expand All @@ -21,6 +24,7 @@ def initialize(opts)
buffer_initialize max_items: @batch_events, max_interval: @batch_timeout
end


def connect
@io = ::Redis.new(@redis_options)
end
Expand Down Expand Up @@ -64,6 +68,16 @@ def flush(*args)
end
end
end

private

def normalize_path(opts)
path = opts.fetch(:path, nil)
if path
opts[:db] = path.gsub("/", "").to_i unless path.empty?
opts.delete(:path)
end
end

end
end
Expand Down
30 changes: 30 additions & 0 deletions spec/device/redis_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,34 @@
it "defaults the Redis list to 'logstash'" do
expect(redis_device.list).to eq('logstash')
end

describe "initializer" do
let(:redis_options) { { host: HOST, port: 6379 } }
subject { LogStashLogger::Device::Redis.new(redis_options).connect }

context "path is not blank" do
before do
redis_options[:path] = "/0"
end

it "sets the db" do
expect(Redis).to receive(:new).with(hash_including(db: 0))
subject
end

end

context "path is blank" do
before do
redis_options[:path] = ""
end

it "does not set the db" do
expect(Redis).to receive(:new).with(hash_excluding(:db))
subject
end
end

end

end