tiny_bus 1.0.1 → 1.0.3

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 +17 -13
  3. metadata +5 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d4d536f5d26c59f828baf005884e8d6be51596b61c151a2c77e129c2a1f80305
4
- data.tar.gz: 885f08feed148a3808dfde24a38871544e01ac82fe27114ef8000e7fe0f8a630
3
+ metadata.gz: 167d35639547e4dd4a52b2882a02926cc7cc1b1e73c0ef2affaca995190f70a3
4
+ data.tar.gz: 38326f77eba40699377e2b42f99e15484286c97028cdea5f35018d930f1fa18d
5
5
  SHA512:
6
- metadata.gz: 8ead2a2d5889921a3544a8ffc91fe93929bb3a52efe3edd1de14c0046533641f7a170f1de32b065cdeb19984d007904a6ef391a9b58fcb61839baf557868a33e
7
- data.tar.gz: 453bd1af26f50d84155a45ba454f66fbdf1cd83b6418cd0543120c3a012a0f91738d6109faea9f983656ed4bdd2ebf0992de95ca449fc3fcbeb0b154a008bd6c
6
+ metadata.gz: a588b106a52b799ab9c70f271ce2b4dc51fdac19f5b0518c53a95d5f5932b2eeda9ad53a5d61644e1d5aeba6ec0a0802540519ebbd21bc3e0c89f44ba8cd2ebd
7
+ data.tar.gz: 61f2606e77ea63a9638fadc926d9c8020db234946271ed829b4b6d4e3d09115c6c260f81fca86830df1681363e1d002731fc63cba058d0311d6ab497c019fd24
data/lib/tiny_bus.rb CHANGED
@@ -20,9 +20,9 @@ require 'tiny_log'
20
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
- # 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
23
+ # TinyBus.new(log: <a filename for log output>) # will log all normal msgs in this file
24
+ # TinyBus.new(dead: <a filename for dead message log output>) # will log all undeliverable msgs in this file
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
@@ -37,8 +37,8 @@ class TinyBus
37
37
  def initialize(log: nil, dead: nil, raise_on_dead: false)
38
38
  @subs = {}
39
39
  @stats = { '.dead' => 0 }
40
- @log = log ? TinyLog.new(log) : $stdout
41
- @dead = dead ? File.open(dead, 'a') : $stderr
40
+ @log = log ? TinyLog.new(log) : TinyLog.new($stdout)
41
+ @dead = dead ? File.open(dead, 'a') : TinyLog.new($stderr)
42
42
  @raise_on_dead = raise_on_dead
43
43
  end
44
44
 
@@ -48,7 +48,7 @@ class TinyBus
48
48
  # reserved for internal TinyBus usage, such as:
49
49
  # - .log
50
50
  def sub(topic, subber)
51
- raise TinyBus::SubscriptionToDotTopicError.new("Cannot subscribe to dot topic `#{topic}', because these are reserved for internal use") if topic.start_with?('.')
51
+ raise TinyBus::SubscriptionToDotTopicError.new("Cannot subscribe to dot topic `#{topic}', because those are reserved for internal use") if topic.start_with?('.')
52
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
@@ -69,9 +69,12 @@ class TinyBus
69
69
  #
70
70
  # NOTE: it modifies the incoming msg object in place in order to avoid
71
71
  # unnecessary object allocations
72
- def msg(msg)
73
- t = msg['topic']
74
- subbers = @subs[t]
72
+ def msg(msg, lvl='info')
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,15 +82,15 @@ 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
- @log.puts annotated
87
+ @log.send(lvl, annotated)
85
88
  else
86
89
  if @raise_on_dead
87
- raise TinyBus::DeadMsgError.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
- @dead.puts annotated
93
+ @dead.send(lvl, annotated)
91
94
  end
92
95
  end
93
96
  end
@@ -105,3 +108,4 @@ end
105
108
  class TinyBus::DeadMsgError < RuntimeError; end
106
109
  class TinyBus::SubscriptionToDotTopicError < RuntimeError; end
107
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.1
4
+ version: 1.0.3
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: []