Skip to content

Commit 72293b4

Browse files
authored
fix!: Big move to use kwargs as the initializer for classes which include us (#28)
* fix!: Big move to use kwargs as the initializer for classes which include us * fix: Remove breakpoint (Thanks, Copilot) * fix: Do not set header if headers are empty * fix: Mocks are why we cannot have good things * fix: Corrects documentation for new method signature * fix: Cleaner assignment with ||= * docs: Corrects parameter documentation * fix: Removes stray copied client * chore: Updates gem dependencies
1 parent 965b2c3 commit 72293b4

File tree

5 files changed

+21
-17
lines changed

5 files changed

+21
-17
lines changed

Gemfile.lock

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
leopard (0.1.6)
4+
leopard (0.1.7)
55
concurrent-ruby (~> 1.1)
66
dry-configurable (~> 1.3)
77
dry-monads (~> 1.9)
@@ -28,7 +28,7 @@ GEM
2828
dry-core (~> 1.1)
2929
zeitwerk (~> 2.6)
3030
io-console (0.8.1)
31-
json (2.13.1)
31+
json (2.13.2)
3232
language_server-protocol (3.17.0.5)
3333
lint_roller (1.1.0)
3434
logger (1.7.0)
@@ -53,10 +53,10 @@ GEM
5353
racc (1.8.1)
5454
rainbow (3.1.1)
5555
rake (13.3.0)
56-
regexp_parser (2.10.0)
56+
regexp_parser (2.11.0)
5757
reline (0.6.2)
5858
io-console (~> 0.5)
59-
rubocop (1.79.0)
59+
rubocop (1.79.2)
6060
json (~> 2.3)
6161
language_server-protocol (~> 3.17.0.2)
6262
lint_roller (~> 1.1.0)
@@ -66,7 +66,6 @@ GEM
6666
regexp_parser (>= 2.9.3, < 3.0)
6767
rubocop-ast (>= 1.46.0, < 2.0)
6868
ruby-progressbar (~> 1.7)
69-
tsort (>= 0.2.0)
7069
unicode-display_width (>= 2.4.0, < 4.0)
7170
rubocop-ast (1.46.0)
7271
parser (>= 3.3.7.2)
@@ -92,7 +91,6 @@ GEM
9291
simplecov_json_formatter (~> 0.1)
9392
simplecov-html (0.13.2)
9493
simplecov_json_formatter (0.1.4)
95-
tsort (0.2.0)
9694
unicode-display_width (3.1.4)
9795
unicode-emoji (~> 4.0, >= 4.0.4)
9896
unicode-emoji (4.0.4)

examples/echo_endpoint.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
class EchoService
88
include Rubyists::Leopard::NatsApiServer
99

10-
def initialize(a_var = 1)
10+
def initialize(a_var: 1)
1111
logger.info "EchoService initialized with a_var: #{a_var}"
1212
end
1313

@@ -21,7 +21,7 @@ def initialize(a_var = 1)
2121
service_opts: {
2222
name: 'example.echo',
2323
version: '1.0.0',
24-
instance_args: [2],
24+
instance_args: { a_var: 2 },
2525
},
2626
instances: 1,
2727
)

lib/leopard/message_wrapper.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ class MessageWrapper
1010
#
1111
# @!attribute [r] data
1212
# @return [Object] The parsed data from the NATS message.
13+
attr_reader :raw, :data
1314
#
14-
# @!attribute [r] headers
15+
# @!attribute [w] headers
1516
# @return [Hash] The headers from the NATS message.
16-
attr_reader :raw, :data, :headers
17+
attr_accessor :headers
1718

1819
# @param nats_msg [NATS::Message] The NATS message to wrap.
1920
def initialize(nats_msg)
@@ -26,6 +27,7 @@ def initialize(nats_msg)
2627
#
2728
# @return [void]
2829
def respond(payload)
30+
raw.header = headers unless headers.empty?
2931
raw.respond(serialize(payload))
3032
end
3133

lib/leopard/nats_api_server.rb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ def spawn_instances(url, opts, count, workers, blocking)
9898
pool = Concurrent::FixedThreadPool.new(count)
9999
@instance_args = opts.delete(:instance_args) || nil
100100
logger.info "Building #{count} workers with options: #{opts.inspect}, instance_args: #{@instance_args}"
101+
raise ArgumentError, 'instance_args must be a Hash' if @instance_args && !@instance_args.is_a?(Hash)
102+
101103
count.times do
102104
pool.post { build_worker(url, opts, workers, blocking) }
103105
end
@@ -106,18 +108,19 @@ def spawn_instances(url, opts, count, workers, blocking)
106108

107109
# Builds a worker instance and sets it up with the NATS server.
108110
#
109-
# @param url [String] The URL of the NATS server.
110-
# @param opts [Hash] Options for the NATS service.
111+
# @param nats_url [String] The URL of the NATS server.
112+
# @param service_opts [Hash] Options for the NATS service.
111113
# @param workers [Array] The array to store worker instances.
112114
# @param blocking [Boolean] If true, blocks the current thread until the worker is set up.
113115
#
114116
# @return [void]
115-
def build_worker(url, opts, workers, blocking)
116-
worker = @instance_args ? new(*@instance_args) : new
117+
def build_worker(nats_url, service_opts, workers, blocking)
118+
worker = @instance_args ? new(**@instance_args) : new
117119
workers << worker
118-
return worker.setup_worker!(nats_url: url, service_opts: opts) if blocking
120+
args = { nats_url:, service_opts: }
121+
return worker.setup_worker!(**args) if blocking
119122

120-
worker.setup_worker(nats_url: url, service_opts: opts)
123+
worker.setup_worker(**args)
121124
end
122125

123126
# Shuts down the NATS API server gracefully.

test/lib/message_wrapper.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
require 'leopard/message_wrapper'
55

66
class FakeMsg
7-
attr_reader :data, :header, :responded_payload, :error_args
7+
attr_reader :data, :responded_payload, :error_args
8+
attr_accessor :header
89

910
def initialize(data, header = {})
1011
@data = data

0 commit comments

Comments
 (0)