tiny_bus 1.0.0 → 1.0.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 +4 -4
- data/lib/tiny_bus.rb +20 -17
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d4d536f5d26c59f828baf005884e8d6be51596b61c151a2c77e129c2a1f80305
|
4
|
+
data.tar.gz: 885f08feed148a3808dfde24a38871544e01ac82fe27114ef8000e7fe0f8a630
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8ead2a2d5889921a3544a8ffc91fe93929bb3a52efe3edd1de14c0046533641f7a170f1de32b065cdeb19984d007904a6ef391a9b58fcb61839baf557868a33e
|
7
|
+
data.tar.gz: 453bd1af26f50d84155a45ba454f66fbdf1cd83b6418cd0543120c3a012a0f91738d6109faea9f983656ed4bdd2ebf0992de95ca449fc3fcbeb0b154a008bd6c
|
data/lib/tiny_bus.rb
CHANGED
@@ -6,23 +6,23 @@ require 'tiny_log'
|
|
6
6
|
# This class implements a very simpler PubSub system where:
|
7
7
|
# - subscribers can subscribe via the #sub method
|
8
8
|
# - subscribers can unsubscribe via the #unsub method
|
9
|
-
# - msgs can enter the
|
9
|
+
# - msgs can enter the TinyBus via the #msg method
|
10
10
|
#
|
11
|
-
# The messages that come into this
|
11
|
+
# The messages that come into this TinyBus are assumed to be Hash-like, in the
|
12
12
|
# sense that they have a 'topic' key that can be accessed using Hash-like key
|
13
13
|
# access syntax, and that the 'topic' key will serve as the method of
|
14
14
|
# distribution.
|
15
15
|
#
|
16
16
|
# Usage:
|
17
|
-
#
|
18
|
-
#
|
19
|
-
#
|
20
|
-
#
|
17
|
+
# t = TinyBus.new
|
18
|
+
# t.sub('news', <some object that responds to #msg)
|
19
|
+
# t.msg({'topic' => 'news', 'details' => 'Historic happenings!}) # goes to 'news' subscribers
|
20
|
+
# t.msg({'topic' => 'whatever', 'details' => 'Historic happenings!}) # goes to dead letter output, or raises exception, depending on the configuration
|
21
21
|
#
|
22
22
|
# Initialization options:
|
23
|
-
#
|
24
|
-
#
|
25
|
-
#
|
23
|
+
# TinyBus.new(log: <some object that responds to #puts>) # will send a copy of all successful messages to the log
|
24
|
+
# TinyBus.new(dead: <some object that responds to #puts>) # will send a copy of all unsuccessful messages to the dead object
|
25
|
+
# TinyBus.new(raise_on_dead: true) # strict mode for undeliverable messages, defaults to false
|
26
26
|
class TinyBus
|
27
27
|
# log:
|
28
28
|
# if specified it should be a valid filename
|
@@ -45,11 +45,11 @@ class TinyBus
|
|
45
45
|
# adds a subscriber to a topic
|
46
46
|
#
|
47
47
|
# topics can be any string that doesn't start with a dot (.) - dot topics are
|
48
|
-
# reserved for internal
|
48
|
+
# reserved for internal TinyBus usage, such as:
|
49
49
|
# - .log
|
50
50
|
def sub(topic, subber)
|
51
|
-
raise SubscriptionToDotTopicError.new("Cannot subscribe to dot topic `#{topic}', because these are reserved for internal use") if topic.start_with?('.')
|
52
|
-
raise SubscriberDoesNotMsg.new("The specified subscriber type `#{subber.class.inspect}' does not respond to #msg") unless subber.respond_to?(:msg)
|
51
|
+
raise TinyBus::SubscriptionToDotTopicError.new("Cannot subscribe to dot topic `#{topic}', because these are reserved for internal use") if topic.start_with?('.')
|
52
|
+
raise TinyBus::SubscriberDoesNotMsg.new("The specified subscriber type `#{subber.class.inspect}' does not respond to #msg") unless subber.respond_to?(:msg)
|
53
53
|
|
54
54
|
@subs[topic] ||= Set.new
|
55
55
|
@subs[topic] << subber
|
@@ -84,7 +84,7 @@ class TinyBus
|
|
84
84
|
@log.puts annotated
|
85
85
|
else
|
86
86
|
if @raise_on_dead
|
87
|
-
raise
|
87
|
+
raise TinyBus::DeadMsgError.new("Could not deliver message to topic `#{t}'")
|
88
88
|
else
|
89
89
|
@stats['.dead'] += 1
|
90
90
|
@dead.puts annotated
|
@@ -92,13 +92,16 @@ class TinyBus
|
|
92
92
|
end
|
93
93
|
end
|
94
94
|
|
95
|
+
# helpful for debugging, gives you a count of the number of messages sent to
|
96
|
+
# each topic, including the .dead topic, which is where messages go where
|
97
|
+
# there are no subscribes for a given topic
|
95
98
|
def to_s
|
96
99
|
<<~DEBUG
|
97
|
-
|
100
|
+
TinyBus stats: #{@subs.keys.length > 0 ? "\n " + @stats.keys.sort.map{|t| "#{t.rjust(12)}: #{@stats[t]}" }.join("\n ") : '<NONE>'}
|
98
101
|
DEBUG
|
99
102
|
end
|
100
103
|
end
|
101
104
|
|
102
|
-
class DeadMsgError < RuntimeError; end
|
103
|
-
class SubscriptionToDotTopicError < RuntimeError; end
|
104
|
-
class SubscriberDoesNotMsg < RuntimeError; end
|
105
|
+
class TinyBus::DeadMsgError < RuntimeError; end
|
106
|
+
class TinyBus::SubscriptionToDotTopicError < RuntimeError; end
|
107
|
+
class TinyBus::SubscriberDoesNotMsg < RuntimeError; end
|