Skip to content

Commit bbf5ce6

Browse files
rosaclaude
andcommitted
Simplify API: move setup/client_for from Rspamd::Rails to Rspamd module
The Rails module name was misleading since this code works outside Rails. Moving it to the top-level Rspamd module simplifies the API: Rspamd.setup(config) Rspamd.client_for(:outbound) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 74a4bf9 commit bbf5ce6

5 files changed

Lines changed: 98 additions & 105 deletions

File tree

lib/rspamd-ruby.rb

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,37 @@
1+
require "active_support/core_ext/hash/keys"
12
require "rspamd/client"
23
require "rspamd/client_stub"
34
require "rspamd/errors"
4-
require "rspamd/rails"
55
require "rspamd/railtie" if defined?(::Rails::Railtie)
6+
7+
module Rspamd
8+
class << self
9+
def setup(config)
10+
@config = config.deep_symbolize_keys
11+
@clients = {}
12+
end
13+
14+
def client_for(name)
15+
clients[name] ||= enabled? ? build_client(name) : ClientStub.new
16+
end
17+
18+
def reset!
19+
@config = nil
20+
@clients = {}
21+
end
22+
23+
private
24+
def clients
25+
@clients ||= {}
26+
end
27+
28+
def enabled?
29+
@config&.dig(:enabled)
30+
end
31+
32+
def build_client(name)
33+
settings = @config.fetch(name) { raise ArgumentError, "No rspamd configuration for #{name.inspect}" }
34+
Client.new(**settings.slice(:host, :port, :password))
35+
end
36+
end
37+
end

lib/rspamd/rails.rb

Lines changed: 0 additions & 37 deletions
This file was deleted.

lib/rspamd/railtie.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
require "rspamd/rails"
2-
31
module Rspamd
42
class Railtie < ::Rails::Railtie
53
initializer "rspamd.setup" do
64
config_path = ::Rails.root.join("config/rspamd.yml")
75

86
if config_path.exist?
9-
Rspamd::Rails.setup(::Rails.application.config_for(:rspamd))
7+
Rspamd.setup(::Rails.application.config_for(:rspamd))
108
end
119
end
1210
end

test/rails_test.rb

Lines changed: 0 additions & 64 deletions
This file was deleted.

test/setup_test.rb

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
require "test_helper"
2+
3+
class Rspamd::SetupTest < Minitest::Test
4+
def teardown
5+
Rspamd.reset!
6+
end
7+
8+
def test_returns_stub_when_disabled
9+
Rspamd.setup("enabled" => false, "outbound" => { "host" => "localhost", "port" => 11334 })
10+
11+
client = Rspamd.client_for(:outbound)
12+
assert_instance_of Rspamd::ClientStub, client
13+
end
14+
15+
def test_returns_client_when_enabled
16+
Rspamd.setup("enabled" => true, "outbound" => { "host" => "rspamd.example.com", "port" => 11334 })
17+
18+
client = Rspamd.client_for(:outbound)
19+
assert_instance_of Rspamd::Client, client
20+
end
21+
22+
def test_caches_client_instances
23+
Rspamd.setup("enabled" => true, "outbound" => { "host" => "localhost", "port" => 11334 })
24+
25+
client1 = Rspamd.client_for(:outbound)
26+
client2 = Rspamd.client_for(:outbound)
27+
assert_same client1, client2
28+
end
29+
30+
def test_returns_stub_when_not_configured
31+
client = Rspamd.client_for(:outbound)
32+
assert_instance_of Rspamd::ClientStub, client
33+
end
34+
35+
def test_raises_for_unknown_client_name
36+
Rspamd.setup("enabled" => true, "outbound" => { "host" => "localhost", "port" => 11334 })
37+
38+
assert_raises(ArgumentError) { Rspamd.client_for(:nonexistent) }
39+
end
40+
41+
def test_reset_clears_config_and_clients
42+
Rspamd.setup("enabled" => true, "outbound" => { "host" => "localhost", "port" => 11334 })
43+
Rspamd.client_for(:outbound)
44+
Rspamd.reset!
45+
46+
# After reset, should return stub (no config = disabled)
47+
client = Rspamd.client_for(:outbound)
48+
assert_instance_of Rspamd::ClientStub, client
49+
end
50+
51+
def test_accepts_symbol_keys
52+
Rspamd.setup(enabled: true, outbound: { host: "localhost", port: 11334 })
53+
54+
client = Rspamd.client_for(:outbound)
55+
assert_instance_of Rspamd::Client, client
56+
end
57+
58+
def test_passes_password_to_client
59+
Rspamd.setup("enabled" => true, "outbound" => { "host" => "localhost", "port" => 11334, "password" => "secret" })
60+
61+
client = Rspamd.client_for(:outbound)
62+
assert_equal "secret", client.configuration.password
63+
end
64+
end

0 commit comments

Comments
 (0)