Module: Cloudenvoy::Backend::MemoryPubSub
- Defined in:
- lib/cloudenvoy/backend/memory_pub_sub.rb
Overview
Store messages in a memory queue. Used for testing
Class Method Summary collapse
-
.clear(topic) ⇒ Array
Clear all messages in a specific topic.
-
.clear_all ⇒ Object
Clear all messages across all topics.
-
.publish(topic, payload, metadata = {}) ⇒ Cloudenvoy::Message
Publish a message to a topic.
-
.publish_all(topic, msg_args) ⇒ Array<Cloudenvoy::Message>
Publish multiple messages to a topic.
-
.queue(topic) ⇒ Array
Return the message queue for a specific topic.
-
.upsert_subscription(_topic, name, _opts) ⇒ Cloudenvoy::Subscription
Create or update a subscription for a specific topic.
-
.upsert_topic(topic) ⇒ Cloudenvoy::Topic
Create or update a topic.
Class Method Details
.clear(topic) ⇒ Array
Clear all messages in a specific topic.
30 31 32 |
# File 'lib/cloudenvoy/backend/memory_pub_sub.rb', line 30 def clear(topic) queue(topic).clear end |
.clear_all ⇒ Object
Clear all messages across all topics.
39 40 41 |
# File 'lib/cloudenvoy/backend/memory_pub_sub.rb', line 39 def clear_all @queues&.values&.each { |e| e.clear } end |
.publish(topic, payload, metadata = {}) ⇒ Cloudenvoy::Message
Publish a message to a topic.
52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/cloudenvoy/backend/memory_pub_sub.rb', line 52 def publish(topic, payload, = {}) msg = Message.new( id: SecureRandom.uuid, payload: payload, metadata: , topic: topic ) queue(topic).push(msg) msg end |
.publish_all(topic, msg_args) ⇒ Array<Cloudenvoy::Message>
Publish multiple messages to a topic.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/cloudenvoy/backend/memory_pub_sub.rb', line 72 def publish_all(topic, msg_args) # Build the messages msgs = msg_args.map do |(payload, )| Message.new( id: SecureRandom.uuid, payload: payload, metadata: , topic: topic ) end # Push all the messages and return them queue(topic).push(*msgs) msgs end |
.queue(topic) ⇒ Array
Return the message queue for a specific topic.
18 19 20 21 |
# File 'lib/cloudenvoy/backend/memory_pub_sub.rb', line 18 def queue(topic) @queues ||= {} @queues[topic.to_s] ||= [] end |
.upsert_subscription(_topic, name, _opts) ⇒ Cloudenvoy::Subscription
Create or update a subscription for a specific topic.
97 98 99 |
# File 'lib/cloudenvoy/backend/memory_pub_sub.rb', line 97 def upsert_subscription(_topic, name, _opts) Subscription.new(name: name) end |
.upsert_topic(topic) ⇒ Cloudenvoy::Topic
Create or update a topic.
108 109 110 |
# File 'lib/cloudenvoy/backend/memory_pub_sub.rb', line 108 def upsert_topic(topic) Topic.new(name: topic) end |