Skip to content

Commit e853cc8

Browse files
committed
Allow host app to configure Redis client options
1 parent a30af9f commit e853cc8

File tree

5 files changed

+97
-7
lines changed

5 files changed

+97
-7
lines changed

lib/kracken/config.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
module Kracken
44
class Config
55
attr_accessor :app_id, :app_secret
6-
attr_writer :provider_url
6+
attr_writer :provider_url, :redis_options
77

88
# @deprecated the associated reader returns static `::User`
99
attr_writer :user_class
@@ -16,6 +16,10 @@ def provider_url
1616
@provider_url ||= PROVIDER_URL
1717
end
1818

19+
def redis_options
20+
@redis_options ||= {}
21+
end
22+
1923
def user_class
2024
::User
2125
end

lib/kracken/session_manager.rb

+19-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
11
# frozen_string_literal: true
22

3+
require 'redis'
4+
35
module Kracken
46
module SessionManager
57
def self.conn
6-
@conn ||= if ENV["REDIS_SESSION_URL"].present?
7-
Redis.new(url: ENV["REDIS_SESSION_URL"])
8-
else
9-
NullRedis.new
10-
end
8+
@conn ||=
9+
begin
10+
default_redis_options = { url: ENV['REDIS_SESSION_URL'] }
11+
redis_options = default_redis_options.merge(Kracken.config.redis_options).compact
12+
13+
if redis_options.any?
14+
Redis.new(**redis_options)
15+
else
16+
NullRedis.new
17+
end
18+
end
19+
end
20+
21+
# @api private
22+
# For use in testing only
23+
def self.reset_conn
24+
@conn = nil
1125
end
1226

1327
def self.get(user_id)

spec/kracken/config_spec.rb

+14-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
module Kracken
66
RSpec.describe Config do
77

8-
subject(:config){ Kracken::Config.new }
8+
subject(:config) { Kracken::Config.new }
99

1010
it "sets a default url" do
1111
expect(config.provider_url).to eq "https://account.radiusnetworks.com"
@@ -22,5 +22,18 @@ module Kracken
2222
expect(config.provider_url).to eq "http://joe.com"
2323
end
2424

25+
describe "#redis_options" do
26+
it 'defaults to {}' do
27+
expect(config.redis_options).to eq({})
28+
end
29+
end
30+
31+
describe "#redis_options=" do
32+
it 'allows redis_options to be set' do
33+
config.redis_options = { url: "redis://www.example.com" }
34+
35+
expect(config.redis_options).to eq({ url: "redis://www.example.com" })
36+
end
37+
end
2538
end
2639
end

spec/kracken/session_manager_spec.rb

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails_helper'
4+
5+
module Kracken
6+
RSpec.describe SessionManager do
7+
include Kracken::Spec::UsingEnv
8+
9+
describe "::conn" do
10+
before { SessionManager.reset_conn }
11+
12+
context "when the REDIS_SESSION_URL env var is present" do
13+
it 'is a Redis instance' do
14+
using_env({ "REDIS_SESSION_URL" => "redis://www.example.com" }) do
15+
expect(SessionManager.conn).to be_an_instance_of Redis
16+
end
17+
end
18+
end
19+
20+
context "when the REDIS_SESSION_URL env var is not present" do
21+
it 'is a NullRedis instance' do
22+
expect(SessionManager.conn).to be_an_instance_of SessionManager::NullRedis
23+
end
24+
end
25+
end
26+
end
27+
end

spec/support/using_env.rb

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# frozen_string_literal: true
2+
3+
module Kracken
4+
module Spec
5+
module UsingEnv
6+
# By forcing the use of a block, this makes working within the context of
7+
# a single spec much easier. If this needs to be wrapped around multiple
8+
# specs, then an appropriate #around(:example) hook may be used.
9+
#
10+
# This is stolen with love from https://github.com/RadiusNetworks/captain/blob/master/spec/support/using_env.rb
11+
#
12+
# @param env_stubs [Hash{String => Object}]
13+
#
14+
# @yieldreturn
15+
def using_env(env_stubs) # rubocop:disable Metrics/MethodLength
16+
keys_to_delete = env_stubs.keys - ENV.keys
17+
original_values = env_stubs.each_with_object({}) { |(k, v), env|
18+
env[k] = ENV[k] if ENV.key?(k)
19+
ENV[k] = v
20+
}
21+
yield
22+
ensure
23+
keys_to_delete.each do |k|
24+
ENV.delete(k)
25+
end
26+
original_values.each do |k, v|
27+
ENV[k] = v
28+
end
29+
end
30+
end
31+
end
32+
end

0 commit comments

Comments
 (0)