Skip to content

Commit 0ec47d1

Browse files
authored
Merge pull request #10 from basecamp/enhance-with-rails-features
Add ClientStub, setup/client_for API, and Railtie
2 parents bc83e34 + 7ac8824 commit 0ec47d1

File tree

8 files changed

+309
-67
lines changed

8 files changed

+309
-67
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,13 @@ jobs:
55
runs-on: ubuntu-latest
66

77
steps:
8-
- uses: actions/checkout@v1
8+
- uses: actions/checkout@v4
99

1010
- name: Set up Ruby
1111
uses: ruby/setup-ruby@v1
1212
with:
13-
ruby-version: 2.7.8
14-
rubygems: latest
15-
16-
- name: Cache gem dependencies
17-
uses: actions/cache@v1
18-
with:
19-
path: vendor/bundle
20-
key: ${{ runner.os }}-bundler-${{ hashFiles('**/Gemfile.lock') }}
21-
restore-keys: ${{ runner.os }}-bundler-
22-
23-
- name: Install dependencies
24-
run: gem install bundler && bundle install --jobs 4 --retry 3 --path vendor/bundle
13+
ruby-version: 3.2
14+
bundler-cache: true
2515

2616
- name: Run tests
2717
run: bundle exec rake test

Gemfile.lock

Lines changed: 108 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,138 @@
11
GIT
22
remote: https://github.com/basecamp/house-style.git
3-
revision: b3ad65254828e8e8019a0d9a6205aff9ad206a77
3+
revision: 6f73ca5c3fd5002b48aeede2d980f9b8fe047d55
44
branch: main
55
specs:
6-
rubocop-37signals (1.0.0)
7-
rubocop
8-
rubocop-minitest
9-
rubocop-performance
10-
rubocop-rails
6+
rubocop-37signals (1.2.1)
7+
rubocop (>= 1.72)
8+
rubocop-minitest (>= 0.37.0)
9+
rubocop-performance (>= 1.24)
10+
rubocop-rails (>= 2.30)
1111

1212
PATH
1313
remote: .
1414
specs:
1515
rspamd-ruby (1.0.0)
16+
activesupport (>= 6.0)
1617

1718
GEM
1819
remote: https://rubygems.org/
1920
specs:
20-
activesupport (7.0.7.2)
21-
concurrent-ruby (~> 1.0, >= 1.0.2)
21+
activesupport (8.1.2)
22+
base64
23+
bigdecimal
24+
concurrent-ruby (~> 1.0, >= 1.3.1)
25+
connection_pool (>= 2.2.5)
26+
drb
2227
i18n (>= 1.6, < 2)
28+
json
29+
logger (>= 1.4.2)
2330
minitest (>= 5.1)
24-
tzinfo (~> 2.0)
25-
addressable (2.8.1)
26-
public_suffix (>= 2.0.2, < 6.0)
27-
ast (2.4.2)
28-
concurrent-ruby (1.2.2)
29-
crack (0.4.3)
30-
safe_yaml (~> 1.0.0)
31-
debug (1.7.1)
32-
irb (>= 1.5.0)
33-
reline (>= 0.3.1)
34-
hashdiff (1.0.0)
35-
i18n (1.14.1)
31+
securerandom (>= 0.3)
32+
tzinfo (~> 2.0, >= 2.0.5)
33+
uri (>= 0.13.1)
34+
addressable (2.8.9)
35+
public_suffix (>= 2.0.2, < 8.0)
36+
ast (2.4.3)
37+
base64 (0.3.0)
38+
bigdecimal (4.0.1)
39+
concurrent-ruby (1.3.6)
40+
connection_pool (3.0.2)
41+
crack (1.0.1)
42+
bigdecimal
43+
rexml
44+
date (3.5.1)
45+
debug (1.11.1)
46+
irb (~> 1.10)
47+
reline (>= 0.3.8)
48+
drb (2.2.3)
49+
erb (6.0.2)
50+
hashdiff (1.2.1)
51+
i18n (1.14.8)
3652
concurrent-ruby (~> 1.0)
37-
io-console (0.6.0)
38-
irb (1.6.2)
39-
reline (>= 0.3.0)
40-
json (2.6.3)
41-
minitest (5.17.0)
42-
parallel (1.22.1)
43-
parser (3.2.1.0)
53+
io-console (0.8.2)
54+
irb (1.17.0)
55+
pp (>= 0.6.0)
56+
prism (>= 1.3.0)
57+
rdoc (>= 4.0.0)
58+
reline (>= 0.4.2)
59+
json (2.18.1)
60+
json-schema (6.1.0)
61+
addressable (~> 2.8)
62+
bigdecimal (>= 3.1, < 5)
63+
language_server-protocol (3.17.0.5)
64+
lint_roller (1.1.0)
65+
logger (1.7.0)
66+
mcp (0.8.0)
67+
json-schema (>= 4.1)
68+
minitest (6.0.2)
69+
drb (~> 2.0)
70+
prism (~> 1.5)
71+
parallel (1.27.0)
72+
parser (3.3.10.2)
4473
ast (~> 2.4.1)
45-
public_suffix (5.0.0)
46-
rack (3.0.8)
74+
racc
75+
pp (0.6.3)
76+
prettyprint
77+
prettyprint (0.2.0)
78+
prism (1.9.0)
79+
psych (5.3.1)
80+
date
81+
stringio
82+
public_suffix (7.0.5)
83+
racc (1.8.1)
84+
rack (3.2.5)
4785
rainbow (3.1.1)
48-
rake (13.0.1)
49-
regexp_parser (2.7.0)
50-
reline (0.3.2)
86+
rake (13.3.1)
87+
rdoc (7.2.0)
88+
erb
89+
psych (>= 4.0.0)
90+
tsort
91+
regexp_parser (2.11.3)
92+
reline (0.6.3)
5193
io-console (~> 0.5)
52-
rexml (3.2.5)
53-
rubocop (1.45.1)
94+
rexml (3.4.4)
95+
rubocop (1.85.1)
5496
json (~> 2.3)
97+
language_server-protocol (~> 3.17.0.2)
98+
lint_roller (~> 1.1.0)
99+
mcp (~> 0.6)
55100
parallel (~> 1.10)
56-
parser (>= 3.2.0.0)
101+
parser (>= 3.3.0.2)
57102
rainbow (>= 2.2.2, < 4.0)
58-
regexp_parser (>= 1.8, < 3.0)
59-
rexml (>= 3.2.5, < 4.0)
60-
rubocop-ast (>= 1.24.1, < 2.0)
103+
regexp_parser (>= 2.9.3, < 3.0)
104+
rubocop-ast (>= 1.49.0, < 2.0)
61105
ruby-progressbar (~> 1.7)
62-
unicode-display_width (>= 2.4.0, < 3.0)
63-
rubocop-ast (1.26.0)
64-
parser (>= 3.2.1.0)
65-
rubocop-minitest (0.27.0)
66-
rubocop (>= 0.90, < 2.0)
67-
rubocop-performance (1.16.0)
68-
rubocop (>= 1.7.0, < 2.0)
69-
rubocop-ast (>= 0.4.0)
70-
rubocop-rails (2.19.1)
106+
unicode-display_width (>= 2.4.0, < 4.0)
107+
rubocop-ast (1.49.0)
108+
parser (>= 3.3.7.2)
109+
prism (~> 1.7)
110+
rubocop-minitest (0.39.1)
111+
lint_roller (~> 1.1)
112+
rubocop (>= 1.75.0, < 2.0)
113+
rubocop-ast (>= 1.38.0, < 2.0)
114+
rubocop-performance (1.26.1)
115+
lint_roller (~> 1.1)
116+
rubocop (>= 1.75.0, < 2.0)
117+
rubocop-ast (>= 1.47.1, < 2.0)
118+
rubocop-rails (2.34.3)
71119
activesupport (>= 4.2.0)
120+
lint_roller (~> 1.1)
72121
rack (>= 1.1)
73-
rubocop (>= 1.33.0, < 2.0)
74-
ruby-progressbar (1.11.0)
75-
safe_yaml (1.0.5)
122+
rubocop (>= 1.75.0, < 2.0)
123+
rubocop-ast (>= 1.44.0, < 2.0)
124+
ruby-progressbar (1.13.0)
125+
securerandom (0.4.1)
126+
stringio (3.2.0)
127+
tsort (0.2.0)
76128
tzinfo (2.0.6)
77129
concurrent-ruby (~> 1.0)
78-
unicode-display_width (2.4.2)
79-
webmock (3.8.0)
80-
addressable (>= 2.3.6)
130+
unicode-display_width (3.2.0)
131+
unicode-emoji (~> 4.1)
132+
unicode-emoji (4.2.0)
133+
uri (1.1.1)
134+
webmock (3.26.1)
135+
addressable (>= 2.8.0)
81136
crack (>= 0.3.2)
82137
hashdiff (>= 0.4.0, < 2.0.0)
83138

lib/rspamd-ruby.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,37 @@
1+
require "active_support/core_ext/hash/keys"
12
require "rspamd/client"
3+
require "rspamd/client_stub"
24
require "rspamd/errors"
5+
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/client_stub.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
require "rspamd/check/result"
2+
3+
module Rspamd
4+
class ClientStub
5+
HAM_RESULT = Check::Result.new(
6+
"score" => 0.0,
7+
"required_score" => 15.0,
8+
"action" => "no action",
9+
"is_skipped" => false,
10+
"symbols" => {},
11+
"urls" => [],
12+
"emails" => []
13+
).freeze
14+
15+
def ping
16+
true
17+
end
18+
19+
def check(message, headers: {})
20+
HAM_RESULT
21+
end
22+
23+
def spam!(message)
24+
true
25+
end
26+
27+
def ham!(message)
28+
true
29+
end
30+
31+
def add_fuzzy(message, flag: 1, weight: 1)
32+
true
33+
end
34+
35+
def delete_fuzzy(message, flag: 1)
36+
true
37+
end
38+
end
39+
end

lib/rspamd/railtie.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module Rspamd
2+
class Railtie < ::Rails::Railtie
3+
initializer "rspamd.setup" do
4+
config_path = ::Rails.root.join("config/rspamd.yml")
5+
6+
if config_path.exist?
7+
Rspamd.setup(::Rails.application.config_for(:rspamd))
8+
end
9+
end
10+
end
11+
end

rspamd-ruby.gemspec

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ Gem::Specification.new do |s|
88
s.summary = "Client for Rspamd's HTTP API"
99
s.homepage = "https://github.com/basecamp/rspamd-ruby"
1010

11-
s.required_ruby_version = ">= 2.7.8"
11+
s.required_ruby_version = ">= 3.2"
12+
13+
s.add_dependency "activesupport", ">= 6.0"
1214

1315
s.add_development_dependency "rake", "~> 13.0"
1416
s.add_development_dependency "minitest", "> 5.11"

test/client_stub_test.rb

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
require "test_helper"
2+
3+
class Rspamd::ClientStubTest < Minitest::Test
4+
def setup
5+
@stub = Rspamd::ClientStub.new
6+
end
7+
8+
def test_ping_returns_true
9+
assert @stub.ping
10+
end
11+
12+
def test_check_returns_ham_result
13+
result = @stub.check("message body")
14+
assert result.ham?
15+
assert_not result.spam?
16+
assert_equal 0.0, result.score
17+
assert_equal 15.0, result.required_score
18+
assert_equal "no action", result.action
19+
end
20+
21+
def test_check_accepts_headers
22+
result = @stub.check("message body", headers: { "Settings-Id" => "outbound" })
23+
assert result.ham?
24+
end
25+
26+
def test_spam_returns_true
27+
assert @stub.spam!("message body")
28+
end
29+
30+
def test_ham_returns_true
31+
assert @stub.ham!("message body")
32+
end
33+
34+
def test_add_fuzzy_returns_true
35+
assert @stub.add_fuzzy("message body")
36+
end
37+
38+
def test_delete_fuzzy_returns_true
39+
assert @stub.delete_fuzzy("message body")
40+
end
41+
42+
private
43+
def assert_not(value)
44+
assert !value
45+
end
46+
end

0 commit comments

Comments
 (0)