natsy 0.3.1 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +84 -19
- data/lib/natsy.rb +4 -0
- data/lib/natsy/client.rb +7 -60
- data/lib/natsy/config.rb +261 -0
- data/lib/natsy/controller.rb +7 -7
- data/lib/natsy/utils.rb +14 -0
- data/lib/natsy/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c6bc9668d49b9d899ccff640de76b89dfdd2063e44d08d8189a1fcce934e2d70
|
4
|
+
data.tar.gz: 3a7b7f59a818778db5a8fd48c5b34d26842a900e8b340a3be1d8541569c671bf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2c4f742f409308fae258a8c0de19c4072757855073b1ccab1114b5e818bd29c53bdd269aa330942457c2c737ee798f2b1b6606c678055babc815d576fbb9dd78
|
7
|
+
data.tar.gz: 9b879994fe87e9a4430accf0a837a03989cdd50b18388e13877541203f91d47f394af8fd64f43e49a4fd34196dee4ca0e1af2bd9035b2e4061419a419115a123
|
data/README.md
CHANGED
@@ -58,6 +58,57 @@ docker run -p 4222:4222 -p 8222:8222 -p 6222:6222 -ti nats:latest
|
|
58
58
|
|
59
59
|
> **NOTE:** For other methods of running a NATS server, see [the NATS documentation](https://docs.nats.io/nats-server/installation).
|
60
60
|
|
61
|
+
### Configuration
|
62
|
+
|
63
|
+
Use `Natsy::Config::set` to set configuration options. These options can either be set via a `Hash`/keyword arguments passed to the `::set` method, or set by invoking the method with a block and assigning your options to the yielded `Natsy::Config::Options` instance.
|
64
|
+
|
65
|
+
This README will use the following two syntaxes interchangably; remember that they do **exactly the same thing:**
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
Natsy::Config.set(
|
69
|
+
urls: ["nats://foo.bar:4567", "nats://foo.bar:5678"],
|
70
|
+
default_queue: "foobar",
|
71
|
+
logger: Rails.logger,
|
72
|
+
)
|
73
|
+
```
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
Natsy::Config.set do |options|
|
77
|
+
options.urls = ["nats://foo.bar:4567", "nats://foo.bar:5678"]
|
78
|
+
options.default_queue = "foobar"
|
79
|
+
options.logger = Rails.logger
|
80
|
+
end
|
81
|
+
```
|
82
|
+
|
83
|
+
The following options are available:
|
84
|
+
|
85
|
+
- `url`: A single URL string (including protocol, domain, and port) which points to the relevant NATS server (see [here](#setting-nats-server-url-section) for more info)
|
86
|
+
- `urls`: An array of URL strings in case you need to listen to multiple NATS servers (see [here](#setting-nats-server-url-section) for more info)
|
87
|
+
- `logger`: A logger where `natsy` can write helpful information (see [here](#logging-section) for more info)
|
88
|
+
- `default_queue`: The default queue that your application should fall back to if none is given in a more specific context (see [here](#default-queue-section) for more info)
|
89
|
+
|
90
|
+
<a id="setting-nats-server-url-section"></a>
|
91
|
+
|
92
|
+
### Setting the NATS server URL(s)
|
93
|
+
|
94
|
+
Set the URL/URLs at which your NATS server mediates messages.
|
95
|
+
|
96
|
+
```ruby
|
97
|
+
Natsy::Config.set do |options|
|
98
|
+
options.url = "nats://foo.bar:4567"
|
99
|
+
end
|
100
|
+
```
|
101
|
+
|
102
|
+
```ruby
|
103
|
+
Natsy::Config.set do |options|
|
104
|
+
options.urls = ["nats://foo.bar:4567", "nats://foo.bar:5678"]
|
105
|
+
end
|
106
|
+
```
|
107
|
+
|
108
|
+
> **NOTE:** If no `url`/`urls` option is specified, `natsy` will fall back on the default NATS server URL, which is `nats://localhost:4222`.
|
109
|
+
|
110
|
+
<a id="logging-section"></a>
|
111
|
+
|
61
112
|
### Logging
|
62
113
|
|
63
114
|
#### Attaching a logger
|
@@ -68,23 +119,24 @@ Attach a logger to have `natsy` write out logs for messages received, responses
|
|
68
119
|
require 'natsy'
|
69
120
|
require 'logger'
|
70
121
|
|
71
|
-
|
72
|
-
nats_logger
|
73
|
-
|
74
|
-
|
122
|
+
Natsy::Config.set do |options|
|
123
|
+
nats_logger = Logger.new(STDOUT)
|
124
|
+
nats_logger.level = Logger::INFO
|
125
|
+
options.logger = nats_logger
|
126
|
+
end
|
75
127
|
```
|
76
128
|
|
77
129
|
In a Rails application, you might do this instead:
|
78
130
|
|
79
131
|
```ruby
|
80
|
-
Natsy::
|
132
|
+
Natsy::Config.set(logger: Rails.logger)
|
81
133
|
```
|
82
134
|
|
83
135
|
#### Log levels
|
84
136
|
|
85
137
|
The following will be logged at the specified log levels
|
86
138
|
|
87
|
-
- `DEBUG`: Lifecycle events (starting NATS listeners, stopping NATS, reply registration,
|
139
|
+
- `DEBUG`: Lifecycle events (starting NATS listeners, stopping NATS, reply registration, etc.), as well as everything under `INFO`, `WARN`, and `ERROR`
|
88
140
|
- `INFO`: Message activity over NATS (received a message, replied with a message, etc.), as well as everything under `WARN` and `ERROR`
|
89
141
|
- `WARN`: Error handled gracefully (listening restarted due to some exception, etc.), as well as everything under `ERROR`
|
90
142
|
- `ERROR`: Some exception was raised in-thread (error in handler, error in subscription, etc.)
|
@@ -96,13 +148,13 @@ The following will be logged at the specified log levels
|
|
96
148
|
Set a default queue for subscriptions.
|
97
149
|
|
98
150
|
```ruby
|
99
|
-
Natsy::
|
151
|
+
Natsy::Config.set(default_queue: "foobar")
|
100
152
|
```
|
101
153
|
|
102
|
-
Leave the
|
154
|
+
Leave the `default_queue` blank (or assign `nil`) to use no default queue.
|
103
155
|
|
104
156
|
```ruby
|
105
|
-
Natsy::
|
157
|
+
Natsy::Config.set(default_queue: nil)
|
106
158
|
```
|
107
159
|
|
108
160
|
<a id="reply-to-section"></a>
|
@@ -155,15 +207,29 @@ The following should be enough to start a `natsy` setup in your Ruby application
|
|
155
207
|
require 'natsy'
|
156
208
|
require 'logger'
|
157
209
|
|
158
|
-
|
159
|
-
nats_logger
|
210
|
+
Natsy::Config.set do |options|
|
211
|
+
nats_logger = Logger.new(STDOUT)
|
212
|
+
nats_logger.level = Logger::DEBUG
|
160
213
|
|
161
|
-
|
162
|
-
|
214
|
+
options.logger = nats_logger
|
215
|
+
options.urls = ["nats://foo.bar:4567", "nats://foo.bar:5678"]
|
216
|
+
options.default_queue = "foobar"
|
217
|
+
end
|
163
218
|
|
164
|
-
Natsy::Client.reply_to("some.subject")
|
165
|
-
|
166
|
-
|
219
|
+
Natsy::Client.reply_to("some.subject") do |data|
|
220
|
+
"Got it! #{data.inspect}"
|
221
|
+
end
|
222
|
+
|
223
|
+
Natsy::Client.reply_to("some.*.pattern") do |data, subject|
|
224
|
+
"Got #{data} on #{subject}"
|
225
|
+
end
|
226
|
+
|
227
|
+
Natsy::Client.reply_to("subject.in.queue", queue: "barbaz") do
|
228
|
+
{
|
229
|
+
msg: "My turn!",
|
230
|
+
turn: 5,
|
231
|
+
}
|
232
|
+
end
|
167
233
|
|
168
234
|
Natsy::Client.start!
|
169
235
|
```
|
@@ -174,7 +240,7 @@ Natsy::Client.start!
|
|
174
240
|
|
175
241
|
Create controller classes which inherit from `Natsy::Controller` in order to give your message listeners some structure.
|
176
242
|
|
177
|
-
Use the `::default_queue` macro to set a default queue string. If omitted, the controller will fall back on the global default queue assigned
|
243
|
+
Use the `::default_queue` macro to set a default queue string. If omitted, the controller will fall back on the global default queue assigned to `Natsy::Config::default_queue` (as described [here](#default-queue-section)). If no default queue is set in either the controller or globally, then the default queue will be blank. Set the default queue to `nil` in a controller to override the global default queue and explicitly make the default queue blank for that controller.
|
178
244
|
|
179
245
|
Use the `::subject` macro to create a block for listening to that subject segment. Nested calls to `::subject` will append each subsequent subject/pattern string to the last (joined by a periods). There is no limit to the level of nesting.
|
180
246
|
|
@@ -231,8 +297,7 @@ end
|
|
231
297
|
> For example: in a Rails project (assuming you have your NATS controllers in a directory called `app/nats/`), you may want to put something like the following in an initializer (such as `config/initializers/nats.rb`):
|
232
298
|
>
|
233
299
|
> ```ruby
|
234
|
-
> Natsy::
|
235
|
-
> Natsy::Client.default_queue = "foobar"
|
300
|
+
> Natsy::Config.set(logger: Rails.logger, default_queue: "foobar")
|
236
301
|
>
|
237
302
|
> # ...
|
238
303
|
>
|
data/lib/natsy.rb
CHANGED
@@ -3,6 +3,7 @@
|
|
3
3
|
require "nats/client"
|
4
4
|
require_relative "natsy/version"
|
5
5
|
require_relative "natsy/utils"
|
6
|
+
require_relative "natsy/config"
|
6
7
|
require_relative "natsy/client"
|
7
8
|
require_relative "natsy/controller"
|
8
9
|
|
@@ -14,4 +15,7 @@ module Natsy
|
|
14
15
|
|
15
16
|
# New subscription has been added at runtime
|
16
17
|
class NewSubscriptionsError < Natsy::Error; end
|
18
|
+
|
19
|
+
# Invalid options have been provided to +Natsy::Config+
|
20
|
+
class InvalidConfigError < Natsy::Error; end
|
17
21
|
end
|
data/lib/natsy/client.rb
CHANGED
@@ -10,50 +10,6 @@ module Natsy
|
|
10
10
|
# most functionality if desired.
|
11
11
|
class Client
|
12
12
|
class << self
|
13
|
-
# Optional logger for lifecycle events, messages received, etc.
|
14
|
-
attr_reader :logger
|
15
|
-
|
16
|
-
# Optional default queue for message subscription and replies.
|
17
|
-
attr_reader :default_queue
|
18
|
-
|
19
|
-
# Attach a logger to have +natsy+ write out logs for messages
|
20
|
-
# received, responses sent, errors raised, lifecycle events, etc.
|
21
|
-
#
|
22
|
-
# @example
|
23
|
-
# require 'natsy'
|
24
|
-
# require 'logger'
|
25
|
-
#
|
26
|
-
# nats_logger = Logger.new(STDOUT)
|
27
|
-
# nats_logger.level = Logger::INFO
|
28
|
-
#
|
29
|
-
# Natsy::Client.logger = nats_logger
|
30
|
-
#
|
31
|
-
# In a Rails application, you might do this instead:
|
32
|
-
#
|
33
|
-
# @example
|
34
|
-
# Natsy::Client.logger = Rails.logger
|
35
|
-
#
|
36
|
-
def logger=(some_logger)
|
37
|
-
@logger = some_logger
|
38
|
-
log("Set the logger to #{@logger.inspect}")
|
39
|
-
end
|
40
|
-
|
41
|
-
# Set a default queue for subscriptions.
|
42
|
-
#
|
43
|
-
# @example
|
44
|
-
# Natsy::Client.default_queue = "foobar"
|
45
|
-
#
|
46
|
-
# Leave the +::default_queue+ blank (or assign +nil+) to use no default
|
47
|
-
# queue.
|
48
|
-
#
|
49
|
-
# @example
|
50
|
-
# Natsy::Client.default_queue = nil
|
51
|
-
#
|
52
|
-
def default_queue=(some_queue)
|
53
|
-
@default_queue = Utils.presence(some_queue.to_s)
|
54
|
-
log("Setting the default queue to #{@default_queue || '(none)'}", level: :debug)
|
55
|
-
end
|
56
|
-
|
57
13
|
# Returns +true+ if +::start!+ has already been called (meaning the client
|
58
14
|
# is listening to NATS messages). Returns +false+ if it has not yet been
|
59
15
|
# called, or if it has been stopped.
|
@@ -72,8 +28,9 @@ module Natsy
|
|
72
28
|
# method. Pass a subject string as the first argument (either a static
|
73
29
|
# subject string or a pattern to match more than one subject). Specify a
|
74
30
|
# queue (or don't) with the +queue:+ option. If you don't provide the
|
75
|
-
# +queue:+ option, it will be set to the value of
|
76
|
-
# +nil+ (no queue) if a default
|
31
|
+
# +queue:+ option, it will be set to the value of
|
32
|
+
# +Natsy::Config::default_queue+, or to +nil+ (no queue) if a default
|
33
|
+
# queue hasn't been set.
|
77
34
|
#
|
78
35
|
# The result of the given block will be published in reply to the message.
|
79
36
|
# The block is passed two arguments when a message matching the subject is
|
@@ -101,7 +58,7 @@ module Natsy
|
|
101
58
|
# end
|
102
59
|
#
|
103
60
|
def reply_to(subject, queue: nil, &block)
|
104
|
-
queue = Utils.presence(queue) || default_queue
|
61
|
+
queue = Utils.presence(queue) || Config.default_queue
|
105
62
|
queue_desc = " in queue '#{queue}'" if queue
|
106
63
|
log("Registering a reply handler for subject '#{subject}'#{queue_desc}", level: :debug)
|
107
64
|
register_reply!(subject: subject.to_s, handler: block, queue: queue.to_s)
|
@@ -180,17 +137,7 @@ module Natsy
|
|
180
137
|
end
|
181
138
|
|
182
139
|
def log(text, level: :info, indent: 0)
|
183
|
-
|
184
|
-
|
185
|
-
timestamp = Time.now.to_s
|
186
|
-
text_lines = text.split("\n")
|
187
|
-
indentation = indent.is_a?(String) ? indent : (" " * indent)
|
188
|
-
|
189
|
-
text_lines.each do |line|
|
190
|
-
logger.send(level, "[#{timestamp}] Natsy | #{indentation}#{line}")
|
191
|
-
end
|
192
|
-
|
193
|
-
nil
|
140
|
+
Utils.log(Config.logger, text, level: level, indent: indent)
|
194
141
|
end
|
195
142
|
|
196
143
|
def kill!
|
@@ -241,7 +188,7 @@ module Natsy
|
|
241
188
|
reply = {
|
242
189
|
subject: subject,
|
243
190
|
handler: handler,
|
244
|
-
queue: Utils.presence(queue) || default_queue,
|
191
|
+
queue: Utils.presence(queue) || Config.default_queue,
|
245
192
|
}
|
246
193
|
|
247
194
|
replies << reply
|
@@ -250,7 +197,7 @@ module Natsy
|
|
250
197
|
end
|
251
198
|
|
252
199
|
def listen
|
253
|
-
NATS.start do
|
200
|
+
NATS.start(servers: Natsy::Config.urls) do
|
254
201
|
replies.each do |replier|
|
255
202
|
queue_desc = " in queue '#{replier[:queue]}'" if replier[:queue]
|
256
203
|
log("Subscribing to subject '#{replier[:subject]}'#{queue_desc}", level: :debug)
|
data/lib/natsy/config.rb
ADDED
@@ -0,0 +1,261 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "ostruct"
|
4
|
+
require_relative "./utils"
|
5
|
+
|
6
|
+
module Natsy
|
7
|
+
# Represents the configuration options for +Natsy+. Configuration options are
|
8
|
+
# set using the `Natsy::Config::set` method, either as arguments or by using
|
9
|
+
# the appropriate setters on the object passed to the block.
|
10
|
+
class Config
|
11
|
+
# A +Natsy::Config::Options+ object is passed as a single argument to the
|
12
|
+
# block (if provided) for the +Natsy::Config::set+ method. This class should
|
13
|
+
# probably *NOT* be instantiated directly; instead, set the relevant options
|
14
|
+
# using +Natsy::Config.set(some_option: "...", some_other_option: "...")+.
|
15
|
+
# If you find yourself instantiating this class directly, there's probably a
|
16
|
+
# better way to do what you're trying to do.
|
17
|
+
class Options
|
18
|
+
# Specify a NATS server URL (or multiple URLs)
|
19
|
+
#
|
20
|
+
# **NOTE:** The following two examples do exactly the same thing.
|
21
|
+
#
|
22
|
+
# @example
|
23
|
+
# Natsy::Config.set(url: "nats://foo.bar:4567"))
|
24
|
+
#
|
25
|
+
# @example
|
26
|
+
# Natsy::Config.set do |options|
|
27
|
+
# options.url = "nats://foo.bar:4567"
|
28
|
+
# end
|
29
|
+
#
|
30
|
+
# **NOTE:** The following two examples do exactly the same thing.
|
31
|
+
#
|
32
|
+
# @example
|
33
|
+
# Natsy::Config.set(urls: ["nats://foo.bar:4567", "nats://foo.bar:5678"])
|
34
|
+
#
|
35
|
+
# @example
|
36
|
+
# Natsy::Config.set do |options|
|
37
|
+
# options.urls = ["nats://foo.bar:4567", "nats://foo.bar:5678"]
|
38
|
+
# end
|
39
|
+
#
|
40
|
+
# If left blank/omitted, +natsy+ will fall back on the default URL, which
|
41
|
+
# is +nats://localhost:4222+.
|
42
|
+
#
|
43
|
+
attr_accessor :url, :urls
|
44
|
+
|
45
|
+
# Attach a logger to have +natsy+ write out logs for messages
|
46
|
+
# received, responses sent, errors raised, lifecycle events, etc.
|
47
|
+
#
|
48
|
+
# **NOTE:** The following two examples do exactly the same thing.
|
49
|
+
#
|
50
|
+
# @example
|
51
|
+
# require 'natsy'
|
52
|
+
# require 'logger'
|
53
|
+
#
|
54
|
+
# nats_logger = Logger.new(STDOUT)
|
55
|
+
# nats_logger.level = Logger::INFO
|
56
|
+
#
|
57
|
+
# Natsy::Config.set(logger: nats_logger)
|
58
|
+
#
|
59
|
+
# @example
|
60
|
+
# require 'natsy'
|
61
|
+
# require 'logger'
|
62
|
+
#
|
63
|
+
# Natsy::Config.set do |options|
|
64
|
+
# nats_logger = Logger.new(STDOUT)
|
65
|
+
# nats_logger.level = Logger::INFO
|
66
|
+
#
|
67
|
+
# options.logger = nats_logger
|
68
|
+
# end
|
69
|
+
#
|
70
|
+
#
|
71
|
+
# In a Rails application, you might do this instead:
|
72
|
+
#
|
73
|
+
# @example
|
74
|
+
# Natsy::Config.set(logger: Rails.logger)
|
75
|
+
#
|
76
|
+
def logger=(new_logger)
|
77
|
+
@logger = new_logger
|
78
|
+
Utils.log(@logger, "Set the logger to #{@logger.inspect}", level: :debug)
|
79
|
+
end
|
80
|
+
|
81
|
+
# Optional logger for lifecycle events, messages received, etc.
|
82
|
+
#
|
83
|
+
# @see Natsy::Config::Options#logger=
|
84
|
+
#
|
85
|
+
attr_reader :logger
|
86
|
+
|
87
|
+
# Set a default queue for subscriptions.
|
88
|
+
#
|
89
|
+
# **NOTE:** The following two examples do exactly the same thing.
|
90
|
+
#
|
91
|
+
# @example
|
92
|
+
# Natsy::Config.set(default_queue: "foobar")
|
93
|
+
#
|
94
|
+
# @example
|
95
|
+
# Natsy::Config.set do |options|
|
96
|
+
# options.default_queue = "foobar"
|
97
|
+
# end
|
98
|
+
#
|
99
|
+
# Leave the +::default_queue+ blank (or assign +nil+) to use no default
|
100
|
+
# queue.
|
101
|
+
#
|
102
|
+
# **NOTE:** The following two examples do exactly the same thing.
|
103
|
+
#
|
104
|
+
# @example
|
105
|
+
# Natsy::Config.set(default_queue: nil)
|
106
|
+
#
|
107
|
+
# @example
|
108
|
+
# Natsy::Config.set do |options|
|
109
|
+
# options.default_queue = nil
|
110
|
+
# end
|
111
|
+
#
|
112
|
+
def default_queue=(new_queue)
|
113
|
+
@default_queue = Utils.presence(new_queue.to_s)
|
114
|
+
Utils.log(logger, "Setting the default queue to #{@default_queue || '(none)'}", level: :debug)
|
115
|
+
end
|
116
|
+
|
117
|
+
# Optional default queue for message subscription and replies.
|
118
|
+
#
|
119
|
+
# @see Natsy::Config::Options#default_queue=
|
120
|
+
#
|
121
|
+
attr_reader :default_queue
|
122
|
+
|
123
|
+
# Returns ONLY the config options THAT HAVE BEEN SET as a +Hash+. Will not
|
124
|
+
# have keys for properties that are unassigned, but will have keys for
|
125
|
+
# properties assigned +nil+.
|
126
|
+
def to_h
|
127
|
+
hash = {}
|
128
|
+
hash[:url] = url if defined?(@url)
|
129
|
+
hash[:urls] = urls if defined?(@urls)
|
130
|
+
hash[:logger] = logger if defined?(@logger)
|
131
|
+
hash[:default_queue] = default_queue if defined?(@default_queue)
|
132
|
+
hash
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
# Valid option keys that can be given to +Natsy::Config::set+, either in a
|
137
|
+
# +Hash+ passed to the method, keyword arguments passed to the method, or by
|
138
|
+
# using setters on the +Natsy::Config::Options+ object passed to the block.
|
139
|
+
VALID_OPTIONS = %i[
|
140
|
+
url
|
141
|
+
urls
|
142
|
+
logger
|
143
|
+
default_queue
|
144
|
+
].freeze
|
145
|
+
|
146
|
+
# The default NATS server URL (used if none is configured)
|
147
|
+
DEFAULT_URL = "nats://localhost:4222"
|
148
|
+
|
149
|
+
class << self
|
150
|
+
# Specify configuration options, either by providing them as keyword
|
151
|
+
# arguments or by using a block. Should you choose to set options using
|
152
|
+
# a block, it will be passed a single argument (an instance of
|
153
|
+
# +Natsy::Config::Options+). You can set any options on the instance that
|
154
|
+
# you see fit.
|
155
|
+
#
|
156
|
+
# **NOTE:** The following two examples do exactly the same thing.
|
157
|
+
#
|
158
|
+
# @example
|
159
|
+
# Natsy::Config.set(
|
160
|
+
# urls: ["nats://foo.bar:4567", "nats://foo.bar:5678"],
|
161
|
+
# default_queue: "foobar",
|
162
|
+
# logger: Rails.logger,
|
163
|
+
# )
|
164
|
+
#
|
165
|
+
# @example
|
166
|
+
# Natsy::Config.set do |options|
|
167
|
+
# options.urls = ["nats://foo.bar:4567", "nats://foo.bar:5678"]
|
168
|
+
# options.default_queue = "foobar"
|
169
|
+
# options.logger = Rails.logger
|
170
|
+
# end
|
171
|
+
#
|
172
|
+
def set(keyword_options = {})
|
173
|
+
new_hash_options = (keyword_options || {}).transform_keys(&:to_sym)
|
174
|
+
|
175
|
+
invalid_config = lambda do |detail, keys|
|
176
|
+
raise InvalidConfigError, "Invalid options provided #{detail}: #{keys.join(', ')}"
|
177
|
+
end
|
178
|
+
|
179
|
+
invalid_keys = invalid_option_keys(new_hash_options)
|
180
|
+
invalid_config.call("as arguments", invalid_keys) if invalid_keys.any?
|
181
|
+
|
182
|
+
# Want to take advantage of the setters on +Natsy::Config::Options+...
|
183
|
+
new_hash_options_object = new_hash_options.each_with_object(Options.new) do |(key, value), options|
|
184
|
+
options.send(:"#{key}=", value)
|
185
|
+
end
|
186
|
+
|
187
|
+
given_options.merge!(new_hash_options_object.to_h)
|
188
|
+
|
189
|
+
new_block_options_object = Options.new
|
190
|
+
yield(new_block_options_object) if block_given?
|
191
|
+
|
192
|
+
invalid_keys = invalid_option_keys(new_block_options_object)
|
193
|
+
invalid_config.call("in block", invalid_keys) if invalid_keys.any?
|
194
|
+
|
195
|
+
given_options.merge!(new_block_options_object.to_h)
|
196
|
+
end
|
197
|
+
|
198
|
+
# The NATS server URLs that +natsy+ should listen on.
|
199
|
+
#
|
200
|
+
# See also: {Natsy::Config::Options#urls=}
|
201
|
+
#
|
202
|
+
def urls
|
203
|
+
given_url_list = [given_options[:url]].flatten
|
204
|
+
given_urls_list = [given_options[:urls]].flatten
|
205
|
+
all_given_urls = [*given_url_list, *given_urls_list].compact.uniq
|
206
|
+
Utils.presence(all_given_urls) || [DEFAULT_URL]
|
207
|
+
end
|
208
|
+
|
209
|
+
# The logger that +natsy+ should use to write out logs for messages
|
210
|
+
# received, responses sent, errors raised, lifecycle events, etc.
|
211
|
+
#
|
212
|
+
# See also: {Natsy::Config::Options#logger=}
|
213
|
+
#
|
214
|
+
def logger
|
215
|
+
Utils.presence(given_options[:logger])
|
216
|
+
end
|
217
|
+
|
218
|
+
# The default queue that +natsy+ should use for subscriptions.
|
219
|
+
#
|
220
|
+
# See also: {Natsy::Config::Options#default_queue=}
|
221
|
+
#
|
222
|
+
def default_queue
|
223
|
+
Utils.presence(given_options[:default_queue])
|
224
|
+
end
|
225
|
+
|
226
|
+
# Returns all config options as a +Hash+.
|
227
|
+
def to_h
|
228
|
+
{
|
229
|
+
urls: urls,
|
230
|
+
logger: logger,
|
231
|
+
default_queue: default_queue,
|
232
|
+
}
|
233
|
+
end
|
234
|
+
|
235
|
+
# Alias for {Natsy::Config::to_h}.
|
236
|
+
def as_json(*_args)
|
237
|
+
to_h
|
238
|
+
end
|
239
|
+
|
240
|
+
# Serialize the configuration into a JSON object string.
|
241
|
+
def to_json(*_args)
|
242
|
+
to_h.to_json
|
243
|
+
end
|
244
|
+
|
245
|
+
# Reset the configuration to default values.
|
246
|
+
def reset!
|
247
|
+
@given_options = nil
|
248
|
+
end
|
249
|
+
|
250
|
+
private
|
251
|
+
|
252
|
+
def given_options
|
253
|
+
@given_options ||= {}
|
254
|
+
end
|
255
|
+
|
256
|
+
def invalid_option_keys(options)
|
257
|
+
options.to_h.keys - VALID_OPTIONS
|
258
|
+
end
|
259
|
+
end
|
260
|
+
end
|
261
|
+
end
|
data/lib/natsy/controller.rb
CHANGED
@@ -25,18 +25,18 @@ module Natsy
|
|
25
25
|
# end
|
26
26
|
#
|
27
27
|
# If omitted, the controller will fall back on the global default queue
|
28
|
-
# assigned with +Natsy::
|
29
|
-
#
|
30
|
-
#
|
31
|
-
#
|
32
|
-
#
|
28
|
+
# assigned with +Natsy::Config::set+. If no default queue is set in either
|
29
|
+
# the controller or globally, then the default queue will be blank. Set
|
30
|
+
# the default queue to +nil+ in a controller to override the global
|
31
|
+
# default queue and explicitly make the default queue blank for that
|
32
|
+
# controller.
|
33
33
|
#
|
34
34
|
def default_queue(some_queue = NO_QUEUE_GIVEN)
|
35
35
|
# +NO_QUEUE_GIVEN+ is a special symbol (rather than +nil+) so that the
|
36
36
|
# default queue can be "unset" to +nil+ (given a non-+nil+ global
|
37
|
-
# default set with +Natsy::Client::
|
37
|
+
# default set with +Natsy::Client::set+).
|
38
38
|
if some_queue == NO_QUEUE_GIVEN
|
39
|
-
@default_queue ||
|
39
|
+
@default_queue || Config.default_queue
|
40
40
|
else
|
41
41
|
@default_queue = Utils.presence(some_queue.to_s)
|
42
42
|
end
|
data/lib/natsy/utils.rb
CHANGED
@@ -15,6 +15,20 @@ module Natsy
|
|
15
15
|
def presence(value)
|
16
16
|
present?(value) ? value : nil
|
17
17
|
end
|
18
|
+
|
19
|
+
def log(logger, text, level: :info, indent: 0)
|
20
|
+
return unless logger
|
21
|
+
|
22
|
+
timestamp = Time.now.to_s
|
23
|
+
text_lines = text.split("\n")
|
24
|
+
indentation = indent.is_a?(String) ? indent : (" " * indent)
|
25
|
+
|
26
|
+
text_lines.each do |line|
|
27
|
+
logger.send(level, "[#{timestamp}] Natsy | #{indentation}#{line}")
|
28
|
+
end
|
29
|
+
|
30
|
+
nil
|
31
|
+
end
|
18
32
|
end
|
19
33
|
end
|
20
34
|
end
|
data/lib/natsy/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: natsy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Keegan Leitz
|
@@ -170,6 +170,7 @@ files:
|
|
170
170
|
- bin/setup
|
171
171
|
- lib/natsy.rb
|
172
172
|
- lib/natsy/client.rb
|
173
|
+
- lib/natsy/config.rb
|
173
174
|
- lib/natsy/controller.rb
|
174
175
|
- lib/natsy/utils.rb
|
175
176
|
- lib/natsy/version.rb
|
@@ -178,7 +179,7 @@ homepage: https://github.com/openbay/natsy
|
|
178
179
|
licenses:
|
179
180
|
- MIT
|
180
181
|
metadata:
|
181
|
-
documentation_uri: https://www.rubydoc.info/gems/natsy/0.
|
182
|
+
documentation_uri: https://www.rubydoc.info/gems/natsy/0.4.0
|
182
183
|
post_install_message:
|
183
184
|
rdoc_options: []
|
184
185
|
require_paths:
|