Skip to content

Commit 24f59aa

Browse files
Initial commit
0 parents  commit 24f59aa

11 files changed

+158
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.gem
2+
.byebug_history

Gemfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
source "https://rubygems.org"
2+
3+
gemspec

Gemfile.lock

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
PATH
2+
remote: .
3+
specs:
4+
rspamd (0.1.0)
5+
activesupport (>= 6.0.0)
6+
addressable (>= 2.7.0)
7+
8+
GEM
9+
remote: https://rubygems.org/
10+
specs:
11+
activesupport (6.0.2.1)
12+
concurrent-ruby (~> 1.0, >= 1.0.2)
13+
i18n (>= 0.7, < 2)
14+
minitest (~> 5.1)
15+
tzinfo (~> 1.1)
16+
zeitwerk (~> 2.2)
17+
addressable (2.7.0)
18+
public_suffix (>= 2.0.2, < 5.0)
19+
byebug (9.1.0)
20+
concurrent-ruby (1.1.5)
21+
crack (0.4.3)
22+
safe_yaml (~> 1.0.0)
23+
hashdiff (1.0.0)
24+
i18n (1.8.2)
25+
concurrent-ruby (~> 1.0)
26+
minitest (5.14.0)
27+
public_suffix (4.0.3)
28+
rake (12.3.3)
29+
safe_yaml (1.0.5)
30+
thread_safe (0.3.6)
31+
tzinfo (1.2.6)
32+
thread_safe (~> 0.1)
33+
webmock (3.8.0)
34+
addressable (>= 2.3.6)
35+
crack (>= 0.3.2)
36+
hashdiff (>= 0.4.0, < 2.0.0)
37+
zeitwerk (2.2.2)
38+
39+
PLATFORMS
40+
ruby
41+
42+
DEPENDENCIES
43+
byebug (~> 9.1)
44+
minitest (~> 5.11)
45+
rake (~> 12.0)
46+
rspamd!
47+
webmock (~> 3.0)
48+
49+
BUNDLED WITH
50+
2.1.4

Rakefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
require "bundler/setup"
2+
require "rake/testtask"
3+
4+
Rake::TestTask.new do |test|
5+
test.libs << "test"
6+
test.test_files = FileList["test/**/*_test.rb"]
7+
end
8+
9+
task default: :test

lib/rspamd.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require "rspamd/client"

lib/rspamd/client.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
require "rspamd/service"
2+
3+
module Rspamd
4+
class Client
5+
def initialize(scheme: "http", host:)
6+
@service = Service.new(scheme: scheme, host: host)
7+
end
8+
9+
def ping
10+
@service.get("/ping")
11+
.then { |response| response.is_a?(Net::HTTPOK) && response.body == "pong" }
12+
end
13+
end
14+
end

lib/rspamd/endpoint.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
require "addressable"
2+
3+
require "active_support/core_ext/object/blank"
4+
require "active_support/core_ext/object/inclusion"
5+
require "active_support/core_ext/object/to_query"
6+
7+
module Rspamd
8+
class Endpoint
9+
attr_reader :scheme, :host
10+
11+
def initialize(scheme: "http", host:)
12+
@scheme = scheme.presence_in(%w[ http https ]) || raise(ArgumentError, "Unsupported scheme: #{scheme.inspect}")
13+
@host = host.presence || raise(ArgumentError, "Expected host, got #{host.inspect}")
14+
end
15+
16+
def base
17+
@base ||= Addressable::URI.new(scheme: scheme, host: host)
18+
end
19+
20+
def url_for(path, params = {})
21+
base.merge(path: path, query: params.to_query).normalize!
22+
end
23+
end
24+
end

lib/rspamd/service.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
require "rspamd/endpoint"
2+
3+
module Rspamd
4+
class Service
5+
def initialize(scheme: "http", host:)
6+
@endpoint = Endpoint.new(scheme: scheme, host: host)
7+
end
8+
9+
def get(path)
10+
Net::HTTP.get_response @endpoint.url_for(path)
11+
end
12+
end
13+
end

rspamd.gemspec

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Gem::Specification.new do |s|
2+
s.name = "rspamd"
3+
s.version = "0.1.0"
4+
s.authors = "George Claghorn"
5+
s.email = "[email protected]"
6+
s.summary = "Client for Rspamd's HTTP API"
7+
s.homepage = "https://github.com/basecamp/rspamd-ruby"
8+
9+
s.required_ruby_version = ">= 2.6.0"
10+
11+
s.add_dependency "activesupport", ">= 6.0.0"
12+
s.add_dependency "addressable", ">= 2.7.0"
13+
14+
s.add_development_dependency "rake", "~> 12.0"
15+
s.add_development_dependency "minitest", "~> 5.11"
16+
s.add_development_dependency "webmock", "~> 3.0"
17+
s.add_development_dependency "byebug", "~> 9.1"
18+
19+
s.files = `git ls-files`.split("\n")
20+
s.test_files = `git ls-files -- test/*`.split("\n")
21+
end

test/client_test.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require "test_helper"
2+
3+
class Rspamd::ClientTest < Minitest::Test
4+
def setup
5+
@client = Rspamd::Client.new(host: "localhost:11333")
6+
end
7+
8+
def test_successfully_pinging
9+
stub_request(:get, "http://localhost:11333/ping").to_return(status: 200, body: "pong")
10+
assert @client.ping
11+
end
12+
13+
def test_unsuccessfully_pinging
14+
stub_request(:get, "http://localhost:11333/ping").to_return(status: 500)
15+
assert !@client.ping
16+
end
17+
end

test/test_helper.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
require "minitest/autorun"
2+
require "webmock/minitest"
3+
require "rspamd"
4+
require "byebug"

0 commit comments

Comments
 (0)