natsy 0.4.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c6bc9668d49b9d899ccff640de76b89dfdd2063e44d08d8189a1fcce934e2d70
4
- data.tar.gz: 3a7b7f59a818778db5a8fd48c5b34d26842a900e8b340a3be1d8541569c671bf
3
+ metadata.gz: ece7795e8d0829b951bc59e8be816af7b48d8be4c30ddbb7cc6d30a802439ce6
4
+ data.tar.gz: 9cd71c604e80e6b2caddec98d159a3bc27487bbd3c295ca779e9d88b7df26968
5
5
  SHA512:
6
- metadata.gz: 2c4f742f409308fae258a8c0de19c4072757855073b1ccab1114b5e818bd29c53bdd269aa330942457c2c737ee798f2b1b6606c678055babc815d576fbb9dd78
7
- data.tar.gz: 9b879994fe87e9a4430accf0a837a03989cdd50b18388e13877541203f91d47f394af8fd64f43e49a4fd34196dee4ca0e1af2bd9035b2e4061419a419115a123
6
+ metadata.gz: 9b9339a4ab42f14e494f6283a50bdfb32465a81c361dc488d636d14d79f62be52e2448a9ebd1f756e8e9dc77be5f001231a87bfa1e3e1535c37b9a7c634a8784
7
+ data.tar.gz: 60dcb7f42d58fa4f6a42795ebd7ead07e74e58282564139f360f14da37cdc76e749e55af036bfcff79589bbd002a6f8cb02cb9e3da35e92c72a83b53911d465c
data/.rubocop.yml CHANGED
@@ -26,7 +26,7 @@ Layout/FirstArrayElementIndentation:
26
26
  # Metrics
27
27
 
28
28
  Metrics/AbcSize:
29
- Max: 30
29
+ Max: 35
30
30
  CountRepeatedAttributes: false
31
31
  Exclude:
32
32
  - 'spec/**/*_spec.rb'
@@ -62,6 +62,9 @@ Metrics/MethodLength:
62
62
  - array
63
63
  - hash
64
64
  - heredoc
65
+ Exclude:
66
+ - 'spec/**/*_spec.rb'
67
+ - '*.gemspec'
65
68
 
66
69
  Metrics/ModuleLength:
67
70
  Max: 150
@@ -75,7 +78,7 @@ Metrics/ModuleLength:
75
78
  - '*.gemspec'
76
79
 
77
80
  Metrics/PerceivedComplexity:
78
- Max: 15
81
+ Max: 20
79
82
  Exclude:
80
83
  - 'spec/**/*_spec.rb'
81
84
  - '*.gemspec'
data/README.md CHANGED
@@ -5,14 +5,14 @@ The `natsy` gem allows you to listen for (and reply to) NATS messages asynchrono
5
5
  ## TODO
6
6
 
7
7
  - [x] docs
8
- - [ ] tests
8
+ - [x] tests
9
9
  - [x] "controller"-style classes for reply organization
10
10
  - [x] runtime subscription additions
11
11
  - [x] multiple queues
12
- - [ ] `on_error` handler so you can send a response (what's standard?)
13
- - [ ] config options for URL/host/port/etc.
12
+ - [x] config options for URL/host/port/etc.
14
13
  - [ ] config for restart behavior (default is to restart listening on any `StandardError`)
15
- - [ ] consider using child processes instead of threads
14
+ - [ ] `on_error` handler so you can send a response (what's standard?)
15
+ - [ ] support lifecycle callbacks (like `on_connect`, `on_disconnect`, etc.) provided by the `nats` gem
16
16
 
17
17
  ## Installation
18
18
 
@@ -240,7 +240,7 @@ Natsy::Client.start!
240
240
 
241
241
  Create controller classes which inherit from `Natsy::Controller` in order to give your message listeners some structure.
242
242
 
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.
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 fall back to the global default queue.
244
244
 
245
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.
246
246
 
@@ -276,9 +276,13 @@ class HelloController < Natsy::Controller
276
276
  end
277
277
 
278
278
  subject "hows" do
279
- subject "*" do
279
+ # The queue at this point is "foobar"
280
+ subject "*", queue: "barbaz" do # Override the default queue at any point
281
+ # The queue at this point is "barbaz" (!)
280
282
  subject "doing" do
281
- response do |data, subject|
283
+ # The queue at this point is "barbaz"
284
+ response queue: "bazbam" do |data, subject|
285
+ # The queue at this point is "bazbam" (!)
282
286
  # The subject at this point is "hows.<wildcard>.doing" (i.e., the
283
287
  # subjects "hows.jack.doing" and "hows.jill.doing" will both match)
284
288
  sender_name = data["name"]
@@ -352,6 +356,12 @@ rake spec
352
356
  bundle exec rubocop
353
357
  ```
354
358
 
359
+ ...or (if your Ruby setup has good defaults) just this:
360
+
361
+ ```bash
362
+ rubocop
363
+ ```
364
+
355
365
  ### Create a release
356
366
 
357
367
  Bump the `Natsy::VERSION` value in `lib/natsy/version.rb`, commit, and then run:
data/lib/natsy/client.rb CHANGED
@@ -126,6 +126,14 @@ module Natsy
126
126
  threads << thread
127
127
  end
128
128
 
129
+ # **USE WITH CAUTION:** This method (+::reset!+) clears all subscriptions,
130
+ # stops listening (if started), and kills any active threads.
131
+ def reset!
132
+ replies.clear
133
+ stop!
134
+ kill!
135
+ end
136
+
129
137
  private
130
138
 
131
139
  def threads
@@ -203,7 +211,11 @@ module Natsy
203
211
  log("Subscribing to subject '#{replier[:subject]}'#{queue_desc}", level: :debug)
204
212
 
205
213
  NATS.subscribe(replier[:subject], queue: replier[:queue]) do |message, reply_subject, subject|
206
- parsed_message = JSON.parse(message)
214
+ parsed_message = begin
215
+ JSON.parse(message)
216
+ rescue StandardError
217
+ message
218
+ end
207
219
 
208
220
  id, data, pattern = if parsed_message.is_a?(Hash)
209
221
  parsed_message.values_at("id", "data", "pattern")
@@ -211,18 +223,21 @@ module Natsy
211
223
  [nil, parsed_message, nil]
212
224
  end
213
225
 
226
+ message_data = id && data && pattern ? data : parsed_message
227
+
214
228
  log("Received a message!")
215
229
  message_desc = <<~LOG_MESSAGE
216
230
  id: #{id || '(none)'}
217
231
  pattern: #{pattern || '(none)'}
218
232
  subject: #{subject || '(none)'}
219
- data: #{data.to_json}
233
+ data: #{message_data.to_json}
220
234
  inbox: #{reply_subject || '(none)'}
221
235
  queue: #{replier[:queue] || '(none)'}
236
+ message: #{message}
222
237
  LOG_MESSAGE
223
238
  log(message_desc, indent: 2)
224
239
 
225
- raw_response = replier[:handler].call(data)
240
+ raw_response = replier[:handler].call(message_data, subject)
226
241
 
227
242
  log("Responding with '#{raw_response}'")
228
243
 
@@ -27,9 +27,8 @@ module Natsy
27
27
  # If omitted, the controller will fall back on the global default queue
28
28
  # assigned with +Natsy::Config::set+. If no default queue is set in either
29
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.
30
+ # the default queue to +nil+ in a controller to fall back to the global
31
+ # default queue.
33
32
  #
34
33
  def default_queue(some_queue = NO_QUEUE_GIVEN)
35
34
  # +NO_QUEUE_GIVEN+ is a special symbol (rather than +nil+) so that the
data/lib/natsy/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Natsy
4
- VERSION = "0.4.0"
4
+ VERSION = "0.4.1"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: natsy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Keegan Leitz
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-05-17 00:00:00.000000000 Z
11
+ date: 2021-05-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -161,7 +161,6 @@ files:
161
161
  - ".gitignore"
162
162
  - ".rspec"
163
163
  - ".rubocop.yml"
164
- - CHANGELOG.md
165
164
  - Gemfile
166
165
  - LICENSE.txt
167
166
  - README.md
@@ -179,7 +178,7 @@ homepage: https://github.com/openbay/natsy
179
178
  licenses:
180
179
  - MIT
181
180
  metadata:
182
- documentation_uri: https://www.rubydoc.info/gems/natsy/0.4.0
181
+ documentation_uri: https://www.rubydoc.info/gems/natsy/0.4.1
183
182
  post_install_message:
184
183
  rdoc_options: []
185
184
  require_paths:
data/CHANGELOG.md DELETED
@@ -1,5 +0,0 @@
1
- ## [Unreleased]
2
-
3
- ## [0.1.0] - 2021-05-10
4
-
5
- - Initial release