Skip to content

Commit caecd0e

Browse files
committed
[FEATURE] Implement Kafka-specific Create command
The command sends message(s) to the Kafka broker(s). The method `#using` allows reloading the command by updating its dataset. == Added * ROM::Kafka::Commands (the namespace) * ROM::Kafka::Commands::Create
1 parent c59c4fc commit caecd0e

4 files changed

Lines changed: 114 additions & 1 deletion

File tree

config/metrics/flay.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
minimum_score: 8
2+
minimum_score: 9

lib/rom/kafka.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ module Kafka
1919
require_relative "kafka/dataset"
2020
require_relative "kafka/gateway"
2121
require_relative "kafka/relation"
22+
require_relative "kafka/create"
2223

2324
end # module Kafka
2425

lib/rom/kafka/create.rb

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# encoding: utf-8
2+
3+
module ROM::Kafka
4+
5+
# The namespace for Kafka-specific ROM commands
6+
#
7+
module Commands
8+
9+
# The Kafka-specific implementation of ROM::Commands::Create
10+
#
11+
# @example
12+
# ROM.use(:auto_registration)
13+
# ROM.setup(:kafka, :consumer, "localhost:9092")
14+
#
15+
# class Users < ROM::Relation[:kafka]
16+
# dataset "log.users"
17+
# end
18+
#
19+
# class AddUsers < ROM::Commands::Create[:kafka]
20+
# end
21+
#
22+
# rom = ROM.finalize.env
23+
# rom.relation(:users).using(max_wait_ms: 100).offset(10).to_a
24+
# # => [{ value: "something", topic: "log", key: "users", offset: 10 }]
25+
#
26+
class Create < ROM::Commands::Create
27+
28+
adapter :kafka
29+
30+
# Sends tuples to the current topic/partition of Kafka
31+
#
32+
# @param (see ROM::Kafka::Dataset#publish)
33+
#
34+
# @return (see ROM::Kafka::Dataset#publish)
35+
#
36+
def execute(*tuples)
37+
dataset.publish(*tuples)
38+
end
39+
40+
# Returns the new commmand where relation's dataset is updated
41+
# using given options
42+
#
43+
# @param [Hash] options
44+
#
45+
# @return [ROM::Kafka::Commands::Create]
46+
#
47+
def using(options)
48+
new relation.using(options)
49+
end
50+
51+
end # class Create
52+
53+
end # module Commands
54+
55+
end # module ROM::Kafka

spec/unit/create_spec.rb

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# encoding: utf-8
2+
3+
describe ROM::Kafka::Commands::Create do
4+
5+
let(:command) { described_class.new relation }
6+
let(:relation) { double :relation, using: updated, dataset: dataset }
7+
let(:updated) { double :updated }
8+
let(:dataset) { double :dataset, publish: tuples }
9+
let(:tuples) { [double(:first_message), double(:second_message)] }
10+
11+
describe ".adapter" do
12+
subject { described_class.adapter }
13+
14+
it { is_expected.to eql(:kafka) }
15+
end # describe .adapter
16+
17+
describe ".new" do
18+
subject { command }
19+
20+
it { is_expected.to be_kind_of ROM::Commands::Create }
21+
end # describe .new
22+
23+
describe "#relation" do
24+
subject { command.relation }
25+
26+
it { is_expected.to eql(relation) }
27+
end # describe #relation
28+
29+
describe "#using" do
30+
subject { command.using(options) }
31+
32+
let(:options) { { foo: :FOO, bar: :BAR } }
33+
34+
it "returns a relation" do
35+
expect(subject).to be_kind_of described_class
36+
end
37+
38+
it "updates the relation" do
39+
expect(relation).to receive(:using).with(options)
40+
expect(subject.relation).to eql(updated)
41+
end
42+
end # describe #using
43+
44+
describe "#execute" do
45+
subject { command.execute(*tuples) }
46+
47+
it "publishes tuples to the dataset" do
48+
expect(dataset).to receive(:publish).with(*tuples)
49+
subject
50+
end
51+
52+
it "returns tuples" do
53+
expect(subject).to eql(tuples)
54+
end
55+
end # describe #execute
56+
57+
end # describe ROM::Kafka::Relation

0 commit comments

Comments
 (0)