From 56a188b39df70f064a12d41333b4209dd92dced1 Mon Sep 17 00:00:00 2001 From: Ryan Selk Date: Mon, 13 Apr 2015 10:13:05 -0600 Subject: [PATCH] Add handling of path option in redis client The URI parser puts a path param into the options hash, this is not handled correctly by the redis client. --- lib/logstash-logger/device/redis.rb | 14 ++++++++++++++ spec/device/redis_spec.rb | 30 +++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/lib/logstash-logger/device/redis.rb b/lib/logstash-logger/device/redis.rb index fb94d85..5e4c1c8 100644 --- a/lib/logstash-logger/device/redis.rb +++ b/lib/logstash-logger/device/redis.rb @@ -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) @@ -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 @@ -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 diff --git a/spec/device/redis_spec.rb b/spec/device/redis_spec.rb index 0e1d402..f189399 100644 --- a/spec/device/redis_spec.rb +++ b/spec/device/redis_spec.rb @@ -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