tiny_bus 1.0.0 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/tiny_bus.rb +27 -20
  3. metadata +5 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: efb4754b66b36fa51ecd847ef7a0a92b8ef884c662e50d51cb3a1353d5fe98dc
4
- data.tar.gz: dc6c393864d50f47131e7efc4dd2ec90781b5552ef68a452b4d0a92998dceb88
3
+ metadata.gz: fb6142b34ec3c5c72a2a4073b080d8aba77dd40b9c04df3df89d9802a26c5c39
4
+ data.tar.gz: b94962edf28bd4ad4affecb69cfd6d68d298b6623b98518cbbc9059fc117c83d
5
5
  SHA512:
6
- metadata.gz: 4275c81f97f72fca9aa1d33a8cdff08afc70392539b3d98096a7878118c69f7eabd5fbb9e336c76a3adf357130ec492ce78715aed3edfe6cb83d83e605ec4e68
7
- data.tar.gz: 99dc348ba1e2d1281da1bd84976a6b54bb14792633f93a36e4bf507cc7fe5e1a48faae79a2d24fd6e9fd15c88c5dbcf1533b6c72e264ffb41a751e88ae432580
6
+ metadata.gz: 1a29c06a16aab93b03d5a68384c55a4f461dd2a5e3b06c888d0d420325cd169dd1a16284af10a467eeb6953a525a809287deed3b25781a831ade89847d8dd0aa
7
+ data.tar.gz: 05de4ca2e32edad9a3d63a065752c1990135d5de66710f957dfbc49838a6f45e2ffb9b4c901f831a073bbf14dd4a5291ef62c9d96a9219a9021c4a32f197176b
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 MsgBus via the #msg method
9
+ # - msgs can enter the TinyBus via the #msg method
10
10
  #
11
- # The messages that come into this MsgBus are assumed to be Hash-like, in the
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
- # mb = MsgBus.new
18
- # mb.sub('news', <some object that responds to #msg)
19
- # mb.msg({'topic' => 'news', 'details' => 'Historic happenings!}) # goes to 'news' subscribers
20
- # mb.msg({'topic' => 'whatever', 'details' => 'Historic happenings!}) # goes to dead letter output, or raises exception, depending on the configuration
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
- # MsgBus.new(log: <some object that responds to #puts>) # will send a copy of all successful messages to the log
24
- # MsgBus.new(dead: <some object that responds to #puts>) # will send a copy of all unsuccessful messages to the dead object
25
- # MsgBus.new(raise_on_dead: true) # strict mode for undeliverable messages, defaults to false
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 MsgBus usage, such as:
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 those 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
@@ -70,8 +70,11 @@ class TinyBus
70
70
  # NOTE: it modifies the incoming msg object in place in order to avoid
71
71
  # unnecessary object allocations
72
72
  def msg(msg)
73
- t = msg['topic']
74
- subbers = @subs[t]
73
+ topic = msg['topic']
74
+
75
+ raise TinyBus::SendToDotTopicError.new("Cannot send to dot topic `#{topic}', because those are reserved for internal use") if topic.start_with?('.')
76
+
77
+ subbers = @subs[topic]
75
78
 
76
79
  annotated = msg.merge!({
77
80
  '.time' => Time.now.utc.iso8601(6),
@@ -79,12 +82,12 @@ class TinyBus
79
82
  })
80
83
 
81
84
  if subbers
82
- @stats[t] += 1
85
+ @stats[topic] += 1
83
86
  subbers.each{|s| s.msg(annotated) }
84
87
  @log.puts annotated
85
88
  else
86
89
  if @raise_on_dead
87
- raise DeadMsgException.new("Could not deliver message to topic `#{t}'")
90
+ raise TinyBus::DeadMsgError.new("Could not deliver message to topic `#{topic}'")
88
91
  else
89
92
  @stats['.dead'] += 1
90
93
  @dead.puts annotated
@@ -92,13 +95,17 @@ class TinyBus
92
95
  end
93
96
  end
94
97
 
98
+ # helpful for debugging, gives you a count of the number of messages sent to
99
+ # each topic, including the .dead topic, which is where messages go where
100
+ # there are no subscribes for a given topic
95
101
  def to_s
96
102
  <<~DEBUG
97
- MsgBus stats: #{@subs.keys.length > 0 ? "\n " + @stats.keys.sort.map{|t| "#{t.rjust(12)}: #{@stats[t]}" }.join("\n ") : '<NONE>'}
103
+ TinyBus stats: #{@subs.keys.length > 0 ? "\n " + @stats.keys.sort.map{|t| "#{t.rjust(12)}: #{@stats[t]}" }.join("\n ") : '<NONE>'}
98
104
  DEBUG
99
105
  end
100
106
  end
101
107
 
102
- class DeadMsgError < RuntimeError; end
103
- class SubscriptionToDotTopicError < RuntimeError; end
104
- class SubscriberDoesNotMsg < RuntimeError; end
108
+ class TinyBus::DeadMsgError < RuntimeError; end
109
+ class TinyBus::SubscriptionToDotTopicError < RuntimeError; end
110
+ class TinyBus::SubscriberDoesNotMsg < RuntimeError; end
111
+ class TinyBus::SendToDotTopicError< RuntimeError; end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tiny_bus
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Lunt
@@ -24,9 +24,7 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
- description: want to have an in-memory message bus that takes hash-like objects and
28
- distributes them out to subscribers based on a 'topic' key, with logging to $stdout
29
- or a file, and absolutely nothing else? then this library is for you
27
+ description: a tiny pubsub message bus with almost no features
30
28
  email: jefflunt@gmail.com
31
29
  executables: []
32
30
  extensions: []
@@ -55,5 +53,7 @@ requirements: []
55
53
  rubygems_version: 3.0.3.1
56
54
  signing_key:
57
55
  specification_version: 4
58
- summary: a tiny pubsub message bus with almost no features
56
+ summary: want to have an in-memory message bus that takes hash-like objects and distributes
57
+ them out to subscribers based on a 'topic' key, with logging to $stdout or a file,
58
+ and absolutely nothing else? then this library is for you
59
59
  test_files: []